-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (104 loc) · 2.15 KB
/
main.go
File metadata and controls
137 lines (104 loc) · 2.15 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
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
package main
import (
"fmt"
"github.com/danvolchek/AdventOfCode/lib"
"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 {
sm := lib.Map(packets, func(pair []Packet) bool {
sm, ok := smaller(pair[0].val, pair[1].val)
return sm && ok
})
sum := 0
for i, item := range sm {
if item {
fmt.Println(i + 1)
sum += i + 1
}
}
return sum
}
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]", 13)
solver.Solve()
}