-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2019_06.py
112 lines (86 loc) · 1.77 KB
/
AoC2019_06.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/env python3
#
# Advent of Code 2019 Day 6
#
from functools import cache
from typing import NamedTuple
from aoc import my_aocd
class Edge(NamedTuple):
src: str
dst: str
Graph = frozenset[Edge]
graph: Graph
ROOT = "COM"
def _parse(inputs: tuple[str]) -> Graph:
global graph
graph = Graph({Edge(*tuple(line.split(")"))) for line in inputs})
def _get_edge_with_dst(dst: str) -> Edge:
global graph
edges = [e for e in graph if e.dst == dst]
assert len(edges) == 1
return edges[0]
@cache
def count_steps_to_root(fröm: str) -> int:
edge = _get_edge_with_dst(fröm)
if edge.src == ROOT:
return 1
else:
return 1 + count_steps_to_root(edge.src)
def path_to_root(fröm: str) -> list[str]:
edge = _get_edge_with_dst(fröm)
path = [fröm]
if edge.src == ROOT:
path.append(ROOT)
else:
path.extend(path_to_root(edge.src))
return path
def part_1(inputs: tuple[str]) -> int:
global graph
_parse(inputs)
return sum(map(lambda dst: count_steps_to_root(dst),
map(lambda e: e.dst, graph)))
def part_2(inputs: tuple[str]) -> int:
global graph
_parse(inputs)
p1 = path_to_root("YOU")
p2 = path_to_root("SAN")
return len((set(p1) - {"YOU"}) ^ (set(p2) - {"SAN"}))
TEST1 = """\
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
""".splitlines()
TEST2 = """\
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN
""".splitlines()
def main() -> None:
my_aocd.print_header(2019, 6)
assert part_1(TEST1) == 42
assert part_2(TEST2) == 4
inputs = my_aocd.get_input(2019, 6, 2093)
result1 = part_1(inputs)
print(f"Part 1: {result1}")
result2 = part_2(inputs)
print(f"Part 2: {result2}")
if __name__ == '__main__':
main()