-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.py
47 lines (37 loc) · 1.12 KB
/
day05.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
from pathlib import Path
from collections import defaultdict
rules_text, batches_text = Path('./data/05.txt').read_text().split('\n\n')
RULES = defaultdict(list)
for rule in rules_text.split():
a, b = rule.split('|')
RULES[int(a)].append(int(b))
batches = []
for batch in batches_text.split():
batches.append(list(map(int, batch.split(','))))
def is_correct(batch) -> bool:
correct = True
for i, page in enumerate(batch):
if not all(p in RULES.get(page, []) for p in batch[i+1:]):
correct = False
break
return correct
# Part 1.
corrects, incorrects = [], []
for batch in batches:
if is_correct(batch):
corrects.append(batch[len(batch) // 2])
else:
incorrects.append(batch) # For Part 2.
print(sum(corrects))
# Part 2.
# Correcting seems like overkill. Also hard.
count = 0
for batch in incorrects:
midpoint = len(batch) // 2
# Find member with this many "afters" in this batch.
for n in batch:
afters = [m for m in RULES.get(n, []) if m in batch]
if len(afters) == midpoint:
count += n
break
print(count)