Esempio n. 1
0
def both_parts(filename):
    f = open(filename, 'r')
    string = f.readline()
    f.close()

    state = State(string)
    return state.run()
Esempio n. 2
0
def day9a_test1():
    f = open('data/day9_test1.txt', 'r')
    string = f.readline()
    f.close()

    state = State(string)
    ret = state.run()
    print(state)
    return ret
Esempio n. 3
0
def day5_iotest():
    ''' io test: will output whatever it gets as input '''
    state = State("3,0,4,0,99")
    state.run()
Esempio n. 4
0
from intcode import State
string = "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,6,19,23,2,23,6,27,1,5,27,31,1,31,9,35,2,10,35,39,1,5,39,43,2,43,10,47,1,47,6,51,2,51,6,55,2,55,13,59,2,6,59,63,1,63,5,67,1,6,67,71,2,71,9,75,1,6,75,79,2,13,79,83,1,9,83,87,1,87,13,91,2,91,10,95,1,6,95,99,1,99,13,103,1,13,103,107,2,107,10,111,1,9,111,115,1,115,10,119,1,5,119,123,1,6,123,127,1,10,127,131,1,2,131,135,1,135,10,0,99,2,14,0,0"

for noun in range(0, 100):
    for verb in range(0, 100):
        state = State(string)
        state.mem[1] = noun
        state.mem[2] = verb
        result = state.run()
        if (result == 19690720):
            print(100 * noun + verb)
Esempio n. 5
0
def day5b():
    state = State(my_string)
    state.run()
Esempio n. 6
0
def both_parts(filename, part):
    f = open(filename, 'r')
    string = f.readline()
    f.close()

    input_pipe = []
    output_pipe = []

    loc = (0, 0)
    delta = (0, -1)
    squares = {}

    if part == 'b':
        squares[(0, 0)] = 1

    state = State(string, input_pipe=input_pipe, output_pipe=output_pipe)
    done = 0
    while not done:
        done = state.run_one_step()
        if state.waiting_for_input:
            if loc in squares:
                value = squares[loc]
            else:
                value = 0
            input_pipe.append(value)
        if len(output_pipe) >= 2:
            paint = output_pipe.pop(0)
            turn = output_pipe.pop(0)
            squares[loc] = paint
            if turn == 0:
                delta = rotate_left(delta)
            else:
                delta = rotate_right(delta)
            loc = step(loc, delta)

    if part == 'a':
        return len(squares)

    min_x = 0
    max_x = 0
    min_y = 0
    max_y = 0
    for loc in squares:
        min_x = min(min_x, loc[0])
        max_x = max(max_x, loc[0])
        min_y = min(min_y, loc[1])
        max_y = max(max_y, loc[1])

    for y in range(min_y, max_y + 1):
        for x in range(min_x, max_x + 1):
            if (x, y) in squares:
                val = squares[(x, y)]
            else:
                val = 0
            if val:
                print('#', end='')
            else:
                print('.', end='')
        print('')

    return 0
Esempio n. 7
0
def test_2a():
    ''' Given inputs to fuel_needed for day 1b '''
    state = State("1,9,10,3,2,3,11,0,99,30,40,50")
    assert (state.run() == 3500)
    state = State("1,0,0,0,99")
    assert (state.run() == 2)
    state = State("2,3,0,3,99")
    assert (state.run() == 2)
    state = State("2,4,4,5,99,0")
    assert (state.run() == 2)
    state = State("1,1,1,4,99,5,6,0,99")
    assert (state.run() == 30)
Esempio n. 8
0
from intcode import State
string = "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,6,19,23,2,23,6,27,1,5,27,31,1,31,9,35,2,10,35,39,1,5,39,43,2,43,10,47,1,47,6,51,2,51,6,55,2,55,13,59,2,6,59,63,1,63,5,67,1,6,67,71,2,71,9,75,1,6,75,79,2,13,79,83,1,9,83,87,1,87,13,91,2,91,10,95,1,6,95,99,1,99,13,103,1,13,103,107,2,107,10,111,1,9,111,115,1,115,10,119,1,5,119,123,1,6,123,127,1,10,127,131,1,2,131,135,1,135,10,0,99,2,14,0,0"

state = State(string)
state.mem[1]=12
state.mem[2]=2
print (state.run())
Esempio n. 9
0
def both_parts(filename, part):
    # Implemented a very naive (and inefficient) breadth-first search where
    # we go back to the original square all the time. Easy to code, but various
    # improvements possible
    f = open(filename, 'r')
    string = f.readline()
    f.close()

    input_pipe = []
    output_pipe = []

    state = State(string, input_pipe=input_pipe, output_pipe=output_pipe)

    squares = {}

    loc = (0, 0)
    squares[loc] = ()
    search_nodes = []

    forward_instructions = []
    return_instructions = []
    depth = 0
    oxygen_loc = (0, 0)

    done = False
    while not done:
        done = state.run_one_step()
        if state.waiting_for_input:
            if output_pipe and output_pipe[-1] == 2:
                # Done
                if part == 'a':
                    return depth
                else:
                    oxygen_loc = loc
                    discover_square(squares, search_nodes, loc,
                                    forward_instructions)
                    input_pipe.extend(return_instructions)
            elif loc == (0, 0) or output_pipe[-1] == 1:
                # Either start or we've found the new square
                discover_square(squares, search_nodes, loc,
                                forward_instructions)
                input_pipe.extend(return_instructions)
            else:
                print("{} is a wall".format(loc))
                # New square is a wall
                squares[loc] = 1
                input_pipe.extend(return_instructions[1:])
            while (output_pipe):
                output_pipe.pop()
            if search_nodes:
                node = search_nodes.pop(0)
                forward_instructions = node.instructions
                depth = len(forward_instructions)
                input_pipe.extend(node.instructions)
                loc = node.loc
                return_instructions = node.get_reverse_instructions()
            else:
                done = True

    done = False
    loc = oxygen_loc
    squares[loc] = -1
    discover_square_part_b(squares, search_nodes, loc, [])
    min_ = 0
    while not done:
        print("looping with {}".format(loc))
        if not search_nodes and not loc == oxygen_loc:
            done = True
        else:
            node = search_nodes.pop(0)
            loc = node.loc
            min_ = min(min_, squares[loc])
            discover_square_part_b(squares, search_nodes, loc, [])

    return -min_ - 1