day22.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. deck1Length := len(deck1)
  73. deck2Length := len(deck2)
  74. for _, round := range previousRounds {
  75. if deck1Length != len(round.deck1) || deck2Length != len(round.deck2) {
  76. continue
  77. }
  78. if checkDeck(deck1, round.deck1) && checkDeck(deck2, round.deck2) {
  79. return true
  80. }
  81. }
  82. return false
  83. }
  84. type previous struct {
  85. deck1 []int
  86. deck2 []int
  87. }
  88. func play2(decks [2][]int) (int, []int) {
  89. var previousRounds []previous
  90. for {
  91. if len(decks[0]) == 0 || len(decks[1]) == 0 {
  92. break
  93. }
  94. if len(previousRounds) > 0 {
  95. if checkDecks(decks[0], decks[1], previousRounds) {
  96. return 0, decks[0]
  97. }
  98. }
  99. previousRounds = append(previousRounds, previous{deck1: decks[0], deck2: decks[1]})
  100. player1Hand := decks[0][0]
  101. decks[0] = decks[0][1:len(decks[0])]
  102. player2Hand := decks[1][0]
  103. decks[1] = decks[1][1:len(decks[1])]
  104. if len(decks[0]) >= player1Hand && len(decks[1]) >= player2Hand {
  105. var newDecks [2][]int
  106. for i, card := range decks[0] {
  107. if i >= player1Hand {
  108. break
  109. }
  110. newDecks[0] = append(newDecks[0], card)
  111. }
  112. for i, card := range decks[1] {
  113. if i >= player2Hand {
  114. break
  115. }
  116. newDecks[1] = append(newDecks[1], card)
  117. }
  118. winner, _ := play2(newDecks)
  119. if winner == 0 {
  120. decks[0] = append(decks[0], player1Hand)
  121. decks[0] = append(decks[0], player2Hand)
  122. } else {
  123. decks[1] = append(decks[1], player2Hand)
  124. decks[1] = append(decks[1], player1Hand)
  125. }
  126. } else {
  127. if player1Hand > player2Hand {
  128. decks[0] = append(decks[0], player1Hand)
  129. decks[0] = append(decks[0], player2Hand)
  130. } else {
  131. decks[1] = append(decks[1], player2Hand)
  132. decks[1] = append(decks[1], player1Hand)
  133. }
  134. }
  135. }
  136. if len(decks[0]) == 0 {
  137. return 1, decks[1]
  138. }
  139. return 0, decks[0]
  140. }
  141. func calculate(deck []int) int {
  142. result := 0
  143. multiplyBy := 1
  144. index := len(deck) - 1
  145. for ; index >= 0; index-- {
  146. result += deck[index] * multiplyBy
  147. multiplyBy++
  148. }
  149. return result
  150. }
  151. func main() {
  152. if len(os.Args) < 2 {
  153. log.Fatal("You need to specify a file!")
  154. }
  155. filePath := os.Args[1]
  156. file, err := os.Open(filePath)
  157. if err != nil {
  158. log.Fatalf("Failed to open %s!\n", filePath)
  159. }
  160. decks := readFile(file)
  161. if err := file.Close(); err != nil {
  162. log.Fatalf("Failed to close file: %s", err)
  163. }
  164. winningDeck1 := play1(decks)
  165. fmt.Println("Part1:", calculate(winningDeck1))
  166. _, winningDeck2 := play2(decks)
  167. fmt.Println("Part2:", calculate(winningDeck2))
  168. }