-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (45 loc) · 1.44 KB
/
main.py
File metadata and controls
56 lines (45 loc) · 1.44 KB
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
# --- Day 5: Print Queue ---
def part1():
lines = open("../../rust/data/inputs/05.txt", mode="r").read().splitlines()
result = 0
rules = set()
for line in lines:
if len(line) <= 1:
break
pages = [int(n) for n in line.split("|") if n.isdigit()]
rules.add((pages[0], pages[1]))
for line in lines[len(rules) + 1 :]:
update = [int(n) for n in line.split(",") if n.isdigit()]
if all((b, a) not in rules for a, b, in zip(update, update[1:])):
result += update[len(update) // 2]
return result
def part2():
lines = open("../../rust/data/inputs/05.txt", mode="r").read().split("\n\n")
rules = lines[0].split("\n")
updates = lines[1].split("\n")
result = 0
order = {}
for r in rules:
key, val = r.split("|")
if key in order:
order[key].append(val)
else:
order[key] = [val]
for i in updates:
update = i.split(",")
flag = True
j = 0
while j < len(update):
for k in range(j + 1, len(update)):
if update[k] not in order[update[j]]:
update[k], update[j] = update[j], update[k]
j -= 1
flag = False
break
j += 1
if not flag:
result += int(update[len(update) // 2])
return result
if __name__ == "__main__":
print(part1())
print(part2())