Browse Source

Able to read keys

Piotr Czajkowski 3 years ago
parent
commit
f52e7a138f
1 changed files with 53 additions and 0 deletions
  1. 53 0
      day25/day25.go

+ 53 - 0
day25/day25.go

@@ -0,0 +1,53 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+	"strconv"
+)
+
+func readFile(file *os.File) []int {
+	var keys []int
+	scanner := bufio.NewScanner(file)
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		key, err := strconv.Atoi(string(line))
+		if err != nil {
+			log.Fatalf("Error processing key for %s: %s", line, err)
+		}
+
+		keys = append(keys, key)
+	}
+	if err := scanner.Err(); err != nil {
+		log.Fatalf("Scanner error: %s", err)
+	}
+
+	return keys
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+
+	}
+
+	keys := readFile(file)
+	if err := file.Close(); err != nil {
+		log.Fatalf("Failed to close file: %s", err)
+	}
+
+	fmt.Println(keys)
+}