day 5 part 1
parent
7f8e50b15f
commit
ccc463cf17
File diff suppressed because it is too large
Load Diff
|
@ -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
|
|
@ -0,0 +1,3 @@
|
|||
75,47,<em>61</em>,53,29
|
||||
97,61,<em>53</em>,29,13
|
||||
75,<em>29</em>,13
|
|
@ -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"))
|
Loading…
Reference in New Issue