-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16.kt
154 lines (123 loc) · 3.78 KB
/
Day16.kt
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
package y2021
import general.Day
import general.product
object Day16 : Day() {
override val name = "Packet Decoder"
private class Bits {
private var dataIndex = 0
private var data = INPUT.readText().
replace("0", "0000").
replace("1", "0001").
replace("2", "0010").
replace("3", "0011").
replace("4", "0100").
replace("5", "0101").
replace("6", "0110").
replace("7", "0111").
replace("8", "1000").
replace("9", "1001").
replace("A", "1010").
replace("B", "1011").
replace("C", "1100").
replace("D", "1101").
replace("E", "1110").
replace("F", "1111")
fun bitsRead() = dataIndex
fun readBit() = data[dataIndex++] - '0'
fun readInt(size: Int): Int {
var n = 0
repeat(size) {
n = (n shl 1) + readBit()
}
return n
}
}
override fun a1() {
val bits = Bits()
var versionSum = 0
fun getPacket() {
val version = bits.readInt(3)
val typeID = bits.readInt(3)
versionSum += version
if (typeID == 4) {
var read: Boolean
do {
read = (bits.readBit() == 1)
bits.readInt(4)
} while (read)
return
}
val lengthTypeID = bits.readBit()
if (lengthTypeID == 0) {
val length = bits.readInt(15)
val start = bits.bitsRead()
while (true) {
getPacket()
val end = bits.bitsRead()
if (end - start == length) return
}
}
else {
val amount = bits.readInt(11)
repeat(amount) {
getPacket()
}
}
}
getPacket()
println(versionSum)
}
override fun a2() {
val bits = Bits()
val number = readPacket(bits)
println(number)
}
private fun readPacket(bits: Bits): Long {
val version = bits.readInt(3)
val typeID = bits.readInt(3)
if (typeID == 4)
return readLiteral(bits)
val outputs = readSubPackets(bits)
when (typeID) {
0 -> return outputs.sum()
1 -> return outputs.product()
2 -> return outputs.minOrNull()!!
3 -> return outputs.maxOrNull()!!
5 -> return if (outputs[0] > outputs[1]) 1 else 0
6 -> return if (outputs[0] < outputs[1]) 1 else 0
7 -> return if (outputs[0] == outputs[1]) 1 else 0
}
throw Error("Unknown typeID: $typeID")
}
private fun readLiteral(bits: Bits): Long {
var read: Boolean
var number = 0L
do {
read = (bits.readBit() == 1)
number = (number shl 4) + bits.readInt(4)
} while (read)
return number
}
private fun readSubPackets(bits: Bits): List<Long> {
val outputs = ArrayList<Long>()
val typeLengthID = bits.readBit()
when (typeLengthID) {
0 -> {
val length = bits.readInt(15)
val start = bits.bitsRead()
while (true) {
outputs += readPacket(bits)
val end = bits.bitsRead()
if (end - start == length) break
}
}
1 -> {
val amount = bits.readInt(11)
repeat(amount) {
outputs += readPacket(bits)
}
}
}
return outputs
}
}