예제 #1
0
파일: day_16.py 프로젝트: gchazot/aoc
    def test_execute_instructions(self):
        samples_lines = data_lines(2018, "day_16_A_mine.txt")
        instructions_mapping = find_instruction_ids(samples_lines)

        processor = Processor(registers=[0, 0, 0, 0])
        for program_line in data_lines(2018, "day_16_B_mine.txt"):
            instruction = list(map(int, program_line.split()))
            instruction_id = instruction[0]
            instruction_code = instructions_mapping[instruction_id]
            args = instruction[1:]
            getattr(processor, instruction_code)(*args)

        self.assertListEqual([649, 3, 2, 1], processor.registers)
예제 #2
0
    def test_mine(self):
        sky = SkyMap()
        for point_str in data_lines(2018, "day_10_mine.txt"):
            point = SkyPoint.parse(point_str)
            sky.add(point)

        self.assertEqual(328, sky.size())

        solver = SkySolver(sky, sample_size=10)
        surface, iteration, table = solver.solve()

        self.assertEqual(620, surface)
        self.assertEqual(10605, iteration)
        self.assertEqual([
            '#####   #    #     ###  #    #     ###    ##    ######  #    #',
            '#    #  #    #      #   #    #      #    #  #   #       #    #',
            '#    #   #  #       #    #  #       #   #    #  #        #  # ',
            '#    #   #  #       #    #  #       #   #    #  #        #  # ',
            '#####     ##        #     ##        #   #    #  #####     ##  ',
            '#    #    ##        #     ##        #   ######  #         ##  ',
            '#    #   #  #       #    #  #       #   #    #  #        #  # ',
            '#    #   #  #   #   #    #  #   #   #   #    #  #        #  # ',
            '#    #  #    #  #   #   #    #  #   #   #    #  #       #    #',
            '#####   #    #   ###    #    #   ###    #    #  ######  #    #',
        ], list(table))
예제 #3
0
파일: day_07.py 프로젝트: gchazot/aoc
    def test_calculate_time_mine(self):
        manual = InstructionManual()

        for line in data_lines(2018, 'day_07_mine.txt'):
            manual.add(line)

        self.assertEqual(1107, manual.calculate_time(num_workers=5))
예제 #4
0
 def test_mine(self):
     self.maxDiff = None
     example_lines = data_lines(2018, 'day_24_mine.txt')
     groups = list(parse(example_lines))
     expected = {
         "Immune System: 1514 units, 8968 hp, 57 x bludgeoning, weak ['cold'], initiative 9",
         "Immune System: 2721 units, 6691 hp, 22 x slashing, weak ['cold'], initiative 15",
         "Immune System: 1214 units, 10379 hp, 69 x fire, immune ['bludgeoning'], initiative 16",
         "Immune System: 2870 units, 4212 hp, 11 x radiation, initiative 12",
         "Immune System: 1239 units, 5405 hp, 37 x cold, weak ['cold'], initiative 18",
         "Immune System: 4509 units, 4004 hp, 8 x slashing, weak ['cold'], immune ['radiation'], initiative 20",
         "Immune System: 3369 units, 10672 hp, 29 x cold, weak ['slashing'], initiative 11",
         "Immune System: 2890 units, 11418 hp, 30 x cold, weak ['fire'], immune ['bludgeoning'], initiative 8",
         "Immune System: 149 units, 7022 hp, 393 x radiation, weak ['slashing'], initiative 13",
         "Immune System: 2080 units, 5786 hp, 20 x fire, weak ['fire'], immune ['slashing', 'bludgeoning'], initiative 7",
         "Infection: 817 units, 47082 hp, 115 x cold, immune ['slashing', 'radiation', 'bludgeoning'], initiative 3",
         "Infection: 4183 units, 35892 hp, 16 x bludgeoning, initiative 1",
         "Infection: 7006 units, 11084 hp, 2 x fire, initiative 2",
         "Infection: 4804 units, 25411 hp, 10 x cold, initiative 14",
         "Infection: 6262 units, 28952 hp, 7 x slashing, weak ['fire'], initiative 10",
         "Infection: 628 units, 32906 hp, 99 x radiation, weak ['slashing'], initiative 4",
         "Infection: 5239 units, 46047 hp, 14 x bludgeoning, immune ['fire'], initiative 6",
         "Infection: 1173 units, 32300 hp, 53 x bludgeoning, weak ['cold', 'slashing'], initiative 19",
         "Infection: 3712 units, 12148 hp, 5 x slashing, weak ['slashing'], immune ['cold'], initiative 17",
         "Infection: 334 units, 43582 hp, 260 x cold, weak ['cold', 'fire'], initiative 5",
     }
     self.assertSetEqual(expected, {repr(g) for g in groups})
예제 #5
0
파일: day_12.py 프로젝트: gchazot/aoc
    def test_sum_numbers_mine(self):
        lines = data_lines(2018, "day_12_mine.txt")
        pots, rules = parse_input(lines)
        for _ in range(0, 20):
            pots.iterate(rules)

        self.assertEqual(6201, pots.sum_numbers())
예제 #6
0
 def parse_initial_state(self, filename):
     for i, line in enumerate(data_lines(2018, filename)):
         for j, c in enumerate(line.rstrip()):
             position = (i, j)
             if c == "+":
                 self._intersections.add(position)
             elif c == ">":
                 self._carts[position] = Directions.right, 0
             elif c == "<":
                 self._carts[position] = Directions.left, 0
             elif c == "^":
                 self._carts[position] = Directions.up, 0
             elif c == "v":
                 self._carts[position] = Directions.down, 0
             elif c == "\\":
                 self._bends[position] = {
                     Directions.left: Directions.up,
                     Directions.right: Directions.down,
                     Directions.up: Directions.left,
                     Directions.down: Directions.right,
                 }
             elif c == "/":
                 self._bends[position] = {
                     Directions.left: Directions.down,
                     Directions.right: Directions.up,
                     Directions.up: Directions.right,
                     Directions.down: Directions.left,
                 }
             elif c not in ["-", "|", " "]:
                 raise RuntimeError("Unknown track item", c)
예제 #7
0
파일: day_19.py 프로젝트: gchazot/aoc
 def test_mine(self):
     program = Program(
         num_registers=6,
         input_lines=data_lines(2018, "day_19_mine.txt"),
         processor_class=JumpingProcessor,
     )
     program.execute(10000000)
     self.assertEqual(2072, program.processor.registers[0])
예제 #8
0
파일: day_18.py 프로젝트: gchazot/aoc
    def test_resource_value_mine(self):
        lines = data_lines(2018, "day_18_mine.txt")
        woodland = Woodlands(input_lines=lines)

        for _ in range(10):
            woodland.iterate()

        self.assertEqual(678529, woodland.resource_value())
예제 #9
0
def read_network(filename):
    network = {}
    for line in data_lines(2017, filename):
        program_id_str, contacts_str = line.split(" <-> ")
        program_id = int(program_id_str)
        contact_list_str = contacts_str.split(", ")
        contacts = map(int, contact_list_str)
        network[program_id] = list(contacts)
    return network
예제 #10
0
    def test_mine(self):
        lines = data_lines(2018, "day_17_mine.txt")
        caves = WaterCaves(lines)

        caves.fill_with_water()
        self.assertEqual(31667, caves.count_water())

        caves.clear_draining_water()
        self.assertEqual(25018, caves.count_water())
예제 #11
0
    def test_simulate_mine(self):
        lines = data_lines(2018, 'day_24_mine.txt')
        groups = list(parse(lines))

        surviving_groups = simulate(groups)

        self.assertTrue(all("Infection" == g.army for g in surviving_groups))

        self.assertEqual(30881, sum(g.units for g in surviving_groups))
예제 #12
0
파일: day_02.py 프로젝트: gchazot/aoc
    def test_find_magic_id(self):
        self.assertEqual(None, find_magic_id(
            ["abcde", "fghik", "klmno", "pqrst", "fguij", "axcye", "wvxyz"]))

        self.assertEqual("fgij", find_magic_id(
            ["abcde", "fghij", "klmno", "pqrst", "fguij", "axcye", "wvxyz"]))

        id_list = list(data_lines(2018, "day_02_mine.txt"))
        self.assertEqual("jiwamotgsfrudclzbyzkhlrvp", find_magic_id(id_list))
예제 #13
0
파일: day_19.py 프로젝트: gchazot/aoc
    def test_example(self):
        labyrinth_lines = data_lines(2017, "day_19_example.txt")

        labyrinth = Labyrinth(labyrinth_lines)
        walker = Walker(labyrinth)
        walker.walk()

        self.assertEqual("ABCDEF", walker.word())
        self.assertEqual(38, walker.steps())
예제 #14
0
파일: day_19.py 프로젝트: gchazot/aoc
    def test_mine(self):
        labyrinth_lines = data_lines(2017, "day_19_mine.txt")

        labyrinth = Labyrinth(labyrinth_lines)
        walker = Walker(labyrinth)
        walker.walk()

        self.assertEqual("PBAZYFMHT", walker.word())
        self.assertEqual(16072, walker.steps())
예제 #15
0
파일: day_16.py 프로젝트: gchazot/aoc
    def test_options_per_sample(self):
        all_lines = data_lines(2018, "day_16_A_mine.txt")
        samples = list(parse_samples(all_lines))
        self.assertEqual(779, len(samples))

        all_matches = map(matching_instructions, samples)
        big_matches = filter(lambda matches: len(list(matches)) >= 3,
                             all_matches)
        self.assertEqual(531, len(list(big_matches)))
예제 #16
0
파일: day_12.py 프로젝트: gchazot/aoc
    def test_sum_numbers_mine_long(self):
        lines = data_lines(2018, "day_12_mine.txt")
        pots, rules = parse_input(lines)
        seen = {}
        for i in range(0, 92):
            pots.iterate(rules)
        # From here, all iterations are only shifting the whole plantation right
        pots._offset -= (50000000000 - 92)

        self.assertEqual(9300000001023, pots.sum_numbers())
예제 #17
0
    def test_simulate_example(self):
        example_lines = data_lines(2018, 'day_24_example.txt')
        groups = list(parse(example_lines))
        surviving_groups = simulate(groups)

        self.assertEqual(2, len(surviving_groups))
        self.assertTrue(all("Infection" == g.army for g in surviving_groups))
        self.assertSetEqual({782, 4434}, {g.units for g in surviving_groups})

        self.assertEqual(5216, sum(g.units for g in surviving_groups))
예제 #18
0
파일: day_12.py 프로젝트: gchazot/aoc
    def test_iterate_mine(self):
        lines = data_lines(2018, "day_12_mine.txt")
        pots, rules = parse_input(lines)

        for _ in range(0, 20):
            pots.iterate(rules)
        self.check_pots(
            pots, 1, 120, 101,
            "#######.###.#####.#####.#.##.##.###.#.####.######.#.#######.#.####.##.##.###.##.###"
            + "#####################################")
예제 #19
0
파일: day_19.py 프로젝트: gchazot/aoc
    def test_mine_again(self):
        program = Program(
            num_registers=6,
            input_lines=data_lines(2018, "day_19_mine.txt"),
            initial_registers=[1, 0, 0, 0, 0, 0],
            processor_class=JumpingProcessor,
        )
        program.execute(34)

        self.assertEqual(2072, program.processor.registers[0])
예제 #20
0
파일: day_10.py 프로젝트: gchazot/aoc
    def test_execute_mine(self):
        lines = data_lines(2016, "day_10_mine.txt")
        bots = ZoomingBots(lines)

        bots.execute()

        self.assertEqual(('bot', 147), bots.find_comparator(17, 61))

        outputs = [bots.get_output(i) for i in range(3)]
        self.assertEqual(55637, reduce(mul, outputs))
예제 #21
0
파일: day_21.py 프로젝트: gchazot/aoc
    def test_execute_program(self):
        program = Program(
            num_registers=6,
            input_lines=data_lines(2018, "day_21_mine.txt"),
            initial_registers=[1024276, 0, 0, 0, 0, 0],
            processor_class=JumpingProcessor,
        )
        program.execute(10000, log=False)

        self.assertEqual(1848, program.total_instructions)
예제 #22
0
    def test_solve_closest_mine(self):
        lines = data_lines(2018, 'day_06_mine.txt')
        pairs = [line.split(', ') for line in lines]
        coordss = [tuple(map(int, pair)) for pair in pairs]

        landing = ClosestLandingArea()
        for i, coords in enumerate(coordss):
            landing.add(i, coords)

        self.assertEqual(43302, landing.solve())
예제 #23
0
    def test_find_all_constellations(self):
        expected_counts = [2, 4, 3, 8]
        for i, example in enumerate(examples):
            points = parse(examples[i])
            constellations = list(find_all_constellations(points))
            self.assertEqual(expected_counts[i], len(constellations))

        my_lines = data_lines(2018, "day_25_mine.txt")
        my_points = parse(my_lines)
        my_constellations = list(find_all_constellations(my_points))
        self.assertEqual(420, len(my_constellations))
예제 #24
0
파일: day_07.py 프로젝트: gchazot/aoc
    def test_find_sequence_mine(self):
        manual = InstructionManual()

        for line in data_lines(2018, 'day_07_mine.txt'):
            manual.add(line)

        self.assertEqual(26, manual.num_steps())
        self.assertEqual(101, manual.num_instructions())

        self.assertEqual("BDHNEGOLQASVWYPXUMZJIKRTFC",
                         "".join(manual.find_sequence()))
예제 #25
0
파일: day_04.py 프로젝트: gchazot/aoc
    def test_find_best_minute_guard(self):
        journal = GuardsJournal()
        self._add_simple_entries(journal)
        self._add_more_entries(journal)

        self.assertEqual(("2", 12, 3), journal.find_best_minute_guard())

        myjournal = GuardsJournal()
        for entry_line in data_lines(2018, "day_04_mine.txt"):
            myjournal.add(entry_line.strip())
        self.assertEqual(("1097", 21, 15), myjournal.find_best_minute_guard())
        self.assertEqual(23037, 1097 * 21)
예제 #26
0
파일: day_10.py 프로젝트: gchazot/aoc
    def test_execute_example(self):
        lines = data_lines(2016, "day_10_example.txt")
        bots = ZoomingBots(lines)

        bots.execute()

        for (type, id), data in bots.nodes.items():
            if type == 'output':
                self.assertEqual(1, len(data['values']))
            elif type == 'bot':
                self.assertEqual(2, len(data['values']))

        self.assertEqual(('bot', 2), bots.find_comparator(2, 5))
예제 #27
0
파일: day_04.py 프로젝트: gchazot/aoc
    def test_find_best_guard_best_minute(self):
        journal = GuardsJournal()
        self._add_simple_entries(journal)
        self._add_more_entries(journal)

        self.assertEqual(("1", 31, 10), journal.find_best_guard_best_minute())

        myjournal = GuardsJournal()
        for entry_line in data_lines(2018, "day_04_mine.txt"):
            myjournal.add(entry_line.strip())
        self.assertEqual(("727", 449, 49),
                         myjournal.find_best_guard_best_minute())
        self.assertEqual(35623, 727 * 49)
예제 #28
0
파일: day_04.py 프로젝트: gchazot/aoc
    def test_add_entry(self):
        journal = GuardsJournal()
        journal.add("[1518-11-01 00:00] Guard #10 begins shift")
        self.assertEqual(1, journal.size())
        journal.add("[1518-11-01 00:05] falls asleep")
        self.assertEqual(2, journal.size())
        journal.add("[1518-11-01 00:25] wakes up")
        self.assertEqual(3, journal.size())

        myjournal = GuardsJournal()
        for entry_line in data_lines(2018, "day_04_mine.txt"):
            myjournal.add(entry_line.strip())
        self.assertEqual(1097, myjournal.size())
예제 #29
0
파일: day_03.py 프로젝트: gchazot/aoc
    def test_count_overlaps(self):
        cls = ClaimsList()
        cls.add("#1 @ 1,3: 4x4")
        cls.add("#2 @ 3,1: 4x4")
        cls.add("#3 @ 5,5: 2x2")

        self.assertEqual(4, cls.count_overlaps())

        claims_list = data_lines(2018, "day_03_mine.txt")
        claims_mine = ClaimsList()
        for claim_string in claims_list:
            claims_mine.add(claim_string.strip())
        self.assertEqual(110389, claims_mine.count_overlaps())
예제 #30
0
파일: day_03.py 프로젝트: gchazot/aoc
    def test_find_non_overlapped(self):
        cls = ClaimsList()
        cls.add("#1 @ 1,3: 4x4")
        cls.add("#2 @ 3,1: 4x4")
        cls.add("#3 @ 5,5: 2x2")

        self.assertEqual([3], cls.find_non_overlapped())

        claims_list = data_lines(2018, "day_03_mine.txt")
        claims_mine = ClaimsList()
        for claim_string in claims_list:
            claims_mine.add(claim_string.strip())
        self.assertEqual([552], claims_mine.find_non_overlapped())