code.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type blueprint struct {
  9. id int
  10. oreCost int
  11. clayCost int
  12. obsidianCost [2]int
  13. geodeCost [2]int
  14. }
  15. func readInput(file *os.File) []blueprint {
  16. scanner := bufio.NewScanner(file)
  17. var blueprints []blueprint
  18. for scanner.Scan() {
  19. line := scanner.Text()
  20. if line == "" {
  21. continue
  22. }
  23. var current blueprint
  24. n, err := fmt.Sscanf(line, "Blueprint %d: Each ore robot costs %d ore. Each clay robot costs %d ore. Each obsidian robot costs %d ore and %d clay. Each geode robot costs %d ore and %d obsidian.", &current.id, &current.oreCost, &current.clayCost, &current.obsidianCost[0], &current.obsidianCost[1], &current.geodeCost[0], &current.geodeCost[1])
  25. if n != 7 || err != nil {
  26. log.Fatal("Can't parse:", line, err)
  27. }
  28. blueprints = append(blueprints, current)
  29. }
  30. return blueprints
  31. }
  32. func main() {
  33. if len(os.Args) < 2 {
  34. log.Fatal("You need to specify a file!")
  35. }
  36. filePath := os.Args[1]
  37. file, err := os.Open(filePath)
  38. if err != nil {
  39. log.Fatalf("Failed to open %s!\n", filePath)
  40. }
  41. blueprints := readInput(file)
  42. fmt.Println(blueprints)
  43. }