-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.py
53 lines (39 loc) · 1.41 KB
/
day5.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
import sys
from pathlib import Path
def is_valid_order(order_map, xs):
seen = set()
for x in xs:
if order_map.get(x, set()) & seen:
return False
seen.add(x)
return True
def part1(order_map, xs):
return sum([input[len(input) // 2] for input in xs if is_valid_order(order_map, input)])
def parse_input(path: Path):
order_map = dict()
with path.open() as r:
for line in r:
if s := line.strip():
xy = [int(m) for m in s.split("|")]
order_map[xy[0]] = order_map.get(xy[0], set()) | {xy[1]}
else:
break
return order_map, [[int(m) for m in line.strip().split(",")] for line in r]
def sort_updates(order_map, updates):
result = []
for x in updates:
insert_positions = [result.index(y) for y in order_map.get(x, {}) if y in result]
if insert_positions:
result.insert(min(insert_positions), x)
else:
result.append(x)
return result
def part2(order_map, xs):
sorted_updates = [sort_updates(order_map, updates) for updates in xs if not is_valid_order(order_map, updates)]
return sum((updates[len(updates) // 2] for updates in sorted_updates))
def main():
order, updates = parse_input(Path(sys.argv[1]))
print(f"Part 1: {part1(order, updates)}")
print(f"Part 2: {part2(order, updates)}")
if __name__ == "__main__":
main()