-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.go
156 lines (144 loc) · 3.51 KB
/
day16.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
package main
import (
"encoding/hex"
"fmt"
"github.com/dergeberl/aoc/utils"
"os"
"strconv"
"strings"
)
type bits []int
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay16Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay16Part2(string(input)))
}
//SolveDay16Part1 returns the sum of the versions of all packets
func SolveDay16Part1(input string) int {
b := parseInput(input)
versions, _, _ := b.parsePacket(0)
return versions
}
//SolveDay16Part2 returns the calculated number for all packets
func SolveDay16Part2(input string) int {
b := parseInput(input)
_, num, _ := b.parsePacket(0)
return num
}
//parsePacket parses a packet with the given start position and returns the version, calculated number and current position
func (p bits) parsePacket(position int) (int, int, int) {
version := p[position : position+3].toInt()
mode := p[position+3 : position+6].toInt()
if mode == 4 {
position += 6
var num bits
for {
num = append(num, p[position+1:position+5]...)
if p[position] == 0 {
position += 5
break
}
position += 5
}
return version, num.toInt(), position
}
if p[position+6] == 0 {
return p.parsePacketOperatorSize(position, version, mode)
}
if p[position+6] == 1 {
return p.packetOperatorCount(position, version, mode)
}
return version, 0, position
}
//packetOperatorSize parse operator packets with the length type bit site
func (p bits) parsePacketOperatorSize(position int, version int, mode int) (int, int, int) {
var nums []int
numberOfBits := p[position+7 : position+22].toInt()
position += 22
endpos := numberOfBits + position
for position < endpos {
tempVersion := 0
num := 0
tempVersion, num, position = p.parsePacket(position)
nums = append(nums, num)
version += tempVersion
}
return version, calcNumbersByMode(mode, nums), position
}
//packetOperatorCount parse operator packets with the length type packet count
func (p bits) packetOperatorCount(position int, version int, mode int) (int, int, int) {
var nums []int
numberOfPackets := p[position+7 : position+18].toInt()
position += 18
for i := 0; i < numberOfPackets; i++ {
tempVersion := 0
num := 0
tempVersion, num, position = p.parsePacket(position)
nums = append(nums, num)
version += tempVersion
}
return version, calcNumbersByMode(mode, nums), position
}
//calcNumbersByMode returns the calculated number for an operator mode and the given numbers
func calcNumbersByMode(mode int, numbers []int) int {
switch mode {
case 0:
return utils.SumSlice(numbers)
case 1:
return utils.ProductSlice(numbers)
case 2:
return utils.MinimumSlice(numbers)
case 3:
return utils.MaximumSlice(numbers)
case 5:
if len(numbers) != 2 {
panic("wrong input")
}
if numbers[0] > numbers[1] {
return 1
}
return 0
case 6:
if len(numbers) != 2 {
panic("wrong input")
}
if numbers[0] < numbers[1] {
return 1
}
return 0
case 7:
if len(numbers) != 2 {
panic("wrong input")
}
if numbers[0] == numbers[1] {
return 1
}
return 0
}
return 0
}
//toInt returns the int for the bits
func (p bits) toInt() int {
var s int
for i := range p {
s = s << 1
s += p[i]
}
return s
}
func parseInput(input string) bits {
input = strings.TrimSuffix(input, "\n")
inputHex, _ := hex.DecodeString(input)
var inputBits string
for i := range inputHex {
inputBits += fmt.Sprintf("%08v", strconv.FormatInt(int64(inputHex[i]), 2))
}
var b bits
for _, r := range inputBits {
b = append(b, int(r)-48)
}
return b
}