def test_jump_false(self):
     #                      0  1  2   3    4  5   6  7  8
     out = IntcodeComp([], [5, 7, 8, 99, 104, 1, 99, 1, 4]).run()
     self.assertEqual(out, 1)
     #                      0    1    2  3  4   5    6   7  8   9 10
     out = IntcodeComp([], [109, 2, 2205, 7, 8, 99, 104, 1, 99, 0, 4]).run()
     self.assertEqual(out, 1)
 def test_rb_offset(self):
     #                      0  1      2  3  4  5    6  7   8  9 10 11 12
     out = IntcodeComp([], [9, 12, 22208, 7, 8, 9, 204, 9, 99, 1, 1, 0, 2]).run()
     self.assertEqual(out, 1)
     #                      0    1      2  3  4  5    6  7   8  9 10 11
     out = IntcodeComp([], [109, 2, 22208, 7, 8, 9, 204, 9, 99, 1, 1, 0]).run()
     self.assertEqual(out, 1)
 def test_eq(self):
     #                      0  1  2  3  4  5   6  7  8  9
     out = IntcodeComp([], [8, 7, 8, 9, 4, 9, 99, 1, 1, 0]).run()
     self.assertEqual(out, 1)
     #                      0    1      2  3  4  5    6  7  8  9 10 11
     out = IntcodeComp([], [109, 2, 22208, 7, 8, 9, 204, 9, 99, 1, 1, 0]).run()
     self.assertEqual(out, 1)
Example #4
0
def test_settings(a, b, c, d, e):
    A = IntcodeComp([a, 0], mem).run()
    B = IntcodeComp([b, A], mem).run()
    C = IntcodeComp([c, B], mem).run()
    D = IntcodeComp([d, C], mem).run()
    E = IntcodeComp([e, D], mem).run()
    return E
def run_program(inputs, prog):
        outputs = []
        comp = IntcodeComp(inputs, prog)
        while True:
            out = comp.run()
            if out is True:
                break
            outputs.append(out)
        return outputs
Example #6
0
def play_game(joy_input, program):
    # Setup play field
    width, height = 38, 21
    area = [0] * width * height

    # load game with joy input
    game = IntcodeComp(joy_input, program)

    solved = False
    scores = [0]  # score history
    bidxs = []  # ball indexes
    pidxs = []  # paddle indexes

    while True:
        # wait for screen to initialize
        if 3 in area and 4 in area:

            draw_area(area, width, height)
            print("[ %d ]" % (scores[0], ))

            if not 2 in area:
                solved = True

            bidxs.append(area.index(4) % width)
            bidxs = bidxs[-4:]
            pidxs.append(area.index(3) % width)
            pidxs = pidxs[-4:]

        # one program cycle
        x = game.run()
        if x is True:
            if solved:
                return True, scores[0]
            else:
                return False, (len(joy_input), bidxs[0], pidxs[0], scores[0])
        y = game.run()
        if y is True:
            if solved:
                return True, scores[0]
            else:
                return False, (len(joy_input), bidxs[0], pidxs[0], scores[0])
        z = game.run()
        if z is True:
            if solved:
                return True, scores[0]
            else:
                return False, (len(joy_input), bidxs[0], pidxs[0], scores[0])

        # condition for score update
        if x == -1 and y == 0:
            scores.append(z)
            scores = scores[-1:]
            continue

        # regular screen output
        pos = get_xy_index(x, y, width)
        area[pos] = z
 def test_mul(self):
     out = IntcodeComp([], [2, 7, 8, 9, 4, 9, 99, 2, 4, 0]).run()
     self.assertEqual(out, 8)
     out = IntcodeComp([], [1102, 4, 5, 7, 4, 7, 99, 0]).run()
     self.assertEqual(out, 20)
     # rb + 3 (immediate)
     # add 4*5 (immediate)
     # save to 6 + 3 (rb) -> 9
     # return position 9
     out = IntcodeComp([], [109, 3, 21102, 4, 5, 6, 4, 9, 99, 0]).run()
     self.assertEqual(out, 20)
 def test_add(self):
     out = IntcodeComp([], [1, 7, 8, 9, 4, 9, 99, 2, 4, 0]).run()
     self.assertEqual(out, 6)
     out = IntcodeComp([], [1101, 4, 5, 7, 4, 7, 99, 0]).run()
     self.assertEqual(out, 9)
     # rb + 3 (immediate)
     # add 4+5 (immediate)
     # save to 6 + 3 (rb) -> 9
     # return position 9
     out = IntcodeComp([], [109, 3, 21101, 4, 5, 6, 4, 9, 99, 0]).run()
     self.assertEqual(out, 9)
Example #9
0
def solve(program, inputs):
    prog = IntcodeComp(inputs, program)
    results = []
    o = False
    while True:
        o = prog.run()
        if o is True:
            break
        results.append(o)

    return results
Example #10
0
    def __init__(self, program, width, height):

        self.painted = set()

        self.height = height
        self.width = width

        # 0 is black, 1 is white
        self.area = [0] * width * height

        # The robot should start in the middle of the area
        self.position = int(floor(width * height / 2) - width / 2 - 1)

        # 0 up, 1 right, 2 down, 3 left
        self.direction = 0

        self.program = IntcodeComp([], program)
Example #11
0
 def test_examplse(self):
     # inp eq 8 position mode
     out = IntcodeComp([8], [3,9,8,9,10,9,4,9,99,-1,8]).run()
     self.assertEqual(out, 1)
     # inp lt 8 position mode
     out = IntcodeComp([7], [3,9,7,9,10,9,4,9,99,-1,8]).run()
     self.assertEqual(out, 1)
     # inp lt 8 position mode
     out = IntcodeComp([7], [3,9,7,9,10,9,4,9,99,-1,8]).run()
     self.assertEqual(out, 1)
     # inp eq 8 immediate mode
     out = IntcodeComp([9], [3,3,1108,-1,8,3,4,3,99]).run()
     self.assertEqual(out, 0)
     # lt 8 immediate mode
     out = IntcodeComp([9], [3,3,1107,-1,8,3,4,3,99]).run()
     self.assertEqual(out, 0)
     # produce self as output
     out = run_program([], [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99])
     self.assertEqual(out, [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99])
     # return 16 digit number
     out = IntcodeComp([], [1102,34915192,34915192,7,4,7,99,0]).run()
     self.assertEqual(len(str(out).strip()), 16)
     # return large number in the middle
     out = IntcodeComp([], [104,1125899906842624,99]).run()
     self.assertEqual(out, 1125899906842624)
Example #12
0
def solve(program, phases):
    results = []
    a, b, c, d, e = phases
    amps = [
        IntcodeComp([a], program),
        IntcodeComp([b], program),
        IntcodeComp([c], program),
        IntcodeComp([d], program),
        IntcodeComp([e], program)
    ]

    o = 0
    while o is not True:
        for i, amp in enumerate(amps):
            o = amp.run([o])
            if o is True:
                return results[-1]
            if i == 4:
                results.append(o)
Example #13
0
 def test_exit(self):
     out = IntcodeComp(
             [],
             [99]
     ).run()
     self.assertTrue(out)
Example #14
0
 def test_read_write(self):
     out = IntcodeComp([4], [3, 5, 4, 5, 99, 0]).run()
     self.assertEqual(out, 4)
     # with rb
     out = IntcodeComp([4], [109, 2, 203, 5, 204, 5, 99, 0]).run()
     self.assertEqual(out, 4)
Example #15
0
class Robot(object):

    def __init__(self, program, width, height):

        self.painted = set()

        self.height = height
        self.width = width

        # 0 is black, 1 is white
        self.area = [0] * width * height

        # The robot should start in the middle of the area
        self.position = int(floor(width * height / 2) - width / 2 - 1)

        # 0 up, 1 right, 2 down, 3 left
        self.direction = 0

        self.program = IntcodeComp([], program)

    def up(self):
        self.position = self.position - self.width

    def right(self):
        self.position = self.position + 1

    def down(self):
        self.position = self.position + self.width

    def left(self):
        self.position = self.position - 1

    def __get_xy_index(self, x, y):
        return (self.width * y) + x

    def draw_area(self):
        system("clear")
        draw = ""
        for i, v in enumerate(self.area):
            if (i % self.width) == 0:
                draw += "\n"
            if i == self.position:
                draw += {
                    0: "^",
                    1: ">",
                    2: "<",
                    3: "v"
                }[self.direction]
            elif v == 0:
                draw += "."
            else:
                draw += "#"
        print(draw)

    def turn(self, direction):
        if direction == 0:
            self.direction -= 1  # left
        else:
            self.direction += 1  # right

        if self.direction < 0:
            self.direction = 3
        if self.direction > 3:
            self.direction = 0

    def move(self):
        """move one step in <direction>"""
        {
            0: self.up,
            1: self.right,
            2: self.down,
            3: self.left
        }[self.direction]()

    def run(self):
        o = False
        while True:
            o = self.program.run([self.area[self.position]])
            if o is True:
                break
            self.area[self.position] = o  # paint

            o = self.program.run()
            if o is True:
                break

            self.turn(o)
            self.move()

            self.painted.add(self.position)
        return self.painted
Example #16
0
        if (i % width) == 0:
            output += "\n"
        output += {
            0: " ",
            1: "#",
            2: "+",
            3: "―",
            4: "•"
        }[v]
    print(output + "\n")


width, height = 38, 21
area = [0] * width * height

game = IntcodeComp([], program)
artifacts = []
o = False
while True:
    o = game.run()
    if o is True:
        break
    artifacts.append(o)
    #pos = get_xy_index(x, y)

i = 0
while i+2 <= len(artifacts):
    pos = get_xy_index(artifacts[i], artifacts[i+1], width)
    area[pos] = artifacts[i+2]
    i += 3