part 12 day 2, inelegant
This commit is contained in:
parent
7fd81e71a8
commit
0552c88a29
27
twelve.py
27
twelve.py
|
@ -1,34 +1,30 @@
|
|||
#!/usr/bin/env python3
|
||||
from useful import *
|
||||
from igraph import *
|
||||
|
||||
def parseline(s):
|
||||
a, b = [x for x in s.strip().split('-')]
|
||||
if b.isupper(): # ensure uppercase is never second
|
||||
return (b, a)
|
||||
return (a, b)
|
||||
return [x for x in s.strip().split('-')]
|
||||
|
||||
def uniqpath(a, b):
|
||||
return '-'.join(sorted([a, b])) # ignore direction
|
||||
|
||||
def solve(pos, matrix):
|
||||
global pathname, index, visit
|
||||
def solve(pos, matrix, can_twice=False, path=[]):
|
||||
global pathname, index, visit, cheat
|
||||
visit += 1
|
||||
|
||||
if pathname[pos] == 'end':
|
||||
pathtext = '-'.join([pathname[i] for i in (*path, pos)])
|
||||
cheat |= {pathtext}
|
||||
return 1
|
||||
|
||||
acc = 0
|
||||
if not pathname[pos].isupper():
|
||||
nextrix = matrix.copy()
|
||||
for i in range(len(matrix)):
|
||||
nextrix[i,pos] = False
|
||||
nextrix[pos,i] = False
|
||||
nextrix[:,pos] = False
|
||||
nextrix[pos,:] = False
|
||||
else:
|
||||
nextrix = matrix
|
||||
for i in range(len(matrix)):
|
||||
if matrix[pos][i]:
|
||||
acc += solve(i, nextrix)
|
||||
acc += solve(i, nextrix, can_twice, path + [pos, ])
|
||||
if can_twice and not pathname[pos].isupper() and pathname[pos] != 'start':
|
||||
acc += solve(i, matrix, False, path + [pos, ])
|
||||
return acc
|
||||
|
||||
with open(0) as f:
|
||||
|
@ -50,5 +46,8 @@ with open(0) as f:
|
|||
matrix[index[a], index[b]] = True
|
||||
matrix[index[b], index[a]] = True
|
||||
ic(matrix)
|
||||
cheat = set()
|
||||
print(solve(index['start'], matrix))
|
||||
print(solve(index['start'], matrix, can_twice=True), '(may be duplicates)')
|
||||
print(len(cheat))
|
||||
ic(visit)
|
||||
|
|
Loading…
Reference in New Issue