Solution to Day 24: Electromagnetic Moat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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