Exemplo n.º 1
0
def part_1():
    input_file = open("resources/07input.txt")
    my_prog = [int(a) for a in input_file.read().strip().split(",")]
    input_file.close()
    params1 = [0, 1, 2, 3, 4]
    param_sets = list(itertools.permutations(params1))

    highscore = 0

    for param_set in param_sets:
        input_vals = [0, 0]

        for phase in param_set:
            intcode = IntCode()
            input_vals[0] = phase
            intcode.input_buffer = input_vals[:]
            intcode.program = my_prog[:]
            intcode.run_program()

            if len(intcode.output_buffer) > 0:
                input_vals[1] = intcode.output_buffer[0]
                highscore = intcode.output_buffer[
                    0] if intcode.output_buffer[0] > highscore else highscore

    return highscore
Exemplo n.º 2
0
def run(prog, instr):
    intcode = IntCode(prog[:], [instr])
    intcode.running = True

    while intcode.running:
        intcode.run_program()

    return intcode.output_buffer[-1]
Exemplo n.º 3
0
def part_1():
    input_file = open("resources/02input.txt")
    prog = [int(a) for a in input_file.read().strip().split(",")]
    input_file.close()
    prog[1] = 12
    prog[2] = 2
    intcode = IntCode()
    intcode.program = prog[:]
    intcode.run_program()
    return intcode.program[0]
Exemplo n.º 4
0
def part_2():
    input_file = open("resources/09input.txt")
    my_prog = [int(a) for a in input_file.read().strip().split(",")]
    input_file.close()
    intcode = IntCode(my_prog, [2], 2000)
    intcode.running = True

    while intcode.running:
        intcode.run_program()

    # print(intcode.output_buffer)
    return intcode.output_buffer[0]
Exemplo n.º 5
0
def gravity_assist(program, expected_result):
    intcode = IntCode()

    for noun in range(0, 99):
        for verb in range(0, 99):
            testprog = program[:]
            testprog[1], testprog[2] = noun, verb
            intcode.program = testprog
            intcode.run_program()
            result = intcode.program

            if result[0] == expected_result:
                return (noun * 100) + verb

    return -1, -1