part 12 day 2, inelegant

This commit is contained in:
OMGOMG 2021-12-12 21:52:38 +01:00
parent 7fd81e71a8
commit 0552c88a29
1 changed files with 13 additions and 14 deletions

View File

@ -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)