-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (154 loc) · 3.75 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
// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
"strings"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/must"
)
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
buf := must.ReadFile(filename)
bits := strings.Join(Map([]rune(buf), func(r rune) string { return hex2dec[r] }), "")
insts := processBits(bits)
v := insts[0].eval()
printf("Solution: %v\n", v)
}
func processBits(bits string) []*instT {
var insts []*instT
for len(bits) > 7 {
bits, insts = processNext(bits, insts)
}
return insts
}
func processNext(bits string, insts []*instT) (string, []*instT) {
inst := &instT{
version: dec2hex["0"+bits[0:3]],
id: dec2hex["0"+bits[3:6]],
}
switch {
case inst.id == 4:
return processLiteral(bits[6:], insts, inst)
case bits[6] == '0':
return processOpLenType0(bits[7:], insts, inst)
default:
return processOpLenType1(bits[7:], insts, inst)
}
}
func processLiteral(bits string, insts []*instT, inst *instT) (string, []*instT) {
var value string
for {
value += bits[1:5]
if bits[0] == '0' {
bits = bits[5:]
break
}
bits = bits[5:]
}
inst.literal = must.ParseInt(value, 2, 64)
return bits, append(insts, inst)
}
func processOpLenType0(bits string, insts []*instT, inst *instT) (string, []*instT) {
totalBitLen := must.ParseInt(bits[0:15], 2, 64)
bits = bits[15:]
inst.subPackets = processBits(bits[0:totalBitLen])
return bits[totalBitLen:], append(insts, inst)
}
func processOpLenType1(bits string, insts []*instT, inst *instT) (string, []*instT) {
n := must.ParseInt(bits[0:11], 2, 64)
bits = bits[11:]
for i := 0; i < n; i++ {
bits, inst.subPackets = processNext(bits, inst.subPackets)
}
return bits, append(insts, inst)
}
type instT struct {
version int
id int
// id=4
literal int
subPackets []*instT
}
func (in *instT) eval() int {
switch in.id {
case 0: // sum
v := Reduce(in.subPackets, 0, func(in *instT, acc int) int { return acc + in.eval() })
in.id, in.literal = 4, v
return v
case 1: // product
v := Reduce(in.subPackets, 1, func(in *instT, acc int) int { return acc * in.eval() })
in.id, in.literal = 4, v
return v
case 2: // minimum
v := ReduceWithIndex(in.subPackets, 0, func(index int, in *instT, acc int) int {
v := in.eval()
if index == 0 || v < acc {
acc = v
}
return acc
})
in.id, in.literal = 4, v
return v
case 3: // maximum
v := ReduceWithIndex(in.subPackets, 0, func(index int, in *instT, acc int) int {
v := in.eval()
if index == 0 || v > acc {
acc = v
}
return acc
})
in.id, in.literal = 4, v
return v
case 4:
return in.literal
case 5: // greater than
a := in.subPackets[0].eval()
b := in.subPackets[1].eval()
v := 0
if a > b {
v = 1
}
in.id, in.literal = 4, v
return v
case 6: // less than
a := in.subPackets[0].eval()
b := in.subPackets[1].eval()
v := 0
if a < b {
v = 1
}
in.id, in.literal = 4, v
return v
case 7: // equal to
a := in.subPackets[0].eval()
b := in.subPackets[1].eval()
v := 0
if a == b {
v = 1
}
in.id, in.literal = 4, v
return v
}
log.Fatalf("bad packet id=%v", in.id)
return 0
}
var hex2dec = map[rune]string{
'0': "0000", '1': "0001", '2': "0010", '3': "0011",
'4': "0100", '5': "0101", '6': "0110", '7': "0111",
'8': "1000", '9': "1001", 'A': "1010", 'B': "1011",
'C': "1100", 'D': "1101", 'E': "1110", 'F': "1111",
}
var dec2hex = map[string]int{
"0000": 0, "0001": 1, "0010": 2, "0011": 3,
"0100": 4, "0101": 5, "0110": 6, "0111": 7,
"1000": 8, "1001": 9, "1010": 10, "1011": 11,
"1100": 12, "1101": 13, "1110": 14, "1111": 15,
}