-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_23.py
51 lines (42 loc) · 1.63 KB
/
day_23.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
from collections import defaultdict
from functools import cached_property
from models.aoc_solution import AOCSolution
class Day23(AOCSolution):
EXPECTED = {
"part_one": {"sample": 7, "data": 1230},
"part_two": {
"sample": "co,de,ka,ta",
"data": "az,cj,kp,lm,lt,nj,rf,rx,sn,ty,ui,wp,zo",
},
}
def __post_init__(self) -> None:
self.connections = [tuple(line.split("-")) for line in self.data.splitlines()]
self.__dict__.pop("groups", None)
@cached_property
def groups(self) -> dict[str, set[str]]:
groups: dict[str, set[str]] = defaultdict(set)
for a, b in self.connections:
groups[a] |= {b}
groups[b] |= {a}
return groups
def part_one(self) -> int:
threes = {
tuple(sorted((computer, conn1, conn2)))
for computer in self.groups
for conn1 in self.groups[computer]
for conn2 in self.groups[conn1]
if computer in self.groups[conn2]
}
return sum((any(computer[0] == "t" for computer in three)) for three in threes)
def part_two(self) -> str:
connected_groups: set[tuple[str, ...]] = set()
for comp, connections in self.groups.items():
connected_group = {comp}
for conn in connections:
if all(nbr in self.groups[conn] for nbr in connected_group):
connected_group |= {conn}
connected_groups.add(tuple(sorted(connected_group)))
biggest = max(connected_groups, key=len)
return ",".join(biggest)
if __name__ == "__main__":
Day23().run()