-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_04.py
54 lines (40 loc) · 1.16 KB
/
day_04.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
# https://adventofcode.com/2016/day/4
from helper import get_input, print_result
from collections import Counter
DAY = 4
inp = get_input(DAY)
# Parse input
# (room_code, sector_id, checksum)
def parse_input():
res = []
for room in inp:
room = room.split("-")
tmp = room[-1].split("[")
sector_id = int(tmp[0])
checksum = tmp[1][:-1]
res.append((room[:-1], sector_id, checksum))
return res
inp = parse_input()
real_rooms = []
def p1():
res = 0
for r in inp:
top_5 = Counter(sorted("".join(r[0]))).most_common(5)
top_5 = "".join(x[0] for x in top_5)
if top_5 == r[2]:
res += r[1]
real_rooms.append(r)
return res
def p2():
def get_room_name(rc, rot):
res = ""
for room_code in rc:
for r in room_code:
res += chr(((ord(r) - 97 + rot) % 26) + 97)
res += " "
return res.strip()
for room in real_rooms:
rc, rot, _ = room
if get_room_name(rc, rot) == "northpole object storage":
return rot
print_result(DAY, p1(), p2())