You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
import re
|
|
from copy import deepcopy
|
|
|
|
def parse(l):
|
|
groups = re.compile(r"(?P<p1>\w+)-(?P<p2>\w+)").search(l)
|
|
p1, p2 = groups.groups()
|
|
for p in (p1,p2):
|
|
if not p in opts.keys(): opts[p] = set()
|
|
if not p in opts.keys(): opts[p] = set()
|
|
if p.islower() and p not in ('start', 'end'): opts['small'].add(p)
|
|
if p.islower() and p not in ('start', 'end'): opts['small'].add(p)
|
|
opts[p1].add(p2)
|
|
opts[p2].add(p1)
|
|
|
|
def paths(p1, l, twice=''):
|
|
p = deepcopy(p1)
|
|
p.append(l)
|
|
if p.count(twice) > 1: twice = ''
|
|
if l == 'end':
|
|
pths.add(tuple(p))
|
|
return
|
|
for n in opts[l]:
|
|
if not ((n != twice and n in p and n in opts['small']) or n == 'start'):
|
|
paths(p, n, twice)
|
|
|
|
if __name__ == '__main__':
|
|
lines = open(sys.argv[1]).read().strip('\n\n').split('\n') # or split \n\n or sth similar
|
|
|
|
opts = {'start': set(), 'end': set(), 'small': set()}
|
|
[parse(i) for i in lines]
|
|
|
|
# challenge 1
|
|
pths = set()
|
|
paths([], 'start')
|
|
|
|
res1 = str(len(pths))
|
|
print("challenge 1:" +"\n" + res1 + "\n")
|
|
|
|
# challenge 2
|
|
pths = set()
|
|
[paths([], 'start', t) for t in opts['small']]
|
|
|
|
res2 = str(len(pths))
|
|
print("challenge 2:" +"\n" + res2 + "\n")
|
|
|
|
|