import intcode_computer as ic from collections import defaultdict, deque puzzle_input = ic.load_input('15') class repairDroid(ic.IntcodeComputer): def __init__(self, *args, **kwargs): super(repairDroid, self).__init__(*args, **kwargs) self.available_moves = defaultdict((lambda: [1, 2, 3, 4]), {(0, 0): [1, 2, 3, 4]}) self.position = (0, 0) self.oxygen_system = None self.area = [(0, 0)] def opposite_direction(self, direction): direction_dict = {1: 2, 2: 1, 3: 4, 4: 3} return direction_dict[direction] def calc_new_pos(self, direction): new_position = None if direction == 1: new_position = (self.position[0], self.position[1] + 1) elif direction == 2: new_position = (self.position[0], self.position[1] - 1) elif direction == 3: new_position = (self.position[0] - 1, self.position[1]) elif direction == 4: new_position = (self.position[0] + 1, self.position[1]) return new_position
if not any(comp.incoming for comp in computers.values()): time.sleep( 3) #Three seconds of no new packets means network's idle. if not any(comp.incoming for comp in computers.values()): if last_NAT_message['Y'] == NAT_packet['Y']: print('Part 2:', NAT_packet['Y']) network_up = False break computers[0].input = [ value for _, value in NAT_packet.items() ][1:] last_NAT_message = NAT_packet network_up = True puzzle_input = ic.load_input('23') NAT_packet = None #Dictionary used to read and send packets among threads. computers = {i: netComputer(puzzle_input, True, True) for i in range(50)} for addr, _ in computers.items(): computers[addr].input = [addr, -1] threads = list() for i in range(50): new_thread = threading.Thread(target=run_pc, args=( computers[i], i, )) new_thread.daemon = True new_thread.start()
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 from itertools import permutations puzzle_input = ic.load_input('7') 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 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()
import intcode_computer as ic import numpy as np from PIL import Image from collections import defaultdict puzzle_input = ic.load_input('11') class paintingRobot(ic.IntcodeComputer): def __init__(self, *args, **kwargs): super(paintingRobot, self).__init__(*args, **kwargs) self.panels = defaultdict(lambda: '.') self.position = (0, 0) self.direction = 90 def restart(self, *args, **kwargs): super(paintingRobot, self).restart(*args, **kwargs) self.panels = defaultdict(lambda: '.') self.position = (0, 0) self.direction = 90 def add_panel(self, color): self.panels[self.position] = color def move(self, turn): movements_dict = { 0: lambda x: (x[0] + 1, x[1]), 90: lambda x: (x[0], x[1] + 1), 180: lambda x: (x[0] - 1, x[1]), 270: lambda x: (x[0], x[1] - 1), }
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 intcode_computer as ic puzzle_input = ic.load_input('21') class springDroid(ic.IntcodeComputer): def __init__(self, *args, **kwargs): super(springDroid, self).__init__(*args, **kwargs) self.scaffolds = dict() self.path = '' droid = springDroid(puzzle_input) walk_program = '''NOT C T OR T J NOT A T OR T J AND D J WALK''' run_program = '''NOT B J NOT C T OR T J AND D J AND H J NOT A T OR T J RUN''' droid.input = droid.list2ASCII(walk_program)