day23.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strconv"
  7. )
  8. func processSequence(input string) []int {
  9. var sequence []int
  10. for _, letter := range input {
  11. cup, err := strconv.Atoi(string(letter))
  12. if err != nil {
  13. log.Fatalf("Error processing cup for %s: %s", letter, err)
  14. }
  15. sequence = append(sequence, cup)
  16. }
  17. return sequence
  18. }
  19. func minMax(sequence []int) (int, int) {
  20. max := 0
  21. min := 9
  22. for _, cup := range sequence {
  23. if cup > max {
  24. max = cup
  25. }
  26. if cup < min {
  27. min = cup
  28. }
  29. }
  30. return min, max
  31. }
  32. func inSequence(value int, sequence []int) bool {
  33. for _, item := range sequence {
  34. if item == value {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. func indexOf(value int, sequence []int) int {
  41. for i, item := range sequence {
  42. if item == value {
  43. return i
  44. }
  45. }
  46. return -1
  47. }
  48. func part1(sequence []int, min, max int) {
  49. index := 0
  50. size := len(sequence)
  51. for i := 0; i < 10; i++ {
  52. pickup := sequence[index+1 : index+4]
  53. for j, _ := range sequence {
  54. if j > index && j < index+4 {
  55. sequence[j] = 0
  56. }
  57. }
  58. destination := sequence[index] - 1
  59. fmt.Println(destination)
  60. for {
  61. if !inSequence(destination, pickup) {
  62. break
  63. }
  64. destination--
  65. if destination < min {
  66. _, newDestination := minMax(sequence)
  67. destination = newDestination
  68. break
  69. }
  70. }
  71. fmt.Println(sequence)
  72. fmt.Println(destination)
  73. partialSequence := make([]int, size)
  74. i := index + 1
  75. for {
  76. partialSequence[i] = sequence[i]
  77. if i == index {
  78. i++
  79. break
  80. }
  81. i++
  82. if i > size-1 {
  83. i = 0
  84. }
  85. }
  86. destinationIndex := indexOf(destination, partialSequence)
  87. if destinationIndex < 0 {
  88. log.Fatalf("Wrong destinationIndex: %d", destinationIndex)
  89. }
  90. newSequence := make([]int, size)
  91. i = destinationIndex
  92. for j := destinationIndex; ; j++ {
  93. newSequence[j] = partialSequence[i]
  94. if j == destinationIndex {
  95. j++
  96. for _, cup := range pickup {
  97. newSequence[j] = cup
  98. j++
  99. }
  100. i++
  101. continue
  102. }
  103. j++
  104. i++
  105. }
  106. sequence = newSequence
  107. index++
  108. if index < 0 {
  109. index = 0
  110. }
  111. }
  112. }
  113. func main() {
  114. if len(os.Args) < 2 {
  115. log.Fatal("You need to specify a sequence!")
  116. }
  117. sequence := processSequence(os.Args[1])
  118. min, max := minMax(sequence)
  119. part1(sequence, min, max)
  120. }