Skip to content

Commit 5a04889

Browse files
committed
20th day
1 parent 7ffce2a commit 5a04889

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

day20/day20.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
. "majcn.si/advent-of-code-2022/util"
8+
)
9+
10+
type DataType []int
11+
12+
func parseData(data string) DataType {
13+
dataSplit := strings.Split(data, "\n")
14+
15+
result := make([]int, len(dataSplit))
16+
for i, line := range dataSplit {
17+
result[i] = ParseInt(line)
18+
}
19+
20+
return result
21+
}
22+
23+
func solvePartX(data DataType, nrMixes int) (rc int) {
24+
myList := NewCircualList(data)
25+
myListAsSlice := myList.AsSlice()
26+
27+
for n := 0; n < nrMixes; n++ {
28+
for i := range myListAsSlice {
29+
node := myListAsSlice[i]
30+
nodeValue := node.Value
31+
32+
switch {
33+
case nodeValue > 0:
34+
myNextNode := node.Next
35+
36+
myList.RemoveNode(node)
37+
node = nil
38+
39+
repeat := (nodeValue % (len(myListAsSlice) - 1)) - 1
40+
for c := 0; c < repeat; c++ {
41+
myNextNode = myNextNode.Next
42+
}
43+
44+
myListAsSlice[i] = myList.AddAfter(myNextNode, nodeValue)
45+
case nodeValue < 0:
46+
myPrevNode := node.Prev
47+
48+
myList.RemoveNode(node)
49+
node = nil
50+
51+
repeat := ((-1 * nodeValue) % (len(myListAsSlice) - 1)) - 1
52+
for c := 0; c < repeat; c++ {
53+
myPrevNode = myPrevNode.Prev
54+
}
55+
56+
myListAsSlice[i] = myList.AddBefore(myPrevNode, nodeValue)
57+
}
58+
}
59+
}
60+
61+
node := myList.FindNode(0)
62+
for i := 1; i <= 3000; i++ {
63+
node = node.Next
64+
if i%1000 == 0 {
65+
rc += node.Value
66+
}
67+
}
68+
69+
return
70+
}
71+
72+
func solvePart1(data DataType) (rc int) {
73+
return solvePartX(data, 1)
74+
}
75+
76+
func solvePart2(data DataType) (rc int) {
77+
modifiedData := make(DataType, len(data))
78+
for i, v := range data {
79+
modifiedData[i] = v * 811589153
80+
}
81+
82+
return solvePartX(modifiedData, 10)
83+
}
84+
85+
func main() {
86+
data := parseData(FetchInputData(20))
87+
fmt.Println(solvePart1(data))
88+
fmt.Println(solvePart2(data))
89+
}

day20/day20_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
. "majcn.si/advent-of-code-2022/util"
8+
)
9+
10+
func ExpectedPart1() int {
11+
return 3
12+
}
13+
14+
func ExpectedPart2() int {
15+
return 1623178306
16+
}
17+
18+
var data DataType
19+
20+
func init() {
21+
input, _ := os.ReadFile("../examples/20.txt")
22+
data = parseData(string(input))
23+
}
24+
25+
func TestPart1(t *testing.T) {
26+
AssertTestPartX(t, ExpectedPart1(), solvePart1(data))
27+
}
28+
29+
func TestPart2(t *testing.T) {
30+
AssertTestPartX(t, ExpectedPart2(), solvePart2(data))
31+
}

examples/20.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1
2+
2
3+
-3
4+
3
5+
-2
6+
0
7+
4

0 commit comments

Comments
 (0)