day22.go 3.2 KB

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