-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202007.py
93 lines (67 loc) · 2.38 KB
/
aoc202007.py
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
"""AoC 7, 2020: Handy Haversacks."""
# Standard library imports
import pathlib
import sys
from functools import cache
# Third party imports
import parse
BAGS = parse.compile("{color} bags contain {inner}.")
BAGS_INNER = parse.compile("{num:d} {color} ba{gs}")
def parse_data(puzzle_input):
"""Parse input."""
return tuple(parse_line(line) for line in puzzle_input.split("\n"))
def parse_line(line):
"""Parse one line of input.
## Examples:
>>> parse_line("shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.")
('shiny gold', (('dark olive', 1), ('vibrant plum', 2)))
>>> parse_line("faded blue bags contain no other bags.")
('faded blue', ())
"""
outer = BAGS.parse(line)
return outer["color"], tuple(
(inner["color"], inner["num"])
for bag in outer["inner"].split(", ")
if (inner := BAGS_INNER.parse(bag))
)
def part1(data):
"""Solve part 1."""
bags = tuple((outer, tuple(bag for bag, _ in inner)) for outer, inner in data)
return len(can_contain(bags, "shiny gold"))
def part2(data):
"""Solve part 2."""
return must_contain(data, "shiny gold") - 1
@cache
def can_contain(bags, color):
"""List all bags that can contain the given color.
## Example:
>>> bags = (("a", ()), ("b", ("a",)), ("c", ("b",)), ("d", ()), ("e", ("d",)))
>>> sorted(can_contain(bags, "a"))
['b', 'c']
"""
directly = {outer for outer, inner in bags if color in inner}
if inside := [can_contain(bags, bag) for bag in directly]:
return directly | set.union(*inside)
else:
return directly
@cache
def must_contain(bags, color):
"""Count the number of bags that a given color must contain.
The bag itself is included in the count.
## Example:
>>> bags = (("a", ()), ("b", (("a", 3),)), ("c", (("b", 2),)))
>>> must_contain(bags, "c")
9
"""
inner = next(inner for bag, inner in bags if bag == color)
return 1 + sum(num * must_contain(bags, bag) for bag, num in inner)
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))