-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (111 loc) · 2.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "23", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func remove(cups []int, index int) []int {
return append(cups[0:index], cups[index+1:]...)
}
func insert(cups []int, index int, value int) []int {
//fmt.Println("beggining", cups[0:index])
//fmt.Println("index", index)
//fmt.Println("value", value)
//fmt.Println("end", cups[index:])
ret := make([]int, len(cups)+1)
copy(ret, cups[0:index])
ret[index] = value
copy(ret[index+1:], cups[index:])
return ret
}
func removeByValue(cups []int, val int) []int {
index := getIndex(cups, val)
if index == -1 {
panic(val)
}
return remove(cups, index)
}
func getIndex(cups []int, value int) int {
for index, cup := range cups {
if cup == value {
return index
}
}
return -1
}
func round(cups []int, current int) ([]int, int) {
fmt.Println("cups: ", cups)
currentIndex := getIndex(cups, current)
fmt.Println("current value: ", current, "current index: ", currentIndex)
if currentIndex == -1 {
panic("asd")
}
first, second, third := cups[(currentIndex+1)%len(cups)], cups[(currentIndex+2)%len(cups)], cups[(currentIndex+3)%len(cups)]
fmt.Println("picked up", first, second, third)
cups = removeByValue(removeByValue(removeByValue(cups, first), second), third)
//fmt.Println("removed:", cups)
destination := current - 1
for getIndex(cups, destination) == -1 {
destination -= 1
if destination < 1 {
destination = 9
}
}
destinationIndex := (getIndex(cups, destination) + 1) % len(cups)
fmt.Println("destination value: ", destination, "destination index: ", destinationIndex)
cups = insert(insert(insert(cups, destinationIndex, third), destinationIndex, second), destinationIndex, first)
return cups, cups[(getIndex(cups, current)+1)%len(cups)]
}
func solve(r io.Reader) {
var cups []int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
row := scanner.Text()
for i := 0; i < len(row); i++ {
val, err := strconv.Atoi(row[i : i+1])
if err != nil {
panic(err)
}
cups = append(cups, val)
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
current := cups[0]
//printy(cups, current)
for i := 0; i < 100; i++ {
cups, current = round(cups, current)
fmt.Println()
//printy(cups, current)
}
fmt.Println(cups)
one := getIndex(cups, 1)
curr := one + 1
var ret strings.Builder
for curr != one {
ret.WriteString(fmt.Sprintf("%d", cups[curr]))
curr = (curr + 1) % len(cups)
}
fmt.Println(ret.String())
}
func printy(cups []int, curr int) {
fmt.Println("current: ", curr)
fmt.Println(cups)
fmt.Println()
}
func main() {
//solve(strings.NewReader("389125467"))
solve(input())
}