Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pawlos
Created December 24, 2017 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawlos/ef49fc86128ef98c46beab1a33fc80e6 to your computer and use it in GitHub Desktop.
Save pawlos/ef49fc86128ef98c46beab1a33fc80e6 to your computer and use it in GitHub Desktop.
Solution to Day 24: Electromagnetic Moat
#aoc_d24.py
'''class Tree(object):
def __init__(self):
self.left = None
self.right = None
self.data = None
'''
inp = [{'t':"0/2",'s':0},
{'t':"2/2",'s':0},
{'t':"2/3",'s':0},
{'t':"3/4",'s':0},
{'t':"3/5",'s':0},
{'t':"0/1",'s':0},
{'t':"10/1",'s':0},
{'t':"9/10",'s':0}]
inp = []
f = open('input_d24.txt')
for i in f.readlines():
inp.append({'t':i.replace('\n',''),'s':0})
inits = []
max_l = 0
max_b = {}
for i in inp:
#print i
if i['t'].startswith("0"):
inits.append(i)
#i['s']=1
if "/0" in i['t']:
inits.append(i)
#i['s']=1
def calc_strength(b):
s = 0
for i in b:
el = i.split("/")
left = int(el[0])
right = int(el[1])
s += left
s += right
return s
def connect(bridge, connector):
global max_b, max_l
new_bridge = list(bridge)
for d in inp:
if d['s'] == 1:
continue
i = d['t']
el = i.split("/")
left = int(el[0])
right = int(el[1])
if left == connector or right == connector:
d['s'] = 1
if left == connector:
new_bridge.append(d['t'])
s = connect(new_bridge, right)
elif right == connector:
new_bridge.append(d['t'])
s = connect(new_bridge, left)
st = calc_strength(s)
if max_l <= len(s):
max_l = len(s)
if max_l not in max_b:
max_b[max_l] = []
max_b[max_l].append(s)
#print 'Max strength: ',max_s
#print 'Max bridge: ', max_b
#print 'Max length: ', max_l
d['s'] = 0
del new_bridge[-1]
#print new_bridge
return new_bridge
for i in inits:
bridge = []
bridge.append(i['t'])
d = i['t'].split("/")
left = int(d[0])
right = int(d[1])
i['s'] = 1
if left != 0:
print i, left
s = connect(bridge, left)
if max_l <= len(s):
max_l = len(s)
if max_l not in max_b:
max_b[max_l] = []
max_b[max_l].append(s)
if right != 0:
print i, right
s = connect(bridge, right)
if max_l <= len(s):
max_l = len(s)
if max_l not in max_b:
max_b[max_l] = []
max_b[max_l].append(s)
i['s'] = 0
#print s
#print bridge
print max(calc_strength(s) for s in max_b[max_l])
#print 'Max strength: ',max_s
#print 'Max bridge: ', max_b
#print 'Max length: ', max_l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment