code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 sequenceKey(sequence [4]int) string {
  54. return fmt.Sprintf("%d_%d_%d_%d", sequence[0], sequence[1], sequence[2], sequence[3])
  55. }
  56. func highestSum(allLastDigits [][]int, iterations int) int {
  57. sums := make(map[string]int)
  58. for _, lastDigits := range allLastDigits {
  59. sequence := [4]int{lastDigits[1] - lastDigits[0], lastDigits[2] - lastDigits[1],
  60. lastDigits[3] - lastDigits[2], lastDigits[4] - lastDigits[3]}
  61. checked := make(map[string]bool)
  62. for i := 5; i < iterations; i++ {
  63. sequence[0], sequence[1], sequence[2] = sequence[1], sequence[2], sequence[3]
  64. sequence[3] = lastDigits[i] - lastDigits[i-1]
  65. if sequence[3] <= 0 {
  66. continue
  67. }
  68. key := sequenceKey(sequence)
  69. if !checked[key] {
  70. sums[key] += lastDigits[i]
  71. checked[key] = true
  72. }
  73. }
  74. }
  75. var highest int
  76. for _, value := range sums {
  77. if value > highest {
  78. highest = value
  79. }
  80. }
  81. return highest
  82. }
  83. func main() {
  84. if len(os.Args) < 2 {
  85. log.Fatal("You need to specify a file!")
  86. }
  87. filePath := os.Args[1]
  88. file, err := os.Open(filePath)
  89. if err != nil {
  90. log.Fatalf("Failed to open %s!\n", filePath)
  91. }
  92. numbers := readInput(file)
  93. part1Result, allLastDigits := part1(numbers, 2000)
  94. fmt.Println("Part1:", part1Result)
  95. fmt.Println("Part2:", highestSum(allLastDigits, 2001))
  96. }