code.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type Cube struct {
  10. count int
  11. color string
  12. }
  13. type Game struct {
  14. id int
  15. sets [][]Cube
  16. }
  17. func readInput(file *os.File) []Game {
  18. scanner := bufio.NewScanner(file)
  19. var games []Game
  20. for scanner.Scan() {
  21. line := scanner.Text()
  22. if line == "" {
  23. break
  24. }
  25. var current Game
  26. n, err := fmt.Sscanf(line, "Game %d:", &current.id)
  27. if n != 1 || err != nil {
  28. log.Fatalf("Failed to read game: %s\n%s", line, err)
  29. }
  30. gameParts := strings.Split(line, ":")
  31. if len(gameParts) != 2 {
  32. log.Fatalf("Wrong input: %s", line)
  33. }
  34. sets := strings.Split(gameParts[1], ";")
  35. if len(sets) == 0 {
  36. log.Fatalf("Wrong input: %s", gameParts[1])
  37. }
  38. for i := range sets {
  39. var set []Cube
  40. cubes := strings.Split(sets[i], ",")
  41. if len(cubes) == 0 {
  42. log.Fatalf("Wrong input: %s", sets[i])
  43. }
  44. for j := range cubes {
  45. var cube Cube
  46. n, err = fmt.Sscanf(cubes[j], "%d %s", &cube.count, &cube.color)
  47. if n != 2 || err != nil {
  48. log.Fatalf("Wrong input: %s\n%s", cubes[j], err)
  49. }
  50. set = append(set, cube)
  51. }
  52. current.sets = append(current.sets, set)
  53. }
  54. games = append(games, current)
  55. }
  56. return games
  57. }
  58. type Limits struct {
  59. red, green, blue int
  60. }
  61. func checkLimits(sets [][]Cube, limits Limits) bool {
  62. for i := range sets {
  63. for j := range sets[i] {
  64. switch sets[i][j].color {
  65. case "red":
  66. if sets[i][j].count > limits.red {
  67. return false
  68. }
  69. case "green":
  70. if sets[i][j].count > limits.green {
  71. return false
  72. }
  73. case "blue":
  74. if sets[i][j].count > limits.blue {
  75. return false
  76. }
  77. }
  78. }
  79. }
  80. return true
  81. }
  82. func part1(games []Game) int {
  83. limits := Limits{red: 12, green: 13, blue: 14}
  84. var result int
  85. for i := range games {
  86. if checkLimits(games[i].sets, limits) {
  87. result += games[i].id
  88. }
  89. }
  90. return result
  91. }
  92. func getLimits(sets [][]Cube) Limits {
  93. var limits Limits
  94. for i := range sets {
  95. for j := range sets[i] {
  96. switch sets[i][j].color {
  97. case "red":
  98. if sets[i][j].count > limits.red {
  99. limits.red = sets[i][j].count
  100. }
  101. case "green":
  102. if sets[i][j].count > limits.green {
  103. limits.green = sets[i][j].count
  104. }
  105. case "blue":
  106. if sets[i][j].count > limits.blue {
  107. limits.blue = sets[i][j].count
  108. }
  109. }
  110. }
  111. }
  112. return limits
  113. }
  114. func part2(games []Game) int {
  115. var result int
  116. for i := range games {
  117. limits := getLimits(games[i].sets)
  118. result += limits.red * limits.green * limits.blue
  119. }
  120. return result
  121. }
  122. func main() {
  123. if len(os.Args) < 2 {
  124. log.Fatal("You need to specify a file!")
  125. }
  126. filePath := os.Args[1]
  127. file, err := os.Open(filePath)
  128. if err != nil {
  129. log.Fatalf("Failed to open %s!\n", filePath)
  130. }
  131. games := readInput(file)
  132. fmt.Println("Part1:", part1(games))
  133. fmt.Println("Part2:", part2(games))
  134. }