-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-05.py
80 lines (63 loc) · 1.38 KB
/
day-05.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def fix( u ):
# check each neighboring pair for order
# a "buble sort" of sorts
done = False
while not done:
swaps = 0
for i in range(len(u)-1):
if u[i] in rules:
if u[i+1] in rules[u[i]]:
continue # neighbors are in order
if u[i+1] in rules:
if u[i] in rules[u[i+1]]:
# neighbors out of order, swap them!
tmp = u[i]
u[i] = u[i+1]
u[i+1] = tmp
swaps += 1
if swaps == 0:
done = True
return u
# --- main ---
rules = {}
updates = []
parsing_rules = True
file = open('day-05.txt', 'r')
for line in file:
line = line.strip()
if not line:
parsing_rules = False
continue
if parsing_rules:
(a, b) = [int(x) for x in line.split("|")]
if a not in rules:
rules[a] = set()
rules[a].add(b)
else:
update = [int(x) for x in line.split(",")]
updates.append(update)
file.close()
part_one = part_two = 0
for update in updates:
fail = False
p = 0
for page in update:
if page in rules:
rule = rules[page]
for r in rule:
if r not in update:
continue
i = update.index(r)
if p > i:
fail = True # fail
break
if fail:
break
p += 1
mid = int(len(update)/2)
if fail:
part_two += fix(update)[mid]
else:
part_one += update[mid]
print( part_one )
print( part_two )