day 5 part 1

main
magical 2024-12-05 07:52:55 +00:00
parent 7f8e50b15f
commit ccc463cf17
4 changed files with 1443 additions and 0 deletions

1370
day05/input 100644

File diff suppressed because it is too large Load Diff

28
day05/sample1.in 100644
View File

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

3
day05/sample2.in 100644
View File

@ -0,0 +1,3 @@
75,47,<em>61</em>,53,29
97,61,<em>53</em>,29,13
75,<em>29</em>,13

42
day05/sol.py 100644
View File

@ -0,0 +1,42 @@
import functools
def parse(file):
rules = []
for line in file:
line = line.strip()
if line == "":
break
a, _, b = line.partition('|')
rules.append((a,b))
updates = []
for line in file:
line = line.strip()
updates.append(line.split(','))
return rules, updates
def solve(file):
rules, updates = parse(file)
rules = set(rules)
def isvalid(pages):
for i in range(len(pages)):
p = pages[i]
for g in pages[i+1:]:
if (g,p) in rules:
return False
return True
t = 0
invalid = []
for pages in updates:
if isvalid(pages):
print(pages)
t += int(pages[len(pages)//2])
else:
invalid.append(pages)
print(t)
solve(open("sample1.in"))
solve(open("input"))