def __init__(self) -> None:
     with open_fixture("day15") as fp:
         data = decode(fp.readline())
     self.vm = IntcodeVM(data)
     self.grid = Grid()
     self.pos = Position(0, 0)
     self.oxygen_system_pos = Position(0, 0)
Exemple #2
0
 def test_part2_example2(self):
     data = decode(
         "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,"
         "-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,"
         "53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10"
     )
     self.assertEqual(compute_thrust(data, range(5, 10)), 18216)
Exemple #3
0
 def test_part2_example7(self):
     data = decode(
         "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"
     )
     for i in range(10):
         expect = 999 if i < 8 else 1000 if i == 8 else 1001
         stdout = run(data, [i])
         self.assertListEqual(stdout, [expect])
 def __init__(
     self,
     wake_up: bool = False,
 ):
     with open_fixture("day17") as fp:
         data = decode(fp.readline())
     self.vm = IntcodeVM(data)
     if wake_up:
         self.vm.data[0] = 2
Exemple #5
0
 def test_part2(self):
     expect = (" ████  ██   ██  ███  █  █ █  █ █    ███    \n"
               "    █ █  █ █  █ █  █ █  █ █ █  █    █  █   \n"
               "   █  █    █    █  █ ████ ██   █    ███    \n"
               "  █   █    █ ██ ███  █  █ █ █  █    █  █   \n"
               " █    █  █ █  █ █ █  █  █ █ █  █    █  █   \n"
               " ████  ██   ███ █  █ █  █ █  █ ████ ███    \n")
     with open_fixture("day11") as fp:
         data = decode(fp.readline())
     robot = Robot(data)
     robot.grid[(0, 0)] = Color.WHITE
     robot.run()
     actual = str(robot.grid)
     self.assertEqual(actual, expect)
Exemple #6
0
def play(program):
    code = decode(program)
    code[0] = 2
    state = [code, 0, 0, [0], [], False]
    score = 0
    while state[5] is False:
        state = resume(state)
        ball = 0
        paddle = 0
        for item in chunk(state[4], 3):
            if item[0] == -1 and item[1] == 0:
                score = item[2]
            elif item[2] == 4:
                ball = item[0]
            elif item[2] == 3:
                paddle = item[0]

        # just track the x-position of the ball
        state[3].append(cmp(paddle, ball))
        state[4] = []  # reset output buffer

    return score
Exemple #7
0
 def test_part2(self):
     with open_fixture("day07") as fp:
         data = decode(fp.readline())
     self.assertEqual(compute_thrust(data, range(5, 10)), 12932154)
 def test_part1_example3(self):
     data = decode("104,1125899906842624,99")
     vm = IntcodeVM(data)
     vm.run()
     self.assertEqual(vm.stdout[0], 1125899906842624)
Exemple #9
0
 def test_part1(self):
     with open_fixture("day13") as fp:
         data = decode(fp.readline())
     cab = ArcadeCabinet(data)
     cab.run()
     self.assertEqual(cab.init_block_count, 420)
Exemple #10
0
 def test_part2_example5(self):
     data = decode("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
     for i in range(10):
         stdout = run(data, [i])
         self.assertListEqual(stdout, [0 if i == 0 else 1])
Exemple #11
0
 def test_part2_example3(self):
     data = decode("3,3,1108,-1,8,3,4,3,99")
     for i in range(10):
         stdout = run(data, [i])
         self.assertListEqual(stdout, [1 if i == 8 else 0],
                              f"Failed with input: {i}")
Exemple #12
0
 def test_part1(self):
     with open_fixture("day11") as fp:
         data = decode(fp.readline())
     robot = Robot(data)
     robot.run()
     self.assertEqual(len(robot.grid), 1747)
Exemple #13
0
 def test_part1_example1(self):
     data = decode(
         "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99")
     vm = IntcodeVM(data)
     vm.run()
     self.assertListEqual(vm.stdout, data)
Exemple #14
0
 def test_part2(self):
     with open_fixture("day09") as fp:
         data = decode(fp.readline())
     vm = IntcodeVM(data, [2])
     vm.run()
     self.assertListEqual(vm.stdout, [49122])
Exemple #15
0
def run_file(name: str, stdin: Optional[Data] = None) -> Data:
    with open_fixture(name) as fp:
        data = decode(fp.readline())
    return run(data, stdin=stdin)
Exemple #16
0
 def test_part2_example2(self):
     data = decode("3,9,7,9,10,9,4,9,99,-1,8")
     for i in range(10):
         stdout = run(data, [i])
         self.assertListEqual(stdout, [1 if i < 8 else 0])
Exemple #17
0
 def test_part1_example1(self):
     data = decode("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")
     self.assertEqual(compute_thrust(data, range(5)), 43210)
Exemple #18
0
 def test_part2_example4(self):
     data = decode("3,3,1107,-1,8,3,4,3,99")
     for i in range(10):
         stdout = run(data, [i])
         self.assertListEqual(stdout, [1 if i < 8 else 0])
Exemple #19
0
 def test_part1_example2(self):
     data = decode(
         "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0"
     )
     self.assertEqual(compute_thrust(data, range(5)), 54321)
Exemple #20
0
 def test_part2_example6(self):
     data = decode("3,3,1105,-1,9,1101,0,0,12,4,12,99,1")
     for i in range(10):
         stdout = run(data, [i])
         self.assertListEqual(stdout, [0 if i == 0 else 1])
Exemple #21
0
 def test_part1_example3(self):
     data = decode(
         "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,"
         "1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0"
     )
     self.assertEqual(compute_thrust(data, range(5)), 65210)
Exemple #22
0
        if diff_x > 0:
            moves = [-1] * diff_x
        else:
            moves = [1] * -diff_x

        # reset to last good state
        state = cab.last_good_state


class TestDay13(unittest.TestCase):
    def test_part1(self):
        with open_fixture("day13") as fp:
            data = decode(fp.readline())
        cab = ArcadeCabinet(data)
        cab.run()
        self.assertEqual(cab.init_block_count, 420)

    def test_part2(self):
        with open_fixture("day13-frame10588") as fp:
            data = decode(fp.readline())
        score = learn_and_score(data)
        self.assertEqual(score, 21651)


if __name__ == "__main__":
    with open_fixture("day13") as fp:
        data = decode(fp.readline())
    with CursesScreen() as stdscr:
        score = learn_and_score(data, stdscr=stdscr)
    print(f"Score: {score}")
Exemple #23
0
 def test_part2_example1(self):
     data = decode(
         "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5"
     )
     self.assertEqual(compute_thrust(data, range(5, 10)), 139629729)
Exemple #24
0
 def test_part2(self):
     with open_fixture("day13-frame10588") as fp:
         data = decode(fp.readline())
     score = learn_and_score(data)
     self.assertEqual(score, 21651)
Exemple #25
0
 def test_part1_example2(self):
     data = decode("1102,34915192,34915192,7,4,7,99,0")
     vm = IntcodeVM(data)
     vm.run()
     self.assertGreaterEqual(vm.stdout[0], 1000000000000000)