-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
41 lines (29 loc) · 918 Bytes
/
day3.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
#!/usr/bin/env python3
from functools import reduce
from string import ascii_letters as letters
from typing import Tuple
s = '''vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw'''.split('\n')
with open('day3.input', 'r') as f:
s = f.read().strip().split('\n')
def partition(s: str) -> Tuple[str]:
return s[:len(s)//2], s[len(s)//2:]
def common(p: Tuple[str]) -> str:
l, r = p
return ''.join(set(l) & set(r))
def score(s: str) -> int:
return letters.index(s) + 1
t = map(partition, s)
t = map(common, t)
t = map(score, t)
print(sum(t))
t = [ set(n) for n in s ]
t = [ t[i:i+3] for i in range(0, len(t), 3) ]
t = [ score(reduce(lambda a, b: a & b, k).pop()) for k in t ]
print(sum(t))
# only unique characters matter, so sets and their __intersection__ method finds
# common characters.