-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (115 loc) · 2.62 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
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
"golang.org/x/exp/slices"
"strconv"
)
type Value struct {
Val int
Items []Value
}
func (v Value) Int() bool {
return v.Items == nil
}
type Packet struct {
val Value
}
func parts(line string) []string {
nesting := 0
var parts []string
var last int
for i := range line {
switch line[i] {
case '[':
nesting += 1
case ']':
nesting -= 1
case ',':
if nesting == 0 {
parts = append(parts, line[last:i])
last = i + 1
}
}
}
parts = append(parts, line[last:])
return parts
}
func parseValue(line string) Value {
if v, err := strconv.Atoi(line); err == nil {
return Value{Val: v}
}
stripped := line[1 : len(line)-1]
p := parts(stripped)
return Value{
Items: lib.Map(lib.Filter(p, func(p string) bool {
return len(p) > 0
}), parseValue),
}
}
func parsePacket(line string) Packet {
return Packet{val: parseValue(line)}
}
func parseChunk(chunk string) []Packet {
return lib.ParseLine(parsePacket)(chunk)
}
func smallerLists(p1, p2 []Value) (bool, bool) {
i := 0
for {
if i >= len(p1) {
return true, len(p1) != len(p2)
}
if i >= len(p2) {
return false, true
}
sm, ok := smaller(p1[i], p2[i])
if ok {
return sm, true
}
i += 1
}
}
func smaller(p1, p2 Value) (bool, bool) {
if p1.Int() && p2.Int() {
return p1.Val < p2.Val, p1.Val != p2.Val
}
if !p1.Int() && !p2.Int() {
return smallerLists(p1.Items, p2.Items)
}
if p1.Int() {
return smallerLists([]Value{p1}, p2.Items)
}
return smallerLists(p1.Items, []Value{p2})
}
func solve(packets [][]Packet) int {
var allPackets []Packet
for _, o := range packets {
for _, i := range o {
allPackets = append(allPackets, i)
}
}
allPackets = append(allPackets, Packet{val: Value{Items: []Value{
{Items: []Value{{Val: 2}}},
}}})
allPackets = append(allPackets, Packet{val: Value{Items: []Value{
{Items: []Value{{Val: 6}}},
}}})
slices.SortFunc(allPackets, func(p1, p2 Packet) bool {
sm, ok := smaller(p1.val, p2.val)
return sm && ok
})
mul := 1
for i, packet := range allPackets {
if len(packet.val.Items) == 1 && len(packet.val.Items[0].Items) == 1 && (packet.val.Items[0].Items[0].Val == 2 || packet.val.Items[0].Items[0].Val == 6) {
mul *= i + 1
}
}
return mul
}
func main() {
solver := lib.Solver[[][]Packet, int]{
ParseF: lib.ParseChunks(parseChunk),
SolveF: solve,
}
solver.Expect("[1,1,3,1,1]\n[1,1,5,1,1]\n\n[[1],[2,3,4]]\n[[1],4]\n\n[9]\n[[8,7,6]]\n\n[[4,4],4,4]\n[[4,4],4,4,4]\n\n[7,7,7,7]\n[7,7,7]\n\n[]\n[3]\n\n[[[]]]\n[[]]\n\n[1,[2,[3,[4,[5,6,7]]]],8,9]\n[1,[2,[3,[4,[5,6,0]]]],8,9]", 140)
solver.Solve()
}