def solve_day_05(input): regex = re.compile("-{0,1}\d+") f = open(input, "r") computer = IntCodeComputer(regex.findall(f.readline())) computer.write(1) computer.run() p1 = computer.read()[-1] computer.reset() computer.write(5) computer.run() p2 = computer.read()[0] return (p1, p2)
class Amplifier: def __init__(self, stream): self.computer = IntCodeComputer(stream) def output(self): return self.computer.output def run(self, input_queue): self.computer.input_queue += input_queue self.computer.run() def reset(self): self.computer.reset()
def part_1(intcode): p1 = 0 computer = IntCodeComputer(intcode) for perm in permutations([0, 1, 2, 3, 4]): computer.write([perm[0], 0]) computer.run() output = computer.read()[0] computer.reset() for p in perm[1:]: computer.write([p, output]) computer.run() output = computer.read()[0] computer.reset() if p1 < output: p1 = output return p1
def solve_day_19(input): regex = re.compile("-{0,1}\d+") f = open(input, "r") computer = IntCodeComputer(regex.findall(f.readline())) size = 50 p1 = 0 for x in range(size): for y in range(size): # I have no clue why I need three inputs, but I do computer.write([0, x, y]) computer.run() result = computer.read()[0] p1 += result computer.reset() print(result, end="") print() return (p1, 0)
def solve_day_13(input): regex = re.compile("-{0,1}\d+") f = open(input, "r") computer = IntCodeComputer(regex.findall(f.readline())) ts = play(computer, {}) tiles = ts[0] score = ts[1] p1 = sum(value == 2 for value in tiles.values()) print_arcade(tiles) computer.reset() computer.memory[0] = 2 while computer.status != Status.TERMINATED: computer.write(get_joystick(tiles)) ts = play(computer, tiles) tiles = ts[0] score = ts[1] # print_arcade(tiles) return (p1, score)
def solve_day_02(input): regex = re.compile("\d+") f = open(input, "r") computer = IntCodeComputer(regex.findall(f.readline())) computer.run() p1 = computer.memory[0] p2 = 0 has_found = False for noun in range(99): for verb in range(99): computer.reset() computer.memory[1] = noun computer.memory[2] = verb computer.run() if computer.memory[0] == 19690720: p2 = 100 * noun + verb has_found = True break if has_found: break return (p1, p2)
import aoc_download from intcode_computer import IntCodeComputer YEAR = 2019 DAY = 9 puzzle_input = aoc_download.aoc.puzzle_input_file(YEAR, DAY) computer = IntCodeComputer(puzzle_input) computer.input_queue.append(1) computer.run() print("part 1:", computer.output) computer.reset() computer.input_queue.append(2) computer.run() print("part 2:", computer.output)
def solve_day_17(input): regex = re.compile("-{0,1}\d+") f = open(input, "r") computer = IntCodeComputer(regex.findall(f.readline())) computer.run() camera = computer.read() lst = [] line = [] for c in camera: if c == 10: lst.append(line) line = [] else: line.append(c) p1 = 0 for y, cy in enumerate(lst[1:-2], 1): for x, cx in enumerate(cy[1:-1], 1): if cx == 35: if lst[y - 1][x] == 35 and lst[y + 1][x] == 35 and lst[y][ x - 1] == 35 and lst[y][x + 1] == 35: lst[y][x] = ord("O") p1 += x * y # This was just for pretty printing so see how things look, but I am now using it # to manually solve part 2 for cy in lst: for cx in cy: if cx == ord("."): print("⬛", end="") else: print("⬜", end="") print() # and after a little puzzling we get (see Scaffold.png) # "A"=65, "B"=66, "C"=67, ","=44, "\n"=10 # "L"=76, "R"=82 # "12"=49 50, "8"=56, "6"=54, "4"=52 # "n" = 110 # A = L12,L12,L6,L6 # B = L12,L6,R12,R8 # C = R8,R4,L12 # A,C,A,B,C,A,B,C,A,B computer.reset() computer.memory[0] = 2 instructions = [ #A , C , A , B , C , A , B , C , A , B \n 65, 44, 67, 44, 65, 44, 66, 44, 67, 44, 65, 44, 66, 44, 67, 44, 65, 44, 66, 10, #L , 1 2 , L , 1 2 , L , 6 , L , 6 \n 76, 44, 49, 50, 44, 76, 44, 49, 50, 44, 76, 44, 54, 44, 76, 44, 54, 10, #L , 1 2 , L , 6 , R , 1 2 , R , 8 \n 76, 44, 49, 50, 44, 76, 44, 54, 44, 82, 44, 49, 50, 44, 82, 44, 56, 10, #R , 8 , R , 4 , L , 1 2 \n 82, 44, 56, 44, 82, 44, 52, 44, 76, 44, 49, 50, 10, #n, \n 110, 10 ] computer.write(instructions) computer.run() p2 = computer.read()[-1] return (p1, p2)
def test_reset(self): computer = IntCodeComputer(self.regex.findall("1,1,1,4,99,5,6,0,99")) computer.run() computer.reset() self.assertEqual([1, 1, 1, 4, 99, 5, 6, 0, 99], computer.memory)