-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04.py
50 lines (36 loc) · 1.07 KB
/
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
# --- Day 4: Security Through Obscurity ---
import re
with open("input/04.txt", "r") as rooms:
sectors = northpole_objects = 0
alphabet = "abcdefghijklmnopqrstuvwxyz"
for room in rooms:
regex = re.search(r"([a-z\-]*)-([0-9]+)\[([a-z]*)\]", room)
roomname = regex.group(1)
sector = int(regex.group(2))
checksum = regex.group(3)
letters = "".join(roomname.split("-"))
uniq = {}
# calculated checksum
cs = ""
for l in letters:
if not l in uniq.keys():
uniq[l] = letters.count(l)
i = max(uniq.values()) # work backwards down to 1
while i > 0:
chars = [k for k, v in uniq.items() if v == i]
chars.sort()
cs += "".join(chars)
i -= 1
if(cs[:5] == checksum):
sectors += sector
decrypted = ""
for r in roomname:
if r == "-":
decrypted += " "
else:
loc = alphabet.find(r)
decrypted += alphabet[(loc + sector) % len(alphabet)]
if decrypted == "northpole object storage":
northpole_objects = sector
print(sectors)
print(northpole_objects)