浏览代码

Able to read input

Piotr Czajkowski 3 月之前
父节点
当前提交
a0442dc0d6
共有 1 个文件被更改,包括 46 次插入0 次删除
  1. 46 0
      23/code.go

+ 46 - 0
23/code.go

@@ -0,0 +1,46 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+	"strings"
+)
+
+func readInput(file *os.File) map[string][]string {
+	scanner := bufio.NewScanner(file)
+	computers := make(map[string][]string)
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		parts := strings.Split(line, "-")
+		if len(parts) != 2 {
+			log.Fatalf("Bad input: %s", line)
+		}
+
+		computers[parts[0]] = append(computers[parts[0]], parts[1])
+		computers[parts[1]] = append(computers[parts[1]], parts[0])
+	}
+
+	return computers
+}
+
+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)
+	}
+
+	computers := readInput(file)
+	fmt.Println(computers)
+}