Skip to content

Instantly share code, notes, and snippets.

@pawlos
Last active December 27, 2017 21:24
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/8218b4934a3e4396eb480e0083785ab6 to your computer and use it in GitHub Desktop.
Save pawlos/8218b4934a3e4396eb480e0083785ab6 to your computer and use it in GitHub Desktop.
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