code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type entry struct {
  9. value int
  10. id int
  11. }
  12. func readInput(file *os.File) []entry {
  13. scanner := bufio.NewScanner(file)
  14. var numbers []entry
  15. count := 0
  16. for scanner.Scan() {
  17. line := scanner.Text()
  18. if line == "" {
  19. continue
  20. }
  21. var current entry
  22. n, err := fmt.Sscanf(line, "%d", &current.value)
  23. if n != 1 || err != nil {
  24. log.Fatal("Can't parse:", line, err)
  25. }
  26. if current.value == 0 {
  27. current.id = -1
  28. } else {
  29. current.id = count
  30. }
  31. numbers = append(numbers, current)
  32. count++
  33. }
  34. return numbers
  35. }
  36. func indexOf(numbers []entry, id int) int {
  37. for i := range numbers {
  38. if numbers[i].id == id {
  39. return i
  40. }
  41. }
  42. return -1
  43. }
  44. func establishNewIndex(edge int, current int, value int) int {
  45. delta := current + value
  46. if delta <= 0 {
  47. delta = 0 - delta
  48. rest := delta % edge
  49. return edge - rest
  50. }
  51. return delta % edge
  52. }
  53. func removeAt(numbers []entry, index int) []entry {
  54. return append(numbers[:index], numbers[index+1:]...)
  55. }
  56. func addAt(numbers []entry, value entry, index int) []entry {
  57. if index >= len(numbers) {
  58. return append(numbers, value)
  59. }
  60. var temp []entry
  61. temp = append(temp, numbers[:index]...)
  62. temp = append(temp, value)
  63. return append(temp, numbers[index:]...)
  64. }
  65. func mix(numbers []entry, times int) []entry {
  66. size := len(numbers)
  67. edge := size - 1
  68. mixed := make([]entry, size)
  69. copy(mixed, numbers)
  70. for t := 0; t < times; t++ {
  71. for i := range numbers {
  72. if numbers[i].value == 0 {
  73. continue
  74. }
  75. currentIndex := indexOf(mixed, numbers[i].id)
  76. newIndex := establishNewIndex(edge, currentIndex, numbers[i].value)
  77. mixed = removeAt(mixed, currentIndex)
  78. mixed = addAt(mixed, numbers[i], newIndex)
  79. }
  80. }
  81. return mixed
  82. }
  83. func calculate(mixed []entry) int {
  84. zeroIndex := indexOf(mixed, -1)
  85. result := 0
  86. size := len(mixed)
  87. for i := 1; i < 4; i++ {
  88. index := establishNewIndex(size, zeroIndex, i*1000)
  89. result += mixed[index].value
  90. }
  91. return result
  92. }
  93. func multiply(numbers []entry) []entry {
  94. for i := range numbers {
  95. numbers[i].value = numbers[i].value * 811589153
  96. }
  97. return numbers
  98. }
  99. func main() {
  100. if len(os.Args) < 2 {
  101. log.Fatal("You need to specify a file!")
  102. }
  103. filePath := os.Args[1]
  104. file, err := os.Open(filePath)
  105. if err != nil {
  106. log.Fatalf("Failed to open %s!\n", filePath)
  107. }
  108. numbers := readInput(file)
  109. mixed1 := mix(numbers, 1)
  110. fmt.Println("Part1:", calculate(mixed1))
  111. multiplied := multiply(numbers)
  112. mixed10 := mix(multiplied, 10)
  113. fmt.Println("Part2:", calculate(mixed10))
  114. }