-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle05b.py
63 lines (55 loc) · 1.42 KB
/
puzzle05b.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
55
56
57
58
59
60
61
62
with open("input") as f:
nums = f.read().splitlines()
n = False
rules = dict()
updates = []
for num in nums:
if num == "":
n = True
continue
if not n:
a, b = num.split("|")
a, b = int(a), int(b)
if not rules.get(a):
rules[a] = set()
rules[a].add(b)
else:
updates.append(list(map(int, num.split(","))))
print(rules)
print(updates)
def sort_list(nums):
sl = nums[:]
for a in nums:
sl.remove(a)
bset = rules.get(a)
if bset is None:
sl.append(a)
continue
smallest = []
for b in bset:
if b in sl:
smallest.append(sl.index(b))
if not smallest:
sl.append(a)
else:
sl.insert(min(smallest), a)
return sl
ans = 0
for update in updates:
print("---------------------")
print("running:", update)
try:
for u in update:
vset = rules.get(u)
if vset is None: continue
for v in vset:
if v not in update: continue
if not update.index(u) < update.index(v):
raise(UserWarning('Update not in right order'))
except UserWarning as e :
update = sort_list(update)
print(update)
middle_idx = (len(update) - 1) // 2
print(update[middle_idx], "<==", update)
ans += update[middle_idx]
print(ans)