Instantly share code, notes, and snippets.

# pawlos/advent_of_code_d13_p2.pySecret Last active Dec 27, 2017

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 = *maximum pos = *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] = '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 = *maximum #pos = *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] == '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
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.