def max_signal(puzzle_input): phases = permutations([0, 1, 2, 3, 4]) amp = ic.IntcodeComputer(puzzle_input) values = [] for phase in phases: last_output = 0 for i in range(5): amp.input = [phase[i], last_output] amp.process_intcode() last_output = amp.output[-1] amp = ic.IntcodeComputer(puzzle_input) values.append(last_output) return max(values)
def find_input(puzzle_input, output): for j in range(100): for i in range(100): puzzle_input[1] = i puzzle_input[2] = j computer = ic.IntcodeComputer(puzzle_input) computer.process_intcode() if computer.puzzle_input[0] == 19690720: return i * 100 + j
def max_signal_feedback(puzzle_input): phases = permutations([5, 6, 7, 8, 9]) values = [] for phase in phases: amps = [ic.IntcodeComputer(puzzle_input, True) for i in range(5)] last_output = 0 for i in range(5): amps[i].input = [phase[i], last_output] amps[i].process_intcode() last_output = amps[i].output[-1] while all(x.turned_on for x in amps): for i in range(5): amps[i].input = [last_output] amps[i].process_intcode() last_output = amps[i].output[-1] values.append(last_output) return max(values)
#!/usr/bin/env python # vim:set fileencoding=utf8: # import sys import intcode_computer with open(sys.argv[1], 'r') as f: program = f.read() computer = intcode_computer.IntcodeComputer(program) computer.execute()
from itertools import count import intcode_computer as ic puzzle_input = ic.load_input('19') drone = ic.IntcodeComputer(puzzle_input) def count_beam_size(x_max, y_max): area = '' x_min = 0 for y in range(y_max): for x in range(x_min, x_max): #Check only segments with '#', not whole line. drone.input = [x, y] drone.process_intcode() if drone.output[-1] == 0: if area[-1] == '#': drone.restart() break area += '.' else: if area: if area[-1] != '#': x_min = x area += '#' drone.restart() area += '\n' return area.count('#')
import intcode_computer as ic def find_input(puzzle_input, output): for j in range(100): for i in range(100): puzzle_input[1] = i puzzle_input[2] = j computer = ic.IntcodeComputer(puzzle_input) computer.process_intcode() if computer.puzzle_input[0] == 19690720: return i * 100 + j puzzle_input = ic.load_input('2') computer = ic.IntcodeComputer(puzzle_input) computer.process_intcode() OUTPUT = 19690720 print('Part 1:', computer.puzzle_input[0]) print('Part 2:', find_input(puzzle_input, OUTPUT))
import argparse import sys import intcode_computer parser = argparse.ArgumentParser() parser.add_argument('--part1', action='store_true') parser.add_argument('--part2', action='store_true') parser.add_argument('file_name') args = parser.parse_args() with open(args.file_name, 'r') as f: program = f.read() amps = [] for i in range(5): amps.append(intcode_computer.IntcodeComputer(program, True)) if args.part1: max_to_thrusters = [] for a in range(5): amps[0].reset() amps[0].set_input([a, 0]) amps[0].execute() for b in range(5): if b == a: continue amps[1].reset() amps[1].set_input([b, amps[0].output]) amps[1].execute() for c in range(5): if c in [a, b]:
for k, v in o.items(): if self._area.get((k[0] + 1, k[1]), Cell(0)).content == '.': self._area[k[0] + 1, k[1]].content = 'O' if self._area.get((k[0] - 1, k[1]), Cell(0)).content == '.': self._area[k[0] - 1, k[1]].content = 'O' if self._area.get((k[0], k[1] + 1), Cell(0)).content == '.': self._area[k[0], k[1] + 1].content = 'O' if self._area.get((k[0], k[1] - 1), Cell(0)).content == '.': self._area[k[0], k[1] - 1].content = 'O' minutes += 1 return minutes parser = argparse.ArgumentParser() parser.add_argument('file_name') args = parser.parse_args() with open(args.file_name, 'r') as f: program = f.read() comp = intcode_computer.IntcodeComputer(program, True) area = Area(comp) area.search(0, 0) # Switch to filling oxygen. print(f'Part 2 Answer: {area.fill()}')
import intcode_computer as ic import re puzzle_input = ic.load_input('25') droid = ic.IntcodeComputer(puzzle_input, True, True) right_answer = '''east take sand west west north take wreath east take fixed point west south south east take escape pod west south east east south take photons west south east east east take space law space brochure
import intcode_computer as ic from collections import defaultdict puzzle_input = ic.load_input('13') arcade = ic.IntcodeComputer(puzzle_input) arcade.process_intcode() arcade_output = arcade.output count = 0 for i in range(2, len(arcade_output), 3): if arcade_output[i] == 2: count += 1 print('Part 1:', count) puzzle_input[0] = 2 arcade.restart(puzzle_input) arcade.toggle_feedback() ball_pos = None paddle_pos = None score = 0 arcade.input = [0] while arcade.turned_on: arcade.process_intcode() x = arcade.output[-1] arcade.process_intcode() y = arcade.output[-1]
import sys import intcode_computer def convert_program_file_to_list(file_name): with open(file_name) as f: return list(map(int, f.read().split(','))) raw_memory = convert_program_file_to_list(sys.argv[1]) computer = intcode_computer.IntcodeComputer(raw_memory) computer.run_program()