def part1(): real = list(eval(open('day13.txt').read())) pgm = Program(real) pgm.run() queue = pgm.dump() print(len(queue)) twos = [1 for k in take(queue, 3) if k[2] == 2] print("Part 1:", len(twos)) # 228
def part2(): pgm = Program(real) pgm.pgm[0] = 2 instructions = ( "A,B,A,C,C,A,B,C,B,B\n", "L,8,R,10,L,8,R,8\n", "L,12,R,8,R,8\n", "L,8,R,6,R,6,R,10,L,8\n", "n\n" ) for c in ''.join(instructions): pgm.push(ord(c)) pgm.run() d = pgm.dump() if TRACE: print( Maze(d[:-1]).string ) return d[-1]
def part2(): real = list(eval(open('day13.txt').read())) # Make one run to determine the limits of the grid. pgm = Program(real) pgm.run() queue = list(take(pgm.dump(), 3)) w = max(k[0] for k in queue) + 1 h = max(k[1] for k in queue) + 1 print(w, h) # Create the printable grid. grid = [] for i in range(h): grid.append([' '] * w) # Populate it from the part 1 output. for x, y, t in queue: grid[y][x] = ' #x-o'[t] display(grid) # Insert a quarter. real[0] = 2 # Go run the app. pgm = Prog13(real) pgm.grid = grid pgm.run() # Pop any unfinished output. pgm.clear_queue() display(grid) print("Part 2:", pgm.score) # 10776
def __getitem__(self,pt): """ Allow indexing by a Point. """ if pt.x < 0 or pt.x >= self.size or pt.y < 0 or pt.y >= self.size: return '.' return self.maze[pt.y][pt.x] TRACE = 'trace' in sys.argv # Run the program once to get the path. real = list(eval(open('day17.txt').read())) pgm = Program(real) pgm.run() maze = Maze( pgm.dump() ) print(maze.string) def part1(): # Find the robot. robot = Point(maze.maze[-1].find('^'), len(maze.maze)-1) lastturn = robot # Robot starts facing north. facing = Point(0,-1) crossings = set() turns = [] while 1: