code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type Card struct {
  10. id int
  11. winning []int
  12. owned []int
  13. wins int
  14. count int
  15. }
  16. func readNumbers(part string) []int {
  17. var numbers []int
  18. numberParts := strings.Split(part, " ")
  19. if len(numberParts) == 0 {
  20. log.Fatalf("Can't split numbers: %s", part)
  21. }
  22. for i := range numberParts {
  23. if numberParts[i] == "" {
  24. continue
  25. }
  26. var number int
  27. n, err := fmt.Sscanf(numberParts[i], "%d", &number)
  28. if n != 1 || err != nil {
  29. log.Fatalf("Can't read number: %s\n%s", numberParts[i], err)
  30. }
  31. numbers = append(numbers, number)
  32. }
  33. return numbers
  34. }
  35. func readInput(file *os.File) []Card {
  36. scanner := bufio.NewScanner(file)
  37. var cards []Card
  38. for scanner.Scan() {
  39. line := scanner.Text()
  40. if line == "" {
  41. break
  42. }
  43. current := Card{count: 1}
  44. n, err := fmt.Sscanf(line, "Card %d:", &current.id)
  45. if n != 1 || err != nil {
  46. log.Fatalf("Failed to read card id: %s\n%s", line, err)
  47. }
  48. parts := strings.Split(line, ":")
  49. if len(parts) != 2 {
  50. log.Fatalf("Can't split card: %s", line)
  51. }
  52. numberParts := strings.Split(parts[1], "|")
  53. if len(parts) != 2 {
  54. log.Fatalf("Can't split tables: %s", line)
  55. }
  56. current.winning = append(current.winning, readNumbers(numberParts[0])...)
  57. current.owned = append(current.owned, readNumbers(numberParts[1])...)
  58. cards = append(cards, current)
  59. }
  60. return cards
  61. }
  62. func isInArray(number int, array []int) bool {
  63. for i := range array {
  64. if array[i] == number {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. func pow(x int) int {
  71. result := 1
  72. for i := 0; i < x; i++ {
  73. result *= 2
  74. }
  75. return result
  76. }
  77. func part1(cards []Card) int {
  78. var result int
  79. for i := range cards {
  80. var count int
  81. for j := range cards[i].owned {
  82. if isInArray(cards[i].owned[j], cards[i].winning) {
  83. count++
  84. }
  85. }
  86. if count > 0 {
  87. result += pow(count - 1)
  88. }
  89. cards[i].wins = count
  90. }
  91. return result
  92. }
  93. func part2(cards []Card) int {
  94. var result int
  95. for i := range cards {
  96. result += cards[i].count
  97. if cards[i].wins > 0 {
  98. for j := i + 1; j <= i+cards[i].wins; j++ {
  99. cards[j].count += cards[i].count
  100. }
  101. }
  102. }
  103. return result
  104. }
  105. func main() {
  106. if len(os.Args) < 2 {
  107. log.Fatal("You need to specify a file!")
  108. }
  109. filePath := os.Args[1]
  110. file, err := os.Open(filePath)
  111. if err != nil {
  112. log.Fatalf("Failed to open %s!\n", filePath)
  113. }
  114. cards := readInput(file)
  115. fmt.Println("Part1:", part1(cards))
  116. fmt.Println("Part2:", part2(cards))
  117. }