day22.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. )
  9. func readFile(file *os.File) [2][]int {
  10. var decks [2][]int
  11. index := 0
  12. changed := false
  13. scanner := bufio.NewScanner(file)
  14. for scanner.Scan() {
  15. line := scanner.Text()
  16. if line == "" {
  17. if changed {
  18. break
  19. }
  20. continue
  21. }
  22. if line == "Player 1:" {
  23. continue
  24. }
  25. if line == "Player 2:" {
  26. index++
  27. changed = true
  28. continue
  29. }
  30. card, err := strconv.Atoi(line)
  31. if err != nil {
  32. log.Fatalf("Error processing card for %s: %s", line, err)
  33. }
  34. decks[index] = append(decks[index], card)
  35. }
  36. if err := scanner.Err(); err != nil {
  37. log.Fatalf("Scanner error: %s", err)
  38. }
  39. return decks
  40. }
  41. func play1(decks [2][]int) []int {
  42. for {
  43. if len(decks[0]) == 0 || len(decks[1]) == 0 {
  44. break
  45. }
  46. player1Hand := decks[0][0]
  47. decks[0] = decks[0][1:len(decks[0])]
  48. player2Hand := decks[1][0]
  49. decks[1] = decks[1][1:len(decks[1])]
  50. if player1Hand > player2Hand {
  51. decks[0] = append(decks[0], player1Hand)
  52. decks[0] = append(decks[0], player2Hand)
  53. } else {
  54. decks[1] = append(decks[1], player2Hand)
  55. decks[1] = append(decks[1], player1Hand)
  56. }
  57. }
  58. if len(decks[0]) == 0 {
  59. return decks[1]
  60. }
  61. return decks[0]
  62. }
  63. func checkDeck(deck []int, deckFromRound []int) bool {
  64. for i, card := range deck {
  65. if card != deckFromRound[i] {
  66. return false
  67. }
  68. }
  69. return true
  70. }
  71. func checkDecks(deck1, deck2 []int, previousRounds []previous) bool {
  72. for _, round := range previousRounds {
  73. if len(deck1) != len(round.deck1) || len(deck2) != len(round.deck2) {
  74. continue
  75. }
  76. if checkDeck(deck1, round.deck1) && checkDeck(deck2, round.deck2) {
  77. return true
  78. }
  79. }
  80. return false
  81. }
  82. type previous struct {
  83. deck1 []int
  84. deck2 []int
  85. }
  86. func play2(decks [2][]int) (int, []int) {
  87. var previousRounds []previous
  88. for {
  89. if len(decks[0]) == 0 || len(decks[1]) == 0 {
  90. break
  91. }
  92. if len(previousRounds) > 0 {
  93. if checkDecks(decks[0], decks[1], previousRounds) {
  94. return 0, decks[0]
  95. }
  96. }
  97. previousRounds = append(previousRounds, previous{deck1: decks[0], deck2: decks[1]})
  98. player1Hand := decks[0][0]
  99. decks[0] = decks[0][1:len(decks[0])]
  100. player2Hand := decks[1][0]
  101. decks[1] = decks[1][1:len(decks[1])]
  102. if len(decks[0]) >= player1Hand && len(decks[1]) >= player2Hand {
  103. var newDecks [2][]int
  104. for i, card := range decks[0] {
  105. if i >= player1Hand {
  106. break
  107. }
  108. newDecks[0] = append(newDecks[0], card)
  109. }
  110. for i, card := range decks[1] {
  111. if i >= player2Hand {
  112. break
  113. }
  114. newDecks[1] = append(newDecks[1], card)
  115. }
  116. winner, _ := play2(newDecks)
  117. if winner == 0 {
  118. decks[0] = append(decks[0], player1Hand)
  119. decks[0] = append(decks[0], player2Hand)
  120. } else {
  121. decks[1] = append(decks[1], player2Hand)
  122. decks[1] = append(decks[1], player1Hand)
  123. }
  124. } else {
  125. if player1Hand > player2Hand {
  126. decks[0] = append(decks[0], player1Hand)
  127. decks[0] = append(decks[0], player2Hand)
  128. } else {
  129. decks[1] = append(decks[1], player2Hand)
  130. decks[1] = append(decks[1], player1Hand)
  131. }
  132. }
  133. }
  134. if len(decks[0]) == 0 {
  135. return 1, decks[1]
  136. }
  137. return 0, decks[0]
  138. }
  139. func calculate(deck []int) int {
  140. result := 0
  141. multiplyBy := 1
  142. index := len(deck) - 1
  143. for ; index >= 0; index-- {
  144. result += deck[index] * multiplyBy
  145. multiplyBy++
  146. }
  147. return result
  148. }
  149. func main() {
  150. if len(os.Args) < 2 {
  151. log.Fatal("You need to specify a file!")
  152. }
  153. filePath := os.Args[1]
  154. file, err := os.Open(filePath)
  155. if err != nil {
  156. log.Fatalf("Failed to open %s!\n", filePath)
  157. }
  158. decks := readFile(file)
  159. if err := file.Close(); err != nil {
  160. log.Fatalf("Failed to close file: %s", err)
  161. }
  162. winningDeck1 := play1(decks)
  163. fmt.Println("Part1:", calculate(winningDeck1))
  164. _, winningDeck2 := play2(decks)
  165. fmt.Println("Part2:", calculate(winningDeck2))
  166. }