Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- import re
- with open("day_4.txt") as f:
- inp_4 = f.read().strip()
- inp_4 = inp_4.split('\n')
- # Part 1
- def is_real(s, k):
- s = Counter(s.replace('-', ''))
- counts = sorted(s.items(), key=lambda x: (-x[1], x[0]))
- return ''.join(map(lambda x: x[0], counts[:5])) == re.findall(r'\[(.*)\]', k)[0]
- total = 0
- inp_filter = []
- for ele in inp_4:
- s, k = ele.rsplit('-', 1)
- if is_real(s, k):
- num = int(re.findall(r'\d+', k)[0])
- inp_filter.append([s, num])
- total += num
- print(total)
- # Part 2 (builds on part 1)
- import string
- def shift(s, k):
- s = s.split('-')
- k = k%26
- d = {}
- for c in string.ascii_lowercase:
- d[c] = (ord(c) - ord('a') + k)%26 + ord('a')
- table = str.maketrans(d)
- return ' '.join(i.translate(table) for i in s)
- for s, k in inp_filter:
- b = shift(s, k)
- if "northpole" in b:
- print(k)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement