def test_examples(self): self.assertEqual( len( str(next(run(parse("1102,34915192,34915192,7,4,7,99,0"), None)))), 16) self.assertEqual(next(run(parse("104,1125899906842624,99"), None)), 1125899906842624)
def test_part1(self): self.assertEqual( amplifiers( parse("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0"), [4, 3, 2, 1, 0] ), 43210, ) self.assertEqual( amplifiers( parse( "3,23,3,24,1002,24,10,24,1002,23,-1,23," "101,5,23,23,1,24,23,23,4,23,99,0,0" ), [0, 1, 2, 3, 4], ), 54321, ) self.assertEqual( amplifiers( parse( "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33," "1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0" ), [1, 0, 4, 3, 2], ), 65210, )
def test_part2(self): self.assertEqual( feedback_amplifiers( parse( "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26," "27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5" ), [9, 8, 7, 6, 5], ), 139629729, ) self.assertEqual( feedback_amplifiers( parse( "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54," "-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4," "53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10" ), [9, 7, 8, 5, 6], ), 18216, )
(point[0], point[1] - 1), ) def navigate(program): # todo # R,8,L,10 # R,8 # R,2 # R,10 # R,12 intcode = IntCode(program) instruction = None while True: output = intcode.run(instruction) if not output: break print(chr(output), end="") if __name__ == "__main__": with open("../inputs/day17.input") as f: program = parse(f.readline()) # Part I print(calibrate(program)) # 2804 # Part II program[0] = 2 print(navigate(program))
def test_pass2(self): data = puzzle.parse(read_file("05", "2")) result = puzzle.solve(data, 5) self.assertEqual(result, 3892695)
def test_pass3(self): data = puzzle.parse(read_file("05", "3")) result = puzzle.solve(data, 1) print(result)
signal = amps[i].run(signal) # loop until program finishes for i in itertools.cycle(range(5)): output = amps[i].run(signal) if output is None: break signal = output return signal def part2(program): permutations = itertools.permutations([5, 6, 7, 8, 9]) max_output = 0 for permutation in permutations: output = feedback_amplifiers(program, permutation) max_output = max(output, max_output) return max_output if __name__ == "__main__": with open("../inputs/day7.input") as f: program = f.readline() print(part1(parse(program))) # 92663 print(part2(parse(program))) # 14365052
from day5 import parse, run if __name__ == "__main__": with open("../inputs/day9.input") as f: content = f.readline().strip() # Part I print(next(run(parse(content), 1))) # 3235019597 # Part II print(next(run(parse(content), 2))) # 80274
def test_examples(self): self.assertEqual(next(run(parse("99"), 1), None), None) self.assertEqual(next(run(parse("3,0,4,0,99"), 1)), 1) self.assertEqual(next(run(parse("3,0,4,0,99"), 28)), 28) self.assertEqual( next(run(parse("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9"), 0)), 0) self.assertEqual( next(run(parse("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9"), 1)), 1) self.assertEqual( next(run(parse("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9"), 7)), 1) self.assertEqual( next(run(parse("3,3,1105,-1,9,1101,0,0,12,4,12,99,1"), 0)), 0) self.assertEqual( next(run(parse("3,3,1105,-1,9,1101,0,0,12,4,12,99,1"), 1)), 1) self.assertEqual( next(run(parse("3,3,1105,-1,9,1101,0,0,12,4,12,99,1"), 18)), 1) self.assertEqual( next( run( parse( "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31," "1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104," "999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99"), 1, )), 999, ) self.assertEqual( next( run( parse( "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31," "1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104," "999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99"), 7, )), 999, ) self.assertEqual( next( run( parse( "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31," "1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104," "999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99"), 8, )), 1000, ) self.assertEqual( next( run( parse( "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31," "1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104," "999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99"), 9, )), 1001, )
def print_image(panel, color=WHITE): values = list(panel.keys()) x_values = [c.real for c in values] y_values = [c.imag for c in values] max_width, min_width = max(x_values), min(x_values) max_height, min_height = max(y_values), min(y_values) width, height = int(max_width - min_width) + 1, int(max_height - min_height) print(width, height) # Reverse the axes for y in range(height, -1, -1): for x in range(width, 0, -1): position = x + min_width + 1j * y + (min_height * 1j) c = panel.get(position) e = "##" if c == color else " " end = "\n" if (x + 1) % width == 0 else "" print(e, end=end) if __name__ == "__main__": with open("../inputs/day11.input") as f: program = f.readline().strip() # Part I loop_run(parse(program), BLACK) # 2238 # Part II panel = loop_run(parse(program), WHITE) print_image(panel) # PKFPAZRP