-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (85 loc) · 1.77 KB
/
main.go
File metadata and controls
112 lines (85 loc) · 1.77 KB
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
package main
import (
"fmt"
"github.com/danvolchek/AdventOfCode/lib"
)
type linkedList[T any] struct {
item T
prev *linkedList[T]
next *linkedList[T]
}
func solve(lines []int) int {
start := &linkedList[int]{
item: lines[0] * 811589153,
}
moveOrder := make([]*linkedList[int], len(lines))
var zero *linkedList[int]
moveOrder[0] = start
curr := start
for i := 1; i < len(lines); i++ {
next := &linkedList[int]{
item: lines[i] * 811589153,
prev: curr,
}
curr.next = next
curr = next
if lines[i] == 0 {
zero = next
}
moveOrder[i] = curr
}
curr.next = start
start.prev = curr
for jj := 0; jj < 10; jj++ {
for _, curr = range moveOrder {
targNum := curr.item
if targNum == 0 {
continue
}
remove(curr)
target := curr
if targNum < 0 {
for j := 0; j < ((lib.Abs(targNum) + 1) % (len(lines) - 1)); j++ {
target = target.prev
}
} else {
for j := 0; j < (targNum % (len(lines) - 1)); j++ {
target = target.next
}
}
if target == curr {
panic("omg")
}
insertAfter(curr, target)
}
}
return get(zero, 1000) + get(zero, 2000) + get(zero, 3000)
}
func get[T any](start *linkedList[T], index int) T {
for i := 0; i < index; i++ {
start = start.next
}
fmt.Println(start.item)
return start.item
}
func remove[T any](item *linkedList[T]) {
item.prev.next = item.next
item.next.prev = item.prev
//item.prev = nil
//item.next = nil
}
func insertAfter[T any](item, target *linkedList[T]) {
targNext := target.next
targNext.prev = item
target.next = item
item.next = targNext
item.prev = target
}
func main() {
solver := lib.Solver[[]int, int]{
ParseF: lib.ParseLine(lib.Atoi),
SolveF: solve,
}
solver.Expect("1\n2\n-3\n3\n-2\n0\n4", 1623178306)
solver.Verify(4275451658004)
}