code.go 753 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. func readInput(file *os.File) map[string][]string {
  10. scanner := bufio.NewScanner(file)
  11. devices := make(map[string][]string)
  12. for scanner.Scan() {
  13. line := scanner.Text()
  14. if line == "" {
  15. continue
  16. }
  17. parts := strings.Split(line, ": ")
  18. if len(parts) < 2 {
  19. log.Fatalf("Bad input: %s", line)
  20. }
  21. name := parts[0]
  22. connections := strings.Split(parts[1], " ")
  23. devices[name] = connections
  24. }
  25. return devices
  26. }
  27. func main() {
  28. if len(os.Args) < 2 {
  29. log.Fatal("You need to specify a file!")
  30. }
  31. filePath := os.Args[1]
  32. file, err := os.Open(filePath)
  33. if err != nil {
  34. log.Fatalf("Failed to open %s!\n", filePath)
  35. }
  36. devices := readInput(file)
  37. fmt.Println(devices)
  38. }