Skip to content

Instantly share code, notes, and snippets.

@pawlos
Last active December 24, 2017 08:55
Show Gist options
  • Save pawlos/482e24afc80ddc36f2847800eb1432ee to your computer and use it in GitHub Desktop.
Save pawlos/482e24afc80ddc36f2847800eb1432ee to your computer and use it in GitHub Desktop.
Solution to Day 24: Electromagnetic Moat
#aoc_d24.py
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_s = 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_s, max_b
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)
st = calc_strength(s)
if max_s < st:
max_s = st
max_b = s
#print s
elif right == connector:
new_bridge.append(d['t'])
s = connect(new_bridge, left)
st = calc_strength(s)
if max_s < st:
max_s = st
max_b = s
#print s
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])
s = connect(bridge, left)
st = calc_strength(s)
if max_s < st:
max_s = st
max_b = s
print s
s = connect(bridge, right)
st = calc_strength(s)
if max_s < st:
max_s = st
max_b = s
print s
#print bridge
print max_s
print max_b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment