-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.py
executable file
·49 lines (35 loc) · 1.22 KB
/
day03.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
from src.common.utils import SolverFunctions
title = 'Day 3: Rucksack Reorganization'
parser_method = 'each_first_item'
handle_data = 'lines'
class SolveTheDay(SolverFunctions):
@classmethod
def level_1(cls, data):
counter = 0
for char in data:
mid = len(char) // 2
left = char[:mid]
right = char[mid:]
common, = set(left).intersection(right)
common = ord(common)
char_a = ord('a')
char_A = ord('A')
if common > char_a:
counter += common - char_a + 1
else:
counter += common - char_A + 26 + 1
return counter
@classmethod
def level_2(cls, data):
counter = 0
for char in range(0, len(data), 3):
step = data[char:char+3]
common, = set(step[0]) & set(step[1]) & set(step[2])
common = ord(common)
char_a = ord('a')
char_A = ord('A')
if common > char_a:
counter += common - char_a + 1
else:
counter += common - char_A + 26 + 1
return counter