Ejemplo n.º 1
0
    def test_second(self):
        data = utils.parse_group("test_input.txt")
        o2 = day03.compute_o2(data)
        self.assertEqual(int(o2, 2), 23)
        co2 = day03.compute_co2(data)
        self.assertEqual(int(co2, 2), 10)

        self.assertEqual(int(o2, 2) * int(co2, 2), 230)
Ejemplo n.º 2
0
 def test_first(self):
     data = utils.parse_group("test_input.txt")
     draw_nums = day04.get_draw(data[0])
     boards = day04.gen_boards(data[1:])
     winning_board, draw = day04.play_bingo(draw_nums, boards)
     self.assertEqual(
         draw * sum(ele.num
                    for ele in winning_board.values() if not ele.drawn),
         4512)
Ejemplo n.º 3
0
    def test_first(self):
        data = utils.parse_group("test_input.txt")
        bit_counter = day03.count_bits(data)
        gamma = day03.compute_gamma(bit_counter)
        self.assertEqual(int(gamma, 2), 22)
        epsilon = day03.compute_epsilon(bit_counter)
        self.assertEqual(int(epsilon, 2), 9)

        self.assertEqual(int(gamma, 2) * int(epsilon, 2), 198)
Ejemplo n.º 4
0
    def test_none(self):
        g = parse_group(
            'test_none',
            '2702 units each with 10159 hit points with an attack that does 7 fire damage at initiative 7'
        )

        self.assertEqual(g.num_units, 2702)
        self.assertEqual(g.hit_points, 10159)
        self.assertEqual(g.attack_damage, 7)
        self.assertEqual(g.initiative, 7)
        self.assertSetEqual(g.weak_to, set())
        self.assertSetEqual(g.immune_to, set())
        self.assertEqual(g.effective_power, 2702 * 7)
Ejemplo n.º 5
0
    def test_immune(self):
        g = parse_group(
            'test_immune',
            '338 units each with 1378 hit points (immune to radiation) with an attack that does 39 cold damage at initiative 10'
        )

        self.assertEqual(g.num_units, 338)
        self.assertEqual(g.hit_points, 1378)
        self.assertEqual(g.attack_damage, 39)
        self.assertEqual(g.initiative, 10)
        self.assertSetEqual(g.weak_to, set())
        self.assertSetEqual(g.immune_to, set((Attack.radiation, )))
        self.assertEqual(g.effective_power, 338 * 39)
Ejemplo n.º 6
0
    def test_weak(self):
        g = parse_group(
            'test_weak',
            '2785 units each with 4474 hit points (weak to cold) with an attack that does 14 fire damage at initiative 20'
        )

        self.assertEqual(g.num_units, 2785)
        self.assertEqual(g.hit_points, 4474)
        self.assertEqual(g.attack_damage, 14)
        self.assertEqual(g.initiative, 20)
        self.assertSetEqual(g.weak_to, set((Attack.cold, )))
        self.assertSetEqual(g.immune_to, set())
        self.assertEqual(g.effective_power, 2785 * 14)
Ejemplo n.º 7
0
    def test_immune_weak(self):
        g = parse_group(
            'test_immune_weak',
            '4674 units each with 7617 hit points (immune to slashing, bludgeoning; weak to fire) with an attack that does 15 slashing damage at initiative 15'
        )

        self.assertEqual(g.num_units, 4674)
        self.assertEqual(g.hit_points, 7617)
        self.assertEqual(g.attack_damage, 15)
        self.assertEqual(g.initiative, 15)
        self.assertSetEqual(g.weak_to, set((Attack.fire, )))
        self.assertSetEqual(g.immune_to,
                            set((
                                Attack.slashing,
                                Attack.bludgeoning,
                            )))
        self.assertEqual(g.effective_power, 4674 * 15)
Ejemplo n.º 8
0
 def test_first(self):
     data = utils.parse_group("test_input.txt")
     self.assertEqual(day02.travel(data), 150)
Ejemplo n.º 9
0
 def test_second(self):
     data = utils.parse_group("test_input.txt")
     self.assertEqual(day02.travel(data, True), 900)
Ejemplo n.º 10
0
                case "down":
                    self.aim += distance
        else:
            match direction:
                case "forward":
                    self.horizontal += distance
                case "up":
                    self.depth -= distance
                case "down":
                    self.depth += distance


def travel(instrucs: list[str], aim: bool = False) -> int:
    position = Position(0, 0, 0) if aim else Position(0, 0)

    for instruc in instrucs:
        position.execute(instruc)

    return position.horizontal * position.depth


if __name__ == "__main__":
    input_fpath = input("Enter path containing input commands: ")
    in_cmds = utils.parse_group(input_fpath)

    # Puzzle 1
    print(f"Depth x position = {travel(in_cmds)}")

    # Puzzle 2
    print(f"Depth * position (with aim) = {travel(in_cmds, True)}")
Ejemplo n.º 11
0
            break

    return final_code[0]


def puzzle1(in_data: list[str]) -> int:
    bit_counts = count_bits(in_data)

    gamma = compute_gamma(bit_counts)
    epsilon = compute_epsilon(bit_counts)

    return int(gamma, 2) * int(epsilon, 2)


def puzzle2(in_data: list[str]) -> int:
    o2 = compute_o2(in_data)
    co2 = compute_co2(in_data)

    return int(o2, 2) * int(co2, 2)


if __name__ == "__main__":
    in_data = input("Enter file path of diagnostic report: ")
    in_fpath = utils.parse_group(in_data)

    # Puzzle 1
    print(f"The power consumption is: {puzzle1(in_fpath)}")

    # Puzzle 2
    print(f"The life support rating is: {puzzle2(in_fpath)}")