def part1(): ship = Ship(100, 100) coder = Day11Coder( IntCoder.extended_memory(IntCoder.read_file('./input.txt'), 1107), ship) covered = set() while step(coder): covered.add(ship.position) print(len(covered))
def part2(): ship = Ship(80, 10) ship.paint(1) coder = Day11Coder( IntCoder.extended_memory(IntCoder.read_file('./input.txt'), 851), ship) while step(coder): pass ship.grid.reverse() for line in ship.grid: chars = ['\u2588' if c == 1 else ' ' for c in line] chars.reverse() print(''.join(chars))
def __init__(self, program: List[int], address, queues: Dict[int, Deque[int]]): super().__init__(IntCoder.extended_memory(program, 10000)) self.address = address self.queues = queues self.out_cache = [] self.first_input = True
def test_day2_part2(self): memory = IntCoder.read_file('../day2/input.txt') memory[1] = 98 memory[2] = 20 coder = IntCoder(memory) coder.run() self.assertEqual(19690720, coder.peek_index(0))
def test_day2_part1(self): memory = IntCoder.read_file('../day2/input.txt') memory[1] = 12 memory[2] = 2 coder = IntCoder(memory) coder.run() self.assertEqual(2782414, coder.peek_index(0))
def test_day9_example(self): program = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99] self.__day9(IntCoder.extended_memory(program, 102), [], program) self.__day9([1102, 34915192, 34915192, 7, 4, 7, 99, 0], [], [1219070632396864]) self.__day9([104, 1125899906842624, 99], [], [1125899906842624])
def test_day2_example4(self): coder = IntCoder([2, 4, 4, 5, 99, 0]) coder.run() self.assertEqual([2, 4, 4, 5, 99, 9801], coder._memory)
def test_day2_example5(self): coder = IntCoder([1, 1, 1, 4, 99, 5, 6, 0, 99]) coder.run() self.assertEqual([30, 1, 1, 4, 2, 5, 6, 0, 99], coder._memory)
from IntCoder import IntCoder coder = IntCoder( IntCoder.extended_memory(IntCoder.read_file('./input.txt'), 4000)) coder.run() width = coder.output.index(10) + 1 lines = [coder.output[i:i + width] for i in range(0, len(coder.output), width)] def is_intersection(x, y): return lines[x][y] == 35 and lines[x - 1][y] == 35 and lines[ x + 1][y] == 35 and lines[x][y - 1] == 35 and lines[x][y + 1] == 35 result = 0 for x in range(1, len(lines) - 2): for y in range(1, len(lines[x]) - 1): if is_intersection(x, y): result += (x * y) print(result)
from IntCoder import IntCoder coder = IntCoder( IntCoder.extended_memory(IntCoder.read_file('input.txt'), 2659)) coder.run() outputs = [coder.output[i:i + 3] for i in range(0, len(coder.output), 3)] count = 0 for o in outputs: if o[2] == 2: count += 1 print(count)
def test_day5_part2(self): self.__day5(IntCoder.read_file('../day5/input.txt'), 5, 773660)
def run(input_value): memory = [int(i) for i in open('./input.txt', 'r').read().split(',')] coder = IntCoder(memory, [input_value]) coder.run() print(coder.output)
def test_day5_example1(self): coder = IntCoder([1002, 4, 3, 4, 33]) coder.run() self.assertEqual([1002, 4, 3, 4, 99], coder._memory)
def test_invalid_input(self): with self.assertRaises(ValueError): IntCoder([3, 1, 99]).run()
def test_day7_part1(self): self.__day7_part1(IntCoder.read_file('../day7/input.txt'), [3, 1, 4, 2, 0], 92663)
def test_day2_example1(self): coder = IntCoder([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]) coder.run() self.assertEqual([3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50], coder._memory)
import itertools from IntCoder import IntCoder from Day7Coder import Day7Coder memory = IntCoder.read_file('./input.txt') def run(seq): c1 = Day7Coder(memory, seq[0]) c2 = Day7Coder(memory, seq[1]) c3 = Day7Coder(memory, seq[2]) c4 = Day7Coder(memory, seq[3]) c5 = Day7Coder(memory, seq[4]) while c5.peek_pointer() != 99: c1.run() c2.run() c3.run() c4.run() c5.run() _max = 0 for sequence in itertools.permutations([5, 6, 7, 8, 9], 5): Day7Coder.state = 0 run(sequence) if Day7Coder.state > _max: _max = Day7Coder.state print(_max)
def run(input1, input2): memory = [int(i) for i in open('./input.txt', 'r').read().split(',')] memory[1] = input1 memory[2] = input2 IntCoder(memory).run() return memory[0]
y = self.out_cache[2] if address == 255: print(f'to nat: ({x}, {y})') # first print of this is part 1 Day23Coder.nat = (x, y) else: target_queue = self.queues[address] target_queue.append(x) target_queue.append(y) self.out_cache = [] def run(self) -> None: super().run() print(f'{self.address} finished') io = {i: deque() for i in range(50)} coders = [ Day23Coder(IntCoder.read_file('./input.txt'), i, io) for i in range(50) ] while True: all_empty = True for coder in coders: coder._step() all_empty &= len(io[coder.address]) == 0 if all_empty and Day23Coder.nat != (0, 0): print(f'sending nat {Day23Coder.nat}') # last print of this is part 2 io[0].append(Day23Coder.nat[0]) io[0].append(Day23Coder.nat[1]) Day23Coder.nat = (0, 0)
def test_day5_example2(self): coder = IntCoder([1101, 100, -1, 4, 0]) coder.run() self.assertEqual([1101, 100, -1, 4, 99], coder._memory)
def test_day9_part1(self): self.__day9(IntCoder.extended_memory(IntCoder.read_file('../day9/input.txt'), 1030), [1], [3839402290])
def test_day5_part1(self): self.__day5(IntCoder.read_file('../day5/input.txt'), 1, 3122865)
def test_day9_part2(self): self.__day9(IntCoder.extended_memory(IntCoder.read_file('../day9/input.txt'), 1077), [2], [35734])
from IntCoder import IntCoder memory = IntCoder.extended_memory(IntCoder.read_file('input.txt'), 1077) coder = IntCoder(memory, [1]) coder.run() print(coder.output) # part 1 coder = IntCoder(memory, [2]) coder.run() print(coder.output) # part 2
def __with_io(memory, input_values): coder = IntCoder(memory, input_values) coder.run() return coder.output
from IntCoder import IntCoder result = 0 program = IntCoder.read_file('./input.txt') for x in range(50): for y in range(50): coder = IntCoder(IntCoder.extended_memory(program, 10000), [x, y]) coder.run() result += coder.output[0] print(result)
def test_day2_example2(self): coder = IntCoder([1, 0, 0, 0, 99]) coder.run() self.assertEqual([2, 0, 0, 0, 99], coder._memory)
from IntCoder import IntCoder program = \ 'NOT C J\n' \ 'NOT B T\n' \ 'OR T J\n' \ 'NOT A T\n' \ 'OR T J\n' \ 'AND D J\n' \ 'WALK\n' coder = IntCoder( IntCoder.extended_memory(IntCoder.read_file('./input.txt'), 10000), [ord(c) for c in program]) coder.run() if len(coder.output) != 34: print(''.join([chr(n) for n in coder.output[:-1]])) # error else: print(coder.output[-1]) # answer
def test_day2_example3(self): coder = IntCoder([2, 3, 0, 3, 99]) coder.run() self.assertEqual([2, 3, 0, 6, 99], coder._memory)