You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
from functools import cmp_to_key
|
|
|
|
|
|
if __name__ == '__main__':
|
|
lines = [line.strip('\n') for line in open(sys.argv[1])]
|
|
rules = [tuple([int(y) for y in x.split('|')]) for x in lines[:lines.index('')]]
|
|
updates = [[int(x) for x in line.split(',')] for line in lines[lines.index('')+1:]]
|
|
|
|
def compare(a,b):
|
|
for rule in rules:
|
|
if a in rule and b in rule:
|
|
return -1 if a == rule[0] else 1
|
|
return 0
|
|
|
|
res1, res2 = 0, 0
|
|
for update in updates:
|
|
correct = True
|
|
for i, u in enumerate(update):
|
|
for u2 in update[i+1:]:
|
|
if compare(u,u2) == 1:
|
|
correct = False
|
|
break
|
|
if not correct:
|
|
update.sort(key=cmp_to_key(compare))
|
|
res2 += update[int(len(update)/2)]
|
|
break
|
|
if correct: res1 += update[int(len(update)/2)]
|
|
|
|
# challenge 1
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|