| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package main
- import (
- "bufio"
- "fmt"
- "log"
- "os"
- "strings"
- )
- func readInput(file *os.File) map[string][]string {
- scanner := bufio.NewScanner(file)
- devices := make(map[string][]string)
- for scanner.Scan() {
- line := scanner.Text()
- if line == "" {
- continue
- }
- parts := strings.Split(line, ": ")
- if len(parts) < 2 {
- log.Fatalf("Bad input: %s", line)
- }
- name := parts[0]
- connections := strings.Split(parts[1], " ")
- devices[name] = connections
- }
- return devices
- }
- func part1(entry string, devices map[string][]string) int {
- if entry == "out" {
- return 1
- }
- var count int
- for _, device := range devices[entry] {
- count += part1(device, devices)
- }
- return count
- }
- 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)
- }
- devices := readInput(file)
- fmt.Println("Part1:", part1("you", devices))
- }
|