-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (143 loc) · 3.45 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
package main
import (
"bytes"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "16", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func parse(r io.Reader) string {
rawPacket, err := io.ReadAll(r)
if err != nil {
panic(err)
}
var packetBuilder strings.Builder
for _, char := range bytes.TrimSpace(rawPacket) {
base10Value, err := strconv.ParseInt(string(char), 16, 8)
if err != nil {
panic(err)
}
base2Value := strconv.FormatInt(base10Value, 2)
for i := 0; i < 4-len(base2Value); i++ {
packetBuilder.WriteByte('0')
}
for _, bit := range base2Value {
packetBuilder.WriteRune(bit)
}
}
return packetBuilder.String()
}
func solve(r io.Reader) {
packetBits := parse(r)
packet := parsePacket(&packetBits)
if len(packetBits) != 0 && toInt(packetBits) != 0 {
panic("bits remain after parsing outermost packet")
}
fmt.Println(packet.VersionSum())
}
type Packet interface {
VersionSum() int
}
type versioned struct {
version int
}
type literalPacket struct {
versioned
}
func (v literalPacket) VersionSum() int {
return v.version
}
type opPacket struct {
versioned
subPackets []Packet
}
func (o opPacket) VersionSum() int {
sum := o.version
for _, subPacket := range o.subPackets {
sum += subPacket.VersionSum()
}
return sum
}
// consumeInt is like consume, but it converts the result to an int
func consumeInt(packetBits *string, numBits int) int {
return toInt(consume(packetBits, numBits))
}
// consume returns numBits bits from packetBits, moving packetBits forward by that many bits
func consume(packetBits *string, numBits int) string {
value := (*packetBits)[:numBits]
*packetBits = (*packetBits)[numBits:]
return value
}
// parsePacket parses the packet in base2 represented by packetBits, returning the packet and modifying packetBits
// to be the start of the next packet
func parsePacket(packetBits *string) Packet {
version := consumeInt(packetBits, 3)
typeId := consumeInt(packetBits, 3)
switch typeId {
case 4:
for {
stop := consume(packetBits, 1) == "0"
_ = consume(packetBits, 4) // ignore value, it's not used for part 1
if stop {
break
}
}
return literalPacket{
versioned: versioned{version: version},
}
default:
lengthTypeId := consume(packetBits, 1)
var continueFunc func() bool
switch lengthTypeId {
case "0":
packetsSize := consumeInt(packetBits, 15)
startSize := len(*packetBits)
continueFunc = func() bool {
return startSize-len(*packetBits) < packetsSize
}
case "1":
numPackets := consumeInt(packetBits, 11)
i := 0
continueFunc = func() bool {
i++
return i <= numPackets
}
default:
panic("unknown length type id")
}
var subPackets []Packet
for continueFunc() {
subPackets = append(subPackets, parsePacket(packetBits))
}
return opPacket{
versioned: versioned{version: version},
subPackets: subPackets,
}
}
}
func toInt(base2Value string) int {
intValue, err := strconv.ParseInt(base2Value, 2, 64)
if err != nil {
panic(err)
}
return int(intValue)
}
func main() {
solve(strings.NewReader("D2FE28"))
solve(strings.NewReader("38006F45291200"))
solve(strings.NewReader("EE00D40C823060"))
solve(strings.NewReader("8A004A801A8002F478"))
solve(strings.NewReader("620080001611562C8802118E34"))
solve(strings.NewReader("C0015000016115A2E0802F182340"))
solve(strings.NewReader("A0016C880162017C3686B18A3D4780"))
solve(input())
}