예제 #1
0
 def __init__(self, fn, nad):
     self.nad = nad
     program = ic.input_to_program(fn)
     self.computer = ic.Computer(program, input_val=nad)
     self.input_queue = []
     self.first_input = True
     self.output_counter_cycle = cycle([0, 1, 2])
     self.output_counter = next(self.output_counter_cycle)
     self.current_output = None
     self.last_action_receive = True
     self.last_receive_count = 0
예제 #2
0
        for row in output_arr:
            print(''.join(row))


def tiles_to_dict(tile_list):
    tile_dict = {}
    for t in tile_list:
        tile_dict[(t[0], t[1])] = t[2]
    return tile_dict


print('Part A')
fn = '13.txt'
program = ic.input_to_program(fn)
input_val = 0
c = ic.Computer(program, input_val)

tiles = []
output_counters = cycle(range(3))
output_counter = next(output_counters)
while c.opcode != 99:
    c.run_op(verbose=False)
    if c.opcode == 4:
        if output_counter == 0:
            tile = [c.output_val]
            # output_counter = next(output_counters)
        elif output_counter == 1:
            tile.append(c.output_val)
            # output_counter = next(output_counters)
        elif output_counter == 2:
            tile.append(c.output_val)
예제 #3
0
import sys

sys.path.append('../')
import intcode2019 as ic

fn = '02.txt'

# Part A
print('Part A')
p = ic.input_to_program(fn)
c = ic.Computer(p)
noun, verb = 12, 2
c.program[1] = noun
c.program[2] = verb
while c.opcode != 99:
    c.run_op(verbose=False)
print(c.program[0])

# Part B
print('Part B')
for noun in range(100):
    for verb in range(100):
        p = ic.input_to_program(fn)
        c = ic.Computer(p)
        c.program[1] = noun
        c.program[2] = verb
        while c.opcode != 99:
            c.run_op(verbose=False)
        if c.program[0] == 19690720:
            answer = 100 * noun + verb
예제 #4
0
import intcode2019 as ic
from itertools import permutations, cycle
# from collections import deque
import numpy as np

fn = '07.txt'
program = ic.input_to_program(fn)

amp_cycle = cycle(range(5))
previous_amp = dict(zip((0, 1, 2, 3, 4), (4, 0, 1, 2, 3)))

# Part A
permute_phases = permutations(range(5))
output_vals = []
for phases in permute_phases:
    amps = dict(zip(range(5), [ic.Computer(program.copy()) for _ in range(5)]))
    amp_no = next(amp_cycle)
    # Initialize computer inputs with the phase settings
    for c, p in zip(amps.keys(), phases):
        amps[c].input_val = p
        amps[c].use_phase = True
    # Run each amp once
    for amp_no, current_c in amps.items():
        while current_c.opcode != 99:
            current_c.run_op(verbose=False)
            # Switch from phase to input
            # on the first instance of opcode 3
            if current_c.opcode == 3 and current_c.use_phase:
                current_c.use_phase = False
                if amp_no == 0:
                    current_c.input_val = 0
예제 #5
0
def initialize_computer(fn):
    program = ic.input_to_program(fn)
    c = ic.Computer(program)
    return c
예제 #6
0
    return input_ascii


def output_to_str(output_ascii, print_switch=True):
    output_str = ''
    for a in output_ascii:
        output_str += chr(a)
    return output_str
    if print_switch:
        print(output_str)


print('Part A')
fn = '21.txt'
program = ic.input_to_program(fn)
c = ic.Computer(program)
output_ascii = []

input_strs = ['NOT A J',
              'NOT B T',
              'NOT C T',
              'OR T J',
              'AND D J',
              'WALK']
input_ascii = input_to_ascii(input_strs)
input_ind = 0

while c.opcode != 99:
    c.run_op()
    if c.opcode == 4:
        output_ascii.append(c.output_val)
예제 #7
0
computers = dict()
input_queues = dict()
input_from_queue = dict()
first_input = dict()
output_counter_cycles = dict()
output_counter = dict()
current_output = dict()
last_action_receive = dict()
last_receive_count = dict()

nat_vals = [-1, -1]

for nad in range(50):
    program = ic.input_to_program(fn)
    computers[nad] = ic.Computer(program, input_val=nad)
    input_queues[nad] = []
    input_from_queue[nad] = False
    first_input[nad] = True
    output_counter_cycles[nad] = cycle([0, 1, 2])
    output_counter[nad] = next(output_counter_cycles[nad])
    current_output[nad] = None
    last_action_receive[nad] = True
    last_receive_count[nad] = 0

y_vals = set()
end_cond = False
while not end_cond:
    # check for NAT conditions
    if (min(last_receive_count.values()) > 10
            and max([len(q) for q in input_queues.values()]) == 0):
예제 #8
0
import sys
sys.path.append('../')
import intcode2019 as ic

fn = '05_test1.txt'
fn = '05.txt'

print('Part A')
p = ic.input_to_program(fn)
input_val = 1
c = ic.Computer(p, input_val)

while c.opcode != 99:
    c.run_op(verbose=False)
print(c.output_val)

print('Part B')
p = ic.input_to_program(fn)
input_val = 5
c = ic.Computer(p, input_val)

while c.opcode != 99:
    c.run_op(verbose=False)
print(c.output_val)