-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
27 lines (23 loc) · 769 Bytes
/
main.py
File metadata and controls
27 lines (23 loc) · 769 Bytes
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
from functools import cmp_to_key
with open('input.txt', 'r') as file:
data = file.read().strip().split('\n\n')
order_rules = data[0].split('\n')
order_rules = [[int(x) for x in rule.split('|')] for rule in order_rules]
updates = data[1].split('\n')
updates = [[int(x) for x in update.split(',')] for update in updates]
def compare(a, b):
if [a, b] in order_rules:
return -1
elif [b, a] in order_rules:
return 1
return 0
total = 0
total2 = 0
for update in updates:
sorted_update = sorted(update, key=cmp_to_key(compare))
if update == sorted_update:
total += sorted_update[(len(sorted_update) - 1) // 2]
else:
total2 += sorted_update[(len(sorted_update) - 1) // 2]
print(total)
print(total2)