code.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. )
  9. func readInput(file *os.File) []int {
  10. scanner := bufio.NewScanner(file)
  11. var numbers []int
  12. for scanner.Scan() {
  13. line := scanner.Text()
  14. if line == "" {
  15. break
  16. }
  17. number, err := strconv.Atoi(line)
  18. if err != nil {
  19. log.Fatalf("Can't parse a number %s: %s", line, err)
  20. }
  21. numbers = append(numbers, number)
  22. }
  23. return numbers
  24. }
  25. func calculate(number int) int {
  26. a := number * 64
  27. number = (number ^ a) % 16777216
  28. b := number / 32
  29. number = (number ^ b) % 16777216
  30. c := number * 2048
  31. number = (number ^ c) % 16777216
  32. return number
  33. }
  34. func generateNumber(number int, iterations int) (int, []int) {
  35. lastDigits := make([]int, iterations+1)
  36. lastDigits[0] = number % 10
  37. for i := 0; i < iterations; i++ {
  38. number = calculate(number)
  39. lastDigits[i+1] = number % 10
  40. }
  41. return number, lastDigits
  42. }
  43. func part1(numbers []int, iterations int) (int, [][]int) {
  44. var result int
  45. var allLastDigits [][]int
  46. for _, number := range numbers {
  47. newNumber, lastDigits := generateNumber(number, iterations)
  48. result += newNumber
  49. allLastDigits = append(allLastDigits, lastDigits)
  50. }
  51. return result, allLastDigits
  52. }
  53. func isDesiredSequence(sequence, desiredSequence []int) bool {
  54. if len(sequence) != len(desiredSequence) {
  55. return false
  56. }
  57. for i := range sequence {
  58. if sequence[i] != desiredSequence[i] {
  59. return false
  60. }
  61. }
  62. return true
  63. }
  64. func sequenceKey(sequence []int) string {
  65. if len(sequence) != 4 {
  66. return ""
  67. }
  68. return fmt.Sprintf("%d_%d_%d_%d", sequence[0], sequence[1], sequence[2], sequence[3])
  69. }
  70. func highestSum(allLastDigits [][]int, iterations int) int {
  71. var allSequences [][]int
  72. sums := make(map[string]int)
  73. for _, lastDigits := range allLastDigits {
  74. var sequence []int
  75. checked := make(map[string]bool)
  76. for i := 1; i < iterations; i++ {
  77. sequence = append(sequence, lastDigits[i]-lastDigits[i-1])
  78. if len(sequence) > 3 {
  79. lastFour := sequence[len(sequence)-4:]
  80. if lastFour[3] <= 0 {
  81. continue
  82. }
  83. key := sequenceKey(lastFour)
  84. if !checked[key] {
  85. sums[key] += lastDigits[i]
  86. checked[key] = true
  87. }
  88. }
  89. }
  90. allSequences = append(allSequences, sequence)
  91. }
  92. var highest int
  93. for _, value := range sums {
  94. if value > highest {
  95. highest = value
  96. }
  97. }
  98. return highest
  99. }
  100. func main() {
  101. if len(os.Args) < 2 {
  102. log.Fatal("You need to specify a file!")
  103. }
  104. filePath := os.Args[1]
  105. file, err := os.Open(filePath)
  106. if err != nil {
  107. log.Fatalf("Failed to open %s!\n", filePath)
  108. }
  109. numbers := readInput(file)
  110. part1Result, allLastDigits := part1(numbers, 2000)
  111. fmt.Println("Part1:", part1Result)
  112. fmt.Println("Part2:", highestSum(allLastDigits, 2001))
  113. }