In [59]:
myinput = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input18.txt'
safe tiles (.) and traps (^)
In [60]:
trap_dict = {True: '^', False: '.'}
In [61]:
def parse_input(myinput):
with open(myinput, 'rt') as f:
return next(f).rstrip()
In [62]:
parse_input(myinput)
Out[62]:
In [83]:
def trap(left, center, right):
cond = []
cond.append((left and center) and not right)
cond.append((center and right) and not left)
cond.append(left and ((not center) and (not right)))
cond.append(right and ((not left) and (not center)))
for item in cond:
if item:
return True
return False
def next_row(row):
new_row = []
for i in range(len(row)):
if i == 0:
left = False
else:
left = (row[i-1] == '^')
center = (row[i] == '^')
if i == len(row) - 1:
right = False
else:
right = (row[i+1] == '^')
new_row.append(trap_dict[trap(left, center, right)])
return ''.join(new_row)
def count_safe(row, n):
safe = row.count('.')
for i in range(n-1):
row = next_row(row)
safe += row.count('.')
return safe
In [85]:
count_safe('..^^.', 3)
Out[85]:
In [86]:
count_safe('.^^.^.^^^^', 10)
Out[86]:
In [87]:
row = parse_input(myinput)
count_safe(row, 40)
Out[87]:
In [88]:
row = parse_input(myinput)
count_safe(row, 400000)
Out[88]: