Solution to Day 12: Digital Plumber - Part 1
#aoc_d12.py | |
import re | |
inp = open('input_d12.txt','r').readlines() | |
#inp = ["0 <-> 2","1 <-> 1", "2 <-> 0, 3, 4", "3 <-> 2, 4", "4 <-> 2, 3, 6", "5 <-> 6", "6 <-> 4, 5"] | |
count = 0 | |
connections = {} | |
visited = [] | |
def check(items): | |
#print items | |
if 0 == items: | |
#print 'yup' | |
return True | |
if type(items) is int and items not in visited: | |
#print 'loop for ',items,visited | |
visited.append(items) | |
r = check(connections[items]) | |
#visited.remove(items) | |
#print 'loop result:',r | |
return r | |
elif type(items) is int and items in visited: | |
#print '=> f', visited | |
return False | |
#print 'moar check' | |
for i in items: | |
#print 'loop for ',i,visited | |
if check(i): | |
return True | |
#print '=> f2' | |
return False | |
for i in inp: | |
if "," in i: | |
m = re.match("(\d+)\s<->\s([0-9,\s]+)",i) | |
#print i | |
i1 = int(m.group(1)) | |
#print m.group(2) | |
items = [int(e) for e in m.group(2).split(',')] | |
connections[i1] = items | |
#for it in items: | |
# connections[it] = i1 | |
if "," not in i: | |
m = re.match("(\d+)\s<->\s(\d+)",i) | |
i1 = int(m.group(1)) | |
i2 = int(m.group(2)) | |
if i1 in connections: | |
connections[i1].append(i2) | |
else: | |
connections[i1] = [i2] | |
if i2 in connections: | |
connections[i2].append(i1) | |
else: | |
connections[i2] = [i1] | |
#print connections | |
for k in connections: | |
visited = [] | |
if connections[k] is int and connections[k] == 0: | |
#print "+1 for k=",k | |
count += 1 | |
else: | |
if check(connections[k]): | |
#print "+1 for k=",k | |
count += 1 | |
else: | |
pass#print "no for k=",k | |
#print '-----' | |
print count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment