-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
243 lines (202 loc) · 4.12 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/devries/advent_of_code_2021/utils"
"github.com/spf13/pflag"
)
func main() {
pflag.Parse()
f, err := os.Open("../inputs/day18.txt")
utils.Check(err, "error opening input file")
defer f.Close()
r := solve(f, utils.Verbose)
fmt.Println(r)
}
func solve(r io.Reader, verbose bool) int {
lines := utils.ReadLines(r)
var el Element
for i, line := range lines {
if i == 0 {
el, _ = parseSnailNumber([]rune(line), 0)
} else {
next, _ := parseSnailNumber([]rune(line), 0)
el = add(el, next)
}
if verbose {
fmt.Println(el)
}
}
return el.Value()
}
type Element interface {
Value() int
String() string
}
type Number struct {
val int
}
func (n *Number) Value() int {
return n.val
}
func (n *Number) String() string {
return fmt.Sprintf("%d", n.val)
}
type Pair struct {
left Element
right Element
}
func (p *Pair) Value() int {
return 3*p.left.Value() + 2*p.right.Value()
}
func (p *Pair) String() string {
return fmt.Sprintf("[%s,%s]", p.left.String(), p.right.String())
}
func parseSnailNumber(line []rune, start int) (Element, int) {
switch line[start] {
case '[':
// Pair
var p Pair
pos := start + 1
p.left, pos = parseSnailNumber(line, pos)
if line[pos] != ',' {
panic(fmt.Errorf("Expected a comma for second part of pair, got %c", line[pos]))
}
pos++
p.right, pos = parseSnailNumber(line, pos)
lastrune := line[pos]
if lastrune != ']' {
panic(fmt.Errorf("Pair does not end with closing bracket"))
}
return &p, pos + 1
default:
// Number
for i := start + 1; true; i++ {
if line[i] == ',' || line[i] == ']' {
v, err := strconv.Atoi(string(line[start:i]))
utils.Check(err, fmt.Sprintf("Unable to parse the number %s", string(line[start:i])))
return &Number{v}, i
}
}
}
return nil, 0
}
type ChainLink struct {
e Element
depth int
parent *Pair
parentSide string
}
type Chain []ChainLink
func (c Chain) String() string {
var sb strings.Builder
sb.WriteRune('[')
for i, cl := range c {
if i != 0 {
sb.WriteRune(',')
}
sb.WriteString(cl.e.String())
}
sb.WriteRune(']')
return sb.String()
}
// Return all elements in order up to depth 4
func scanElements(e Element, depth int, parent *Pair, side string) Chain {
switch v := e.(type) {
case *Pair:
if depth == 4 {
return Chain{{v, 4, parent, side}}
}
ret := make(Chain, 0)
leftElements := scanElements(v.left, depth+1, v, "left")
ret = append(ret, leftElements...)
rightElements := scanElements(v.right, depth+1, v, "right")
ret = append(ret, rightElements...)
return ret
case *Number:
cl := ChainLink{v, depth, parent, side}
return Chain{cl}
}
return Chain{}
}
// Explode first value, return true if explosion happens
func explode(e Element) bool {
chain := scanElements(e, 0, nil, "")
for i, c := range chain {
p, ok := c.e.(*Pair)
if ok {
leftNumber := p.left.Value()
rightNumber := p.right.Value()
switch c.parentSide {
case "left":
c.parent.left = &Number{0}
case "right":
c.parent.right = &Number{0}
}
if i > 0 {
prev := chain[i-1]
switch v := prev.e.(type) {
case *Number:
v.val += leftNumber
case *Pair:
n := v.right.(*Number)
n.val += leftNumber
}
}
if i < len(chain)-1 {
next := chain[i+1]
switch v := next.e.(type) {
case *Number:
v.val += rightNumber
case *Pair:
n := v.left.(*Number)
n.val += rightNumber
}
}
return true
}
}
return false
}
func split(e Element) bool {
chain := scanElements(e, 0, nil, "")
for _, c := range chain {
n, ok := c.e.(*Number)
if ok {
if n.val > 9 {
a := n.val / 2
b := a
if n.val%2 == 1 {
b += 1
}
pair := &Pair{&Number{a}, &Number{b}}
switch c.parentSide {
case "left":
c.parent.left = pair
case "right":
c.parent.right = pair
}
return true
}
}
}
return false
}
func add(a, b Element) Element {
ret := &Pair{a, b}
for {
explodeTest := explode(ret)
if explodeTest {
continue
}
splitTest := split(ret)
if splitTest {
continue
}
break
}
return ret
}