-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.py
35 lines (31 loc) · 990 Bytes
/
day05.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
from helpers.importHelpers import *
part1 = 0
part2 = 0
ruleList, updateList = getInput().split("\n\n")
rules = {}
for rule in ruleList.splitlines():
left, right = rule.split("|")
rules.setdefault(left, []).append(right)
for update in updateList.splitlines():
update = update.split(",")
previousPages = set()
valid = True
for page in update:
if page not in rules or all([laterPage not in previousPages for laterPage in rules[page]]):
previousPages.add(page)
else:
valid = False
break
if valid:
part1 += int(update[len(update)//2])
else:
newUpdate = [0]*len(update)
for i in range(len(update)-1, -1, -1): # fill new update from right to left
for elem in update:
if elem not in rules or all([laterPage not in update for laterPage in rules[elem]]):
newUpdate[i] = elem
update.remove(elem)
break
part2 += int(newUpdate[len(newUpdate)//2])
print("Part 1: ", part1)
print("Part 2: ", part2)