-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.py
43 lines (30 loc) · 904 Bytes
/
03.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
from itertools import zip_longest
from aocd.models import Puzzle
def grouper(iterable, n):
args = [iter(iterable)] * n
return zip_longest(*args)
def get_shared_char(s):
n = len(s)
s1 = s[:n//2]
s2 = s[n//2:]
return (set(s1) & (set(s2))).pop()
def get_priority(c):
if 'a' <= c <= 'z':
offset = ord('a')-1
else:
offset = ord('A')-27
return ord(c)-offset
def score_bag(s):
return get_priority(get_shared_char(s))
def get_badge(s1, s2, s3):
return (set(s1) & set(s2) & set(s3)).pop()
def get_badge_score(group):
return get_priority(get_badge(*group))
def main():
puzzle = Puzzle(year=2022, day=3)
lines = puzzle.input_data.split('\n')
puzzle.answer_a = sum(score_bag(li) for li in lines)
groups = grouper(lines, 3)
puzzle.answer_b = sum(get_badge_score(g) for g in groups)
if __name__ == "__main__":
main()