day 5 cleanup
parent
eeff5e846c
commit
ba3639a098
57
day05/sol.py
57
day05/sol.py
|
@ -25,6 +25,7 @@ def solve(file):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
# part 1
|
||||||
t = 0
|
t = 0
|
||||||
invalid = []
|
invalid = []
|
||||||
for pages in updates:
|
for pages in updates:
|
||||||
|
@ -35,13 +36,20 @@ def solve(file):
|
||||||
invalid.append(pages)
|
invalid.append(pages)
|
||||||
print(t)
|
print(t)
|
||||||
|
|
||||||
|
# part 2: select applicable rules and form a graph
|
||||||
|
# find the root node
|
||||||
|
# do a topological sort by doing a DFS of the graph
|
||||||
t = 0
|
t = 0
|
||||||
for pages in invalid:
|
for pages in invalid:
|
||||||
|
# select rules
|
||||||
rules2 = [(p,g) for p,g in rules if p in pages and g in pages]
|
rules2 = [(p,g) for p,g in rules if p in pages and g in pages]
|
||||||
#print(pages, rules2)
|
#print(pages, rules2)
|
||||||
|
|
||||||
|
# find root node
|
||||||
S = set(p for p,g in rules2)
|
S = set(p for p,g in rules2)
|
||||||
E = set(g for p,g in rules2)
|
E = set(g for p,g in rules2)
|
||||||
top = next(iter(S-E))
|
top = next(iter(S-E))
|
||||||
|
|
||||||
# topological sort
|
# topological sort
|
||||||
l = []
|
l = []
|
||||||
seen = set()
|
seen = set()
|
||||||
|
@ -54,53 +62,16 @@ def solve(file):
|
||||||
l.append(node)
|
l.append(node)
|
||||||
visit(top)
|
visit(top)
|
||||||
l.reverse()
|
l.reverse()
|
||||||
|
|
||||||
|
# sanity check
|
||||||
|
assert len(l) == len(pages)
|
||||||
|
assert isvalid(l)
|
||||||
print(l)
|
print(l)
|
||||||
|
|
||||||
|
# add middle number to total
|
||||||
t += int(l[len(l)//2])
|
t += int(l[len(l)//2])
|
||||||
print(t)
|
print(t)
|
||||||
|
|
||||||
# failed attempt: establish a total order by iterating the graph
|
|
||||||
# to a fix point and then using it to sort the list.
|
|
||||||
# failed because the graph is cyclic
|
|
||||||
"""
|
|
||||||
less = {p:set() for p,g in rules}
|
|
||||||
for p,g in rules:
|
|
||||||
less[p].add(g)
|
|
||||||
|
|
||||||
print("less")
|
|
||||||
for p in less:
|
|
||||||
print(p,less[p])
|
|
||||||
|
|
||||||
done = False
|
|
||||||
while not done:
|
|
||||||
done = True
|
|
||||||
for p in less:
|
|
||||||
for g in list(less[p]):
|
|
||||||
if g in less:
|
|
||||||
e = less[g] - less[p]
|
|
||||||
if e:
|
|
||||||
less[p] |= e
|
|
||||||
done = False
|
|
||||||
print("less2")
|
|
||||||
for p in less:
|
|
||||||
print(p,less[p])
|
|
||||||
|
|
||||||
def compare(a,b):
|
|
||||||
if a == b:
|
|
||||||
return 0
|
|
||||||
if a in less and b in less[a]:
|
|
||||||
return -1
|
|
||||||
return 1
|
|
||||||
|
|
||||||
t = 0
|
|
||||||
for pages in invalid:
|
|
||||||
pages.sort(key = functools.cmp_to_key(compare))
|
|
||||||
print(pages)
|
|
||||||
assert(isvalid(pages))
|
|
||||||
t += int(pages[len(pages)//2])
|
|
||||||
print(t)
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
solve(open("sample1.in"))
|
solve(open("sample1.in"))
|
||||||
solve(open("input"))
|
solve(open("input"))
|
||||||
|
|
Loading…
Reference in New Issue