-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
159 lines (140 loc) · 2.79 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
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"strconv"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input string, moves int) string {
ring := toRing(input)
for i := 0; i < moves; i++ {
ring.round()
}
return ring.String()
}
type Ring struct {
len int
current *Node
nodes map[int]*Node
}
func (r *Ring) round() {
saved := r.current.next
removed := make(map[int]bool)
tmp := saved
for i := 0; i < 3; i++ {
removed[tmp.value] = true
tmp = tmp.next
}
r.current.next = r.current.next.next.next.next
r.current.next.previous = r.current
destinationValue := r.current.value - 1
if destinationValue == 0 {
destinationValue = r.len
}
for removed[destinationValue] {
destinationValue--
if destinationValue == 0 {
destinationValue = r.len
}
}
destination := r.nodes[destinationValue]
next := destination.next
destination.next = saved
saved.previous = destination
saved.next.next.next = next
next.previous = saved.next.next
r.current = r.current.next
}
func (r *Ring) String() string {
s := ""
cur := r.nodes[1].next
for i := 0; i < r.len-1; i++ {
s += strconv.Itoa(cur.value)
cur = cur.next
}
return s
}
func toRing(s string) *Ring {
var values []int
for i := 0; i < len(s); i++ {
r := rune(s[i])
v := aoc.RuneToInt(r)
values = append(values, v)
}
nodes := make(map[int]*Node)
head := &Node{value: values[0]}
nodes[values[0]] = head
previous := head
for i := 1; i < len(values); i++ {
cur := &Node{
value: values[i],
previous: previous,
}
nodes[values[i]] = cur
previous.next = cur
previous = cur
}
previous.next = head
head.previous = previous
return &Ring{
len: len(values),
current: head,
nodes: nodes,
}
}
type Node struct {
value int
previous *Node
next *Node
}
func (n *Node) String() string {
return strconv.Itoa(n.value)
}
func fs2(input string, moves int) string {
ring := toRing2(input)
for i := 0; i < moves; i++ {
ring.round()
}
one := ring.nodes[1]
res := one.next.value * one.next.next.value
return strconv.Itoa(res)
}
func toRing2(s string) *Ring {
var values []int
for i := 0; i < len(s); i++ {
r := rune(s[i])
v := aoc.RuneToInt(r)
values = append(values, v)
}
nodes := make(map[int]*Node)
maxer := aoc.NewMaxer()
maxer.Add(values[0])
head := &Node{value: values[0]}
nodes[values[0]] = head
previous := head
for i := 1; i < len(values); i++ {
maxer.Add(values[i])
cur := &Node{
value: values[i],
previous: previous,
}
nodes[values[i]] = cur
previous.next = cur
previous = cur
}
from := maxer.Get() + 1
for i := from; i <= 1_000_000; i++ {
cur := &Node{
value: i,
previous: previous,
}
nodes[i] = cur
previous.next = cur
previous = cur
}
previous.next = head
head.previous = previous
return &Ring{
len: 1_000_000,
current: head,
nodes: nodes,
}
}