-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.py
54 lines (42 loc) · 1.25 KB
/
day_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
44
45
46
47
48
49
50
51
52
53
54
from util import Test, Answer, ReadPuzzle, ReadExamplePuzzle
def priority(c):
v = ord(c)
if v >= ord('a'):
return v - ord('a') + 1
else:
return v - ord('A') + 27
def part1(data):
total = 0
for sack in data:
print("-",sack)
for c in sack[0]:
if c in sack[1]:
total += priority(c)
break
return total
def part2(data):
total = 0
for i in range(0, len(data), 3):
for c in "".join(data[i]):
if (c in data[i+1][0] or c in data[i+1][1]) and (c in data[i+2][0] or c in data[i+2][1]):
total += priority(c)
break
return total
def preprocess_data(data):
"""Do any additional parsing to the data to prep for answers"""
sacks = []
for d in data:
count = int(len(d) / 2)
sacks.append([d[:count], d[count:]])
return sacks
if __name__ == "__main__":
# test answer on example data
test_data, _ = ReadExamplePuzzle()
test_data = preprocess_data(test_data)
data = preprocess_data(ReadPuzzle())
for v in test_data:
print(v)
if Test(1, part1(test_data), 157):
Answer(1, part1(data))
if Test(2, part2(test_data), 70):
Answer(2, part2(data))