-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
44 lines (32 loc) · 997 Bytes
/
__init__.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
from typing import *
from aocpy import BaseChallenge
def parse(instr: str) -> List[str]:
return instr.strip().splitlines()
def get_priority(char: str) -> int:
char = char[0]
co = ord(char)
if ord("a") <= co and co <= ord("z"):
return (co - ord("a")) + 1
return (co - ord("A")) + 27
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
inp = parse(instr)
sigma = 0
for x in inp:
assert len(x) % 2 == 0
l = len(x) // 2
y = set(x[:l]).intersection(x[l:])
sigma += get_priority(y.pop())
return sigma
@staticmethod
def two(instr: str) -> int:
inp = parse(instr)
assert len(inp) % 3 == 0
sigma = 0
for i in range(0, len(inp), 3):
y = set(inp[i])
y.intersection_update(inp[i + 1])
y.intersection_update(inp[i + 2])
sigma += get_priority(y.pop())
return sigma