-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.py
54 lines (42 loc) · 1.61 KB
/
solver.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
from collections import defaultdict
from dataclasses import dataclass
from aoc import answer
from aoc.parser import Parser
@dataclass(frozen=True)
class Room:
name: str
sector_id: int
checksum: str
def valid(self) -> bool:
frequencies = defaultdict(int)
for ch in self.name:
frequencies[ch] += 1
expected_checksum = list(set([ch for ch in self.name if ch != "-"]))
expected_checksum.sort(key=lambda ch: (-frequencies[ch], ch))
expected_checksum = expected_checksum[: len(self.checksum)]
return self.checksum == "".join(expected_checksum)
def decrypt(self) -> str:
decrypted: list[str] = []
for ch in self.name:
if ch == "-":
decrypted.append(" ")
else:
index = ((ord(ch) - ord("a")) + self.sector_id) % 26
decrypted.append(chr(ord("a") + index))
return "".join(decrypted)
@answer.timer
def main() -> None:
rooms = [room for room in get_rooms() if room.valid()]
answer.part1(278221, sum([room.sector_id for room in rooms]))
north_pole_room = filter(
lambda room: room.decrypt() == "northpole object storage", rooms
)
answer.part2(267, next(north_pole_room).sector_id)
def get_rooms() -> list[Room]:
def parse_room(line: str) -> Room:
name, sector_id_checksum = line.rsplit("-", 1)
sector_id, checksum = sector_id_checksum.split("[")
return Room(name=name, sector_id=int(sector_id), checksum=checksum[:-1])
return [parse_room(line) for line in Parser().lines()]
if __name__ == "__main__":
main()