Add solution for day 12
This commit is contained in:
1 parent
80a3e451ac
commit
5f027cb5cf
6 files changed
+257
No files matched your search
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/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")
|
||||
|
||||
Reference in new issue
Block a user