-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.py
More file actions
69 lines (55 loc) · 1.76 KB
/
day5.py
File metadata and controls
69 lines (55 loc) · 1.76 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
57
58
59
60
61
62
63
64
65
66
67
68
from collections import defaultdict
def part1(rules, pages) -> int:
ans = 0
rules_dict = defaultdict(set)
for r in rules.split("\n"):
start, end = r.strip().split("|")
rules_dict[start].add(end)
for line in pages.split("\n"):
update = line.split(",")
valid = True
seen = set()
for nb in update:
nb = nb.strip()
if nb in rules_dict:
end_pages = rules_dict[nb]
if end_pages & seen: # Only check for overlap; no need for 'seen' to be True
valid = False
break
seen.add(nb)
if valid:
ans += int(update[len(update)//2])
return ans
def part1(rules, pages) -> int:
ans = 0
rules_dict = defaultdict(set)
for r in rules.split("\n"):
start, end = r.strip().split("|")
rules_dict[start].add(end)
for line in pages.split("\n"):
update = line.split(",")
valid = True
seen = set()
pos = 0
for nb in update:
nb = nb.strip()
if nb in rules_dict:
end_pages = rules_dict[nb]
if end_pages & seen: # Only check for overlap; no need for 'seen' to be True
valid = False
break
seen.add(nb)
pos += 1
if not valid:
# rearange
invalid_nb = update[pos]
nums_set = set(update)
ans += int(update[len(update)//2])
return ans
if __name__ == "__main__":
with open("inputs/day5", "r") as f:
txt = f.read()
rules, pages = txt.split("\n\n")
rules_dict = {k: v for k, v in (r.strip().split("|") for r in rules.split("\n"))}
rules_list = [[int(start), int(end)] for start, end in (r.strip().split("|") for r in rules.split("\n"))]
print(part1(rules, pages))