Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Solution to Day 13: Packet Scanners - Part 2
#aoc_d13.py
inp = ["0: 3","1: 2","4: 4","6: 4"]
inp = open('input_d13.txt','r').readlines()
data = [d.split(':') for d in inp]
dic = {}
data = dict(data)
maximum = int(max(data))+1
direction = [1]*maximum
pos = [0]*maximum
def setup():
for i in range(maximum):
if str(i) in data:
scanners[i] = [None]*int(data[str(i)])
direction[i] = 1
else:
scanners[i] = [None]
direction[i] = 0
for i in range(maximum):
if scanners[i] != [None]:
scanners[i][0] = 'S'
def operate(pos, scanners, direction):
for k in range(len(pos)):
#print scanners,pos
scanners[k][pos[k]] = None
pos[k] += direction[k]
if pos[k] + direction[k] >= len(scanners[k]):
direction[k] *= -1
elif pos[k] + direction[k] < 0:
direction[k] *= -1
if scanners[k] != [None]:
#print 'upd for ',k,pos[k]
scanners[k][pos[k]] = 'S'
scanners = [None]*maximum
setup()
copy = scanners
caught = 0
delay = 0
max_scanner = 0
last_executed = copy
last_directions = direction
last_pos = pos
print 'Len:',len(scanners)
while True:
caught = 0
#scanners = copy
#direction = [1]*maximum
#pos = [0]*maximum
#setup()
#print 'delay', delay
if delay != 0:
scanners = list(last_executed)
direction = list(last_directions)
pos = list(last_pos)
#print 'bef:',delay,scanners
operate(pos, scanners, direction)
#print 'aft:',delay,scanners
last_executed = list(scanners)
last_directions = list(direction)
last_pos = list(pos)
#print 'delayed by: ',delay,scanners
for i in range(maximum):
if scanners[i][0] == 'S':
if max_scanner < i:
max_scanner = i
print 'max: ',max_scanner, 'delay: ',delay
#print 'have it on pos: ',i
caught = 1
break
operate(pos, scanners, direction)
if caught == 0:
print delay
break
else:
delay += 1
#if delay > 15:
# break
print delay
#print caught
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment