code.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func readInput(file *os.File) []int {
  9. scanner := bufio.NewScanner(file)
  10. var numbers []int
  11. for scanner.Scan() {
  12. line := scanner.Text()
  13. if line == "" {
  14. continue
  15. }
  16. var current int
  17. n, err := fmt.Sscanf(line, "%d", &current)
  18. if n != 1 || err != nil {
  19. log.Fatal("Can't parse:", line, err)
  20. }
  21. numbers = append(numbers, current)
  22. }
  23. return numbers
  24. }
  25. func indexOf(numbers []int, number int) int {
  26. for i := range numbers {
  27. if numbers[i] == number {
  28. return i
  29. }
  30. }
  31. return -1
  32. }
  33. func establishNewIndex(size int, current int, value int) int {
  34. delta := current + value
  35. if delta <= 0 {
  36. delta = 0 - delta
  37. rest := delta % size
  38. return size - rest
  39. }
  40. if delta >= size {
  41. return delta % size
  42. }
  43. return delta
  44. }
  45. func removeAt(numbers []int, index int) []int {
  46. return append(numbers[:index], numbers[index+1:]...)
  47. }
  48. func addAt(numbers []int, value int, index int) []int {
  49. if index >= len(numbers) {
  50. return append(numbers, value)
  51. }
  52. var temp []int
  53. temp = append(temp, numbers[:index]...)
  54. temp = append(temp, value)
  55. return append(temp, numbers[index:]...)
  56. }
  57. func mix(numbers []int) []int {
  58. size := len(numbers)
  59. edge := size - 1
  60. mixed := make([]int, size)
  61. copy(mixed, numbers)
  62. for i := range numbers {
  63. if numbers[i] == 0 {
  64. continue
  65. }
  66. currentIndex := indexOf(mixed, numbers[i])
  67. newIndex := establishNewIndex(edge, currentIndex, numbers[i])
  68. mixed = removeAt(mixed, currentIndex)
  69. mixed = addAt(mixed, numbers[i], newIndex)
  70. }
  71. return mixed
  72. }
  73. func part1(mixed []int) int {
  74. zeroIndex := indexOf(mixed, 0)
  75. result := 0
  76. size := len(mixed)
  77. for i := 1; i < 4; i++ {
  78. index := (zeroIndex + i*1000) % size
  79. result += mixed[index]
  80. }
  81. return result
  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. mixed := mix(numbers)
  94. fmt.Println("Part1:", part1(mixed))
  95. }