コード例 #1
0
    def test_part_one(self):
        "Test part one example of Nanobots object"

        # 1. Create Nanobots object from text
        myobj = nanobots.Nanobots(text=aoc_23.from_text(PART_ONE_TEXT))

        # 2. Check the part one result
        self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
コード例 #2
0
    def test_part_two(self):
        "Test part two example of Nanobots object"

        # 1. Create Nanobots object from text
        myobj = nanobots.Nanobots(part2=True,
                                  text=aoc_23.from_text(PART_TWO_TEXT))

        # 2. Check the part two result
        self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
コード例 #3
0
    def test_empty_init(self):
        "Test the default Nanobots creation"

        # 1. Create default Nanobots object
        myobj = nanobots.Nanobots()

        # 2. Make sure it has the default values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(myobj.text, None)
        self.assertEqual(len(myobj.bots), 0)
コード例 #4
0
def part_two(args, input_lines):
    "Process part two of the puzzle"

    # 1. Create the puzzle solver
    solver = nanobots.Nanobots(part2=True, text=input_lines)

    # 2. Determine the solution for part two
    solution = solver.part_two(verbose=args.verbose, limit=args.limit)
    if solution is None:
        print("There is no solution")
    else:
        print("The solution for part two is %s" % (solution))

    # 3. Return result
    return solution is not None
コード例 #5
0
    def test_text_init(self):
        "Test the Nanobots object creation from text"

        # 1. Create Nanobots object from text
        myobj = nanobots.Nanobots(text=aoc_23.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 9)
        self.assertEqual(len(myobj.bots), 9)

        # 3. Check distances
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[0]), 0)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[1]), 1)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[2]), 4)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[3]), 2)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[4]), 5)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[5]), 3)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[6]), 3)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[7]), 4)
        self.assertEqual(myobj.bots[0].manhattan_distance(myobj.bots[8]), 5)

        # 4. Check range
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[0]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[1]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[2]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[3]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[4]), False)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[5]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[6]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[7]), True)
        self.assertEqual(myobj.bots[0].in_range(myobj.bots[8]), False)

        # 4. Check strongest signal
        self.assertEqual(myobj.largest_signal_radius(), myobj.bots[0])

        # 5. Check number in range
        self.assertEqual(myobj.bots_in_range(myobj.bots[0]), 7)