Beispiel #1
0
def find_change_along_x(program, x_lb, x_ub, y):
    p = IntCode(program)
    lb_value, _ = p.run_io([x_lb, y])

    while x_ub - x_lb > 1:
        x_search = math.floor((x_ub + x_lb) / 2)
        p = IntCode(program)
        out, _ = p.run_io([x_search, y])
        # print('x_search', x_search, out)
        if out == lb_value:
            x_lb = x_search
        else:
            x_ub = x_search

    return x_lb, x_ub
Beispiel #2
0
def find_change_along_y(program, x, y_lb, y_ub):
    p = IntCode(program)
    lb_value, _ = p.run_io([x, y_lb])

    while y_ub - y_lb > 1:
        y_search = math.floor((y_ub + y_lb) / 2)
        p = IntCode(program)
        out, _ = p.run_io([x, y_search])
        # print('y_search', y_search, out)
        if out == lb_value:
            y_lb = y_search
        else:
            y_ub = y_search

    return y_lb, y_ub
Beispiel #3
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    total_affected = 0
    for y in range(50):
        for x in range(50):
            p = IntCode(raw_program)
            p.inputs = [x, y]
            out, done = p.run_io([x, y])
            total_affected += out

    return total_affected
Beispiel #4
0
def find_max_output(string_program):
    raw_program = list(map(lambda x: int(x), string_program.split(',')))

    max_output = 0
    perms = itertools.permutations(range(5))
    for perm in perms:
        output = 0
        for phase in perm:
            p = IntCode(raw_program, [phase])
            output, _ = p.run_io(output)

        max_output = max(output, max_output)

    return max_output
Beispiel #5
0
def find_beam_boundary(program, y):
    x_max = 10000
    x_lb = 0
    x_ub = x_max
    x_search_step = math.floor(y / 20)
    x_search = 0
    while True:
        # print('x_search', x_search)
        if x_search > x_max:
            break
        p = IntCode(program)
        out, _ = p.run_io([x_search, y])
        if out:
            x_lb = x_search
        if x_lb and not out:
            x_ub = x_search
            break
        x_search += x_search_step

    return x_lb, x_ub
Beispiel #6
0
def find_max_output(string_program):
    raw_program = list(map(lambda x: int(x), string_program.split(',')))

    max_output = 0
    perms = itertools.permutations(range(5, 10))
    for perm in perms:
        input = 0
        programs = []
        for ix, phase in enumerate(perm):
            p = IntCode(raw_program, [phase])
            input, _ = p.run_io(input)
            programs.append(p)

        done = False
        while not done:
            for ix, phase in enumerate(perm):
                input, done = programs[ix].run_io(input)

        round_output = input
        print(round_output)

        max_output = max(round_output, max_output)

    return max_output
Beispiel #7
0
import sys

sys.path.append('..')
from shared.intcode import IntCode

with open('input.txt', 'r') as f:
    for line in f:
        content = line.strip('\n')
        program = list(map(lambda x: int(x), content.split(',')))
        dict_program = dict(x for x in enumerate(program))

p = IntCode(program)
output, _ = p.run_io(5)
print(output)