示例#1
0
def solve2():
    opcodes[0] = "2"
    intCode = IntCode(opcodes)
    intCode.running = True
    input = 0
    score = 0
    tiles = defaultdict(list)
    paddle = ball = False

    while intCode.running:
        x = intCode.run(input)
        y = intCode.run(input)
        id = intCode.run(input)
        if x == -1 and y == 0:
            score = id
        else:
            if id == 3:
                paddle = True
            elif id == 4:
                ball = True
            elif id == 0:
                for i in range(5):
                    if (x, y) in tiles[i]:
                        tiles[i].remove((x, y))
                        if i == 3: paddle = False
                        if i == 4: ball = False
            tiles[id].append((x, y))

            if paddle and ball:
                input = 0
                if tiles[3][0][0] < tiles[4][0][0]:
                    input = 1
                elif tiles[3][0][0] > tiles[4][0][0]:
                    input = -1
    print(score)
def test_1():
    program = [
        109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0,
        99
    ]
    L = []
    machine = IntCode(program, output=L.append)
    machine.run()
    assert L == program
示例#3
0
def solve1():
    intCode = IntCode(opcodes)
    intCode.running = True
    c = 0
    while intCode.running:
        x = intCode.run(0)
        y = intCode.run(0)
        id = intCode.run(0)
        if id == 2:
            c += 1
    print(c)
def test_day5_2(in_, out):
    program = [
        3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
        1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999,
        1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99
    ]

    L = []
    machine = IntCode(program, output=L.append, get_input=lambda: in_)
    machine.run()
    output, = L
    assert output == out
示例#5
0
def paint2():
    intComputer = IntCode(opcodes.split(','))
    panel = defaultdict(int)

    d = 0
    p = (0,0)
    panel[p] = 1

    intComputer.running = True

    while intComputer.running:
        color = intComputer.run(panel[p])
        direction = intComputer.run(panel[p])

        if not intComputer.running:
            break

        panel[p] = color
        
        d = turn(d, direction)
        p = move(p, d)

    return panel
def test_day5():
    program = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]
    L = []
    machine = IntCode(program, output=L.append, get_input=lambda: 8)
    machine.run()
    assert L[0] == 1
def test_3():
    program = [104, 1125899906842624, 99]
    L = []
    machine = IntCode(program, output=L.append)
    machine.run()
    assert L[0] == 1125899906842624
def test_2():
    program = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
    L = []
    machine = IntCode(program, output=L.append)
    machine.run()
    assert len(str(L[0])) == 16