intersections = [(x, y) for (x, y) in scaffold if (all(neighbour in scaffold for neighbour in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]))] return sum([(x * y) for (x, y) in intersections]) def part2(): program = Program("17") program.program[0] = 2 main = "A,B,A,B,C,B,C,A,B,C\n" a = "R,4,R,10,R,8,R,4\n" b = "R,10,R,6,R,4\n" c = "R,4,L,12,R,6,L,12\n" camera = "n\n" input_buffer = [] [input_buffer.extend(ord(c) for c in routine) for routine in [main, a, b, c, camera]] program.run(input_buffer) return program.output_buffer[-1] if __name__ == "__main__": part1_correct = 5788 part2_correct = 648545 print_answer(1, part1(), part1_correct) print_answer(2, part2(), part2_correct)
if moons == moons_initial and velocities == velocities_initial: return steps def part1(moons, velocities): for _ in range(1000): apply_gravity(moons, velocities) apply_velocity(moons, velocities) total_energy = calc_energy(moons, velocities) return total_energy def part2(moons, velocities): return reduce(lcm, [get_period(moons, velocities, dim) for dim in range(3)]) if __name__ == "__main__": part1_correct = 12082 part2_correct = 295693702908636 moons = [extract_ints(moon) for moon in get_input("12")] velocities = [[0 for _ in ["x", "y", "z"]] for moon in moons] print_answer(1, part1(copy.deepcopy(moons), copy.deepcopy(velocities)), part1_correct) print_answer(2, part2(copy.deepcopy(moons), copy.deepcopy(velocities)), part2_correct)
def part2(program): for noun in range(99): for verb in range(99): # Reset program to initial state program.reset() # "the value placed in address 1 is called the noun" program.program[1] = noun # "the value placed in address 2 is called the verb" program.program[2] = verb program.run() # find noun and verb that cause program to output 19690720 if program.program[0] == 19690720: # return 100 * noun + verb return 100 * noun + verb if __name__ == "__main__": part1_correct = 4945026 part2_correct = 5296 program = Program(2) print_answer(1, part1(program), part1_correct) print_answer(2, part2(program), part2_correct)
if position in portal_locations['ZZ'] and q_level == 0: # queue is sorted by distance, first route to exit is shortest return q_distance + 1 if position in walkable - seen: heappush(q, (q_distance + 1, q_level, position)) def part1(portal_names, portal_locations, walkable): return get_shortest_route(portal_names, portal_locations, walkable) def part2(portal_names, portal_locations, walkable, portal_sides): return get_shortest_route(portal_names, portal_locations, walkable, portal_sides) if __name__ == "__main__": part1_correct = 596 part2_correct = 7610 maze = get_input(20, strip=False) portal_names, portal_locations, portal_sides = find_portals(maze) walkable = get_walkable(maze) print_answer(1, part1(portal_names, portal_locations, walkable), part1_correct) print_answer(2, part2(portal_names, portal_locations, walkable, portal_sides), part2_correct)