def test_text_init(self):
        "Test the Caves object creation from text"

        # 1. Create Caves object from text
        myobj = caves.Caves(text=aoc_09.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 5)
        self.assertEqual(len(myobj.heights), 50)
        self.assertEqual(len(myobj.basins), 0)

        # 3. Check methods
        self.assertEqual(sum(myobj.adjacent_heights((0, 0))), 4)
        self.assertEqual(sum(myobj.adjacent_heights((3, 1))), 31)
        self.assertEqual(sum(myobj.adjacent_heights((9, 0))), 2)
        self.assertEqual(sum(myobj.adjacent_heights((6, 4))), 18)

        self.assertEqual(myobj.is_lowest((0, 0)), False)
        self.assertEqual(myobj.is_lowest((1, 0)), True)
        self.assertEqual(myobj.is_lowest((9, 0)), True)
        self.assertEqual(myobj.is_lowest((2, 2)), True)
        self.assertEqual(myobj.is_lowest((6, 4)), True)
        self.assertEqual(myobj.is_lowest((3, 3)), False)

        lowest = myobj.lowest_points()
        self.assertEqual(len(lowest), 4)
        self.assertEqual(sum([myobj.heights[l] for l in lowest]), 11)

        self.assertEqual(myobj.risk((0, 0)), 3)
        self.assertEqual(myobj.risk((1, 0)), 2)
        self.assertEqual(myobj.total_risk([(0, 0), (1, 0)]), 5)
        self.assertEqual(myobj.total_risk(lowest), 15)
        self.assertEqual(myobj.total_risk(), 15)
    def test_text_init_two(self):
        "Test the Caves object creation from text for part 2"

        # 1. Create Caves object from text
        myobj = caves.Caves(text=aoc_09.from_text(EXAMPLE_TEXT), part2=True)

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, True)
        self.assertEqual(len(myobj.text), 5)
        self.assertEqual(len(myobj.heights), 50)
        self.assertEqual(len(myobj.basins), 0)

        # 3. Check methods
        self.assertEqual(len(myobj.not_nines((0, 0))), 2)
        self.assertEqual(len(myobj.not_nines((1, 0))), 1)
        self.assertEqual(len(myobj.not_nines((9, 0))), 2)
        self.assertEqual(len(myobj.not_nines((6, 0))), 3)
        self.assertEqual(len(myobj.not_nines((6, 1))), 1)

        self.assertEqual(len(myobj.grow_basin((0, 0))), 3)
        self.assertEqual(len(myobj.grow_basin((9, 0))), 9)
        self.assertEqual(len(myobj.grow_basin((3, 2))), 14)
        self.assertEqual(len(myobj.grow_basin((6, 4))), 9)

        self.assertEqual(myobj.get_basins(), 1134)
    def test_part_two(self):
        "Test part two example of Caves object"

        # 1. Create Caves object from text
        myobj = caves.Caves(part2=True, text=aoc_09.from_text(PART_TWO_TEXT))

        # 2. Check the part two result
        self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
    def test_part_one(self):
        "Test part one example of Caves object"

        # 1. Create Caves object from text
        myobj = caves.Caves(text=aoc_09.from_text(PART_ONE_TEXT))

        # 2. Check the part one result
        self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
Example #5
0
    def test_empty_init(self):
        "Test the default Caves creation"

        # 1. Create default Caves object
        myobj = caves.Caves()

        # 2. Make sure it has the default values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(myobj.text, None)
        self.assertEqual(len(myobj.caves), 0)
Example #6
0
def part_two(args, input_lines):
    "Process part two of the puzzle"

    # 1. Create the puzzle solver
    solver = caves.Caves(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
Example #7
0
    def test_text_init_one(self):
        "Test the Caves object creation from text"

        # 1. Create Caves object from text
        myobj = caves.Caves(text=aoc_12.from_text(EXAMPLE_ONE))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 7)
        self.assertEqual(len(myobj.caves), 6)
        self.assertEqual(len(myobj.caves['start']), 2)
        self.assertEqual(len(myobj.caves['A']), 4)
        self.assertEqual(len(myobj.caves['b']), 4)
        self.assertEqual(myobj.caves['c'], ['A'])
        self.assertEqual(myobj.caves['d'], ['b'])
        self.assertEqual(len(myobj.caves['end']), 2)

        # 3. Check methods
        self.assertEqual(len(myobj.filter_options([], 'start')), 2)
        self.assertEqual(len(myobj.filter_options(['A'], 'start')), 2)
        self.assertEqual(len(myobj.filter_options(['b'], 'start')), 1)
        self.assertEqual(len(myobj.filter_options(['a', 'b', 'c'], 'start')),
                         1)

        self.assertEqual(len(myobj.find_paths('start', 'end')), 10)

        # 4. Check methods for part 2
        myobj.part2 = True

        self.assertEqual(len(myobj.small_caves()), 3)

        self.assertEqual(myobj.times_occurs([], 'b'), 0)
        self.assertEqual(myobj.times_occurs(['b'], 'b'), 1)
        self.assertEqual(myobj.times_occurs(['A', 'b', 'C'], 'b'), 1)
        self.assertEqual(myobj.times_occurs(['A', 'b', 'C', 'b', 'D'], 'b'), 2)

        self.assertEqual(len(myobj.filter_options([], 'start')), 2)
        self.assertEqual(len(myobj.filter_options([], 'start', twice='')), 2)
        self.assertEqual(len(myobj.filter_options([], 'start', twice='x')), 2)
        self.assertEqual(len(myobj.filter_options(['b'], 'start', twice='d')),
                         1)
        self.assertEqual(len(myobj.filter_options(['b'], 'start', twice='b')),
                         2)
        self.assertEqual(len(myobj.filter_options(['b'], 'start', twice='d')),
                         1)

        self.assertEqual(len(myobj.find_paths('start', 'end')), 36)
Example #8
0
    def test_text_init_three(self):
        "Test the Caves object creation from text using example three"

        # 1. Create Caves object from text
        myobj = caves.Caves(text=aoc_12.from_text(EXAMPLE_THREE))

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

        # 3. Check methods
        self.assertEqual(len(myobj.find_paths('start', 'end')), 226)

        self.assertEqual(len(myobj.small_caves()), 5)

        # 4. Check methods for part 2
        myobj.part2 = True
        self.assertEqual(len(myobj.find_paths('start', 'end')), 3509)