2021-12-20 22:52:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVersionParse(t *testing.T) {
|
2021-12-20 23:45:35 +00:00
|
|
|
tests := map[int][]uint8{
|
2021-12-20 22:52:18 +00:00
|
|
|
0: {0, 0, 0},
|
|
|
|
1: {0, 0, 1},
|
|
|
|
2: {0, 1, 0},
|
|
|
|
3: {0, 1, 1},
|
|
|
|
4: {1, 0, 0},
|
|
|
|
5: {1, 0, 1},
|
|
|
|
6: {1, 1, 0},
|
|
|
|
7: {1, 1, 1},
|
|
|
|
}
|
|
|
|
for result, input := range tests {
|
|
|
|
if result != getPacketVersion(input) {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if result != getPacketType(input) {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePayload(t *testing.T) {
|
|
|
|
if n, q := parsePayload([]uint8{0, 1, 0, 1, 0}); n != 10 || q != 5 {
|
|
|
|
t.Log(n, q)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if n, q := parsePayload([]uint8{1, 0, 0, 0, 1, 0, 0, 1, 0, 0}); n != 20 || q != 10 {
|
|
|
|
t.Log(n, q)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCases(t *testing.T) {
|
|
|
|
inputs := map[string]int{
|
|
|
|
"D2FE28": 6,
|
|
|
|
"38006F45291200": 9,
|
|
|
|
"EE00D40C823060": 14,
|
|
|
|
"8A004A801A8002F478": 16,
|
|
|
|
"620080001611562C8802118E34": 12,
|
|
|
|
"C0015000016115A2E0802F182340": 23,
|
|
|
|
"A0016C880162017C3686B18A3D4780": 31,
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, r := range inputs {
|
|
|
|
stream := []uint8{}
|
|
|
|
for _, hex := range c {
|
|
|
|
stream = append(stream, hexMap[hex]...)
|
|
|
|
}
|
|
|
|
_, parsedPacket := parsePackets(stream)
|
|
|
|
if r != countVersions(parsedPacket) {
|
|
|
|
t.Log(c, countVersions(parsedPacket))
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 00:02:26 +00:00
|
|
|
|
|
|
|
func TestSolving(t *testing.T) {
|
|
|
|
inputs := map[string]int{
|
|
|
|
"C200B40A82": 3,
|
|
|
|
"04005AC33890": 54,
|
|
|
|
"880086C3E88112": 7,
|
|
|
|
"CE00C43D881120": 9,
|
|
|
|
"D8005AC2A8F0": 1,
|
|
|
|
"F600BC2D8F": 0,
|
|
|
|
"9C005AC2F8F0": 0,
|
|
|
|
"9C0141080250320F1802104A08": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, r := range inputs {
|
|
|
|
stream := []uint8{}
|
|
|
|
for _, hex := range c {
|
|
|
|
stream = append(stream, hexMap[hex]...)
|
|
|
|
}
|
|
|
|
_, parsedPacket := parsePackets(stream)
|
|
|
|
if r != solve(parsedPacket) {
|
|
|
|
t.Log(c, solve(parsedPacket))
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|