-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.py
30 lines (24 loc) · 897 Bytes
/
part1.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
import numpy as np
def solve(input_srt):
rules, print_order = input_srt.strip().split("\n\n")
rules = [rule.split("|") for rule in rules.split("\n")]
print_orders = [order.split(",") for order in print_order.split("\n")]
valid_orders = []
for update in print_orders:
# Map page number to indices
pos = {page: idx for idx, page in enumerate(update)}
correct = True
for l, r in rules:
if l in pos and r in pos:
if pos[l] > pos[r]:
correct = False
break
if correct:
valid_orders.append(update)
# loop through valid orders, find the middle, assuming that each
# update will be and odd number length
result = 0
for order in valid_orders:
mid_idx = len(order) // 2
result += int(order[mid_idx])
return str(result)