Exemplo n.º 1
0
def run_program(base_ints, noun, verb):
    ints = base_ints.copy()
    ints[1] = noun
    ints[2] = verb
    ic = IntCode(ints)
    ic.run_through()
    return ic.memory[0]
Exemplo n.º 2
0
def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    ic = IntCode(memory)
    outputs = ic.run_through()
    draw_memory = list(zip(*[iter(outputs)] * 3))
    return sum(1 for t in draw_memory if t[2] == 2)
Exemplo n.º 3
0
def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    #jump if (not A) or (not C and D)
    instructions = ['OR A J', 'AND C J', 'NOT J J', 'AND D J', 'WALK']
    ic = IntCode(memory)
    for inst in instructions:
        ic.add_ascii_inputs(inst)
    return ic.run_through()[-1]
Exemplo n.º 4
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    instructions = [
        'OR A J', 'AND B J', 'AND C J', 'NOT J J', 'OR E T', 'OR H T',
        'AND T J', 'AND D J', 'RUN'
    ]
    ic = IntCode(memory)
    for inst in instructions:
        ic.add_ascii_inputs(inst)
    return ic.run_through()[-1]
Exemplo n.º 5
0
def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    #moves solved by hand for given input
    moves = 'A,B,A,A,B,C,B,C,C,B'
    func_a = 'L,12,R,8,L,6,R,8,L,6'
    func_b = 'R,8,L,12,L,12,R,8'
    func_c = 'L,6,R,6,L,12'
    memory[0] = 2
    ic = IntCode(memory)
    ic.add_ascii_inputs(moves)
    ic.add_ascii_inputs(func_a)
    ic.add_ascii_inputs(func_b)
    ic.add_ascii_inputs(func_c)
    ic.add_ascii_inputs('n')
    return ic.run_through()[-1]
Exemplo n.º 6
0
def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    ic = IntCode(memory)
    asc = ic.run_through()
    grid = defaultdict(lambda: '.')
    y = 0
    x = 0
    scaffolds = set()
    max_x = 0
    max_y = 0
    for n in asc:
        if n == 10:
            x = 0
            y += 1
            max_y = max(max_y, y)
        else:
            c = chr(n)
            grid[(x, y)] = c
            if c in '#^v<>':
                scaffolds.add((x, y))
            if c == '^':
                start = (x, y)
                start_dir = (0, -1)
            elif c == 'v':
                start = (x, y)
                start_dir = (0, 1)
            elif c == '>':
                start = (x, y)
                start_dir = (1, 0)
            elif c == '<':
                start = (x, y)
                start_dir = (-1, 0)
            x += 1
            max_x = max(max_x, x)
    res = 0
    for x, y in scaffolds:
        if (x + 1, y) in scaffolds and (x - 1, y) in scaffolds and \
           (x, y + 1) in scaffolds and (x, y - 1) in scaffolds:
            res += x * y
    path = find_path(grid, start, start_dir)
    return res, path