2020 - Day 16 (Ticket Translation)
View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3
#Advent of Code
#2020 Day 16, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
import re
def solve(puzzle_input):
field_rule = re.compile(r"(\d+)-(\d+) or (\d+)-(\d+)")
def consume_section():
output = []
while puzzle_input:
line = puzzle_input.pop(0)
if line == "":
break
output.append(line)
return output
def parse_rules(l):
vals = re.search(field_rule, l).groups()
return ((int(vals[0]), int(vals[1])), (int(vals[2]), int(vals[3])))
def find_invalid(l):
return sum(x for x in [int(n) for n in l.split(",")] if not any(r[0] <= x <= r[1] for f in fields.values() for r in f))
fields = {line.split(":")[0]:parse_rules(line) for line in consume_section()}
your_ticket = consume_section()[1]
nearby_tickets = consume_section()[1:]
return sum(find_invalid(ticket) for ticket in nearby_tickets)
def main():
puzzle_input = util.read.as_lines()
error_rate = solve(puzzle_input)
print("The ticket scanning error rate is " + str(error_rate) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["class: 1-3 or 5-7",
"row: 6-11 or 33-44",
"seat: 13-40 or 45-50",
"",
"your ticket:",
"7,1,14",
"",
"nearby tickets:",
"7,3,47",
"40,4,50",
"55,2,20",
"38,6,12"]), 71)
if __name__ == "__main__":
run(main)
Part 2
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
#!/usr/bin/env python3
#Advent of Code
#2020 Day 16, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from math import prod
import re
def solve(puzzle_input):
field_rule = re.compile(r"(\d+)-(\d+) or (\d+)-(\d+)")
def consume_section():
output = []
while puzzle_input:
line = puzzle_input.pop(0)
if line == "":
break
output.append(line)
return output
def parse_rules(l):
vals = re.search(field_rule, l).groups()
return ((int(vals[0]), int(vals[1])), (int(vals[2]), int(vals[3])))
def is_invalid(l):
return any(True for x in l if not any(r[0] <= x <= r[1] for f in fields.values() for r in f))
fields = {line.split(":")[0]:parse_rules(line) for line in consume_section()}
your_ticket = [int(n) for n in consume_section()[1].split(",")]
nearby_tickets = [[int(n) for n in l.split(",")] for l in consume_section()[1:] if not is_invalid([int(n) for n in l.split(",")])]
unallocated_fields = {k:set(n for n in range(len(fields))) for k in fields}
allocated_fields = {}
while len(unallocated_fields) > 0:
field_for_deletion = []
for field in unallocated_fields:
for col in list(unallocated_fields[field]):
working = [t[col] for t in nearby_tickets]
if not all(fields[field][0][0] <= t <= fields[field][0][1] or fields[field][1][0] <= t <= fields[field][1][1] for t in working):
unallocated_fields[field].remove(col)
if len(unallocated_fields[field]) == 1:
(allocated_fields[field],) = unallocated_fields[field]
for other in unallocated_fields:
unallocated_fields[other].discard(allocated_fields[field])
field_for_deletion.append(field)
for d in field_for_deletion:
del unallocated_fields[d]
return prod(your_ticket[allocated_fields[field]] for field in allocated_fields if field.startswith("departure"))
def main():
puzzle_input = util.read.as_lines()
product = solve(puzzle_input)
print("The product of the six values starting with the word departure is " + str(product) + ".")
class AOC_Tests(unittest.TestCase):
@unittest.skip("No test cases")
def test_ex1(self):
pass
if __name__ == "__main__":
run(main)