Exemplo n.º 1
0
        max_z += 1
        if four_dims:
            min_w -= 1
            max_w += 1
        new_state, state = state, new_state

    return len(state)


def part_2(initial: List[str]) -> int:
    return part_1(initial, True)


class Testing(unittest.TestCase):
    def test_part_1(self):
        initial = [".#.", "..#", "###"]
        self.assertEqual(part_1(initial), 112)

    def test_part_2(self):
        initial = [".#.", "..#", "###"]
        self.assertEqual(part_2(initial), 848)


if __name__ == '__main__':
    print('Day 17')
    initial: List[str] = read_lines('../inputs/day17.txt')

    print('\tPart 1: {}'.format(part_1(initial)))
    print('\tPart 2: {}'.format(part_2(initial)))
    # unittest.main()
Exemplo n.º 2
0
        if 'nop' in instruction:
            instructions[i] = 'jmp' + instruction[3:]
            accumulator, terminated = check_instructions(instructions)
            if terminated:
                return accumulator
            instructions[i] = instruction
        elif 'jmp' in instruction:
            instructions[i] = 'nop' + instruction[3:]
            accumulator, terminated = check_instructions(instructions)
            if terminated:
                return accumulator
            instructions[i] = instruction

    return -1


def part_1(instructions: List[str]) -> int:
    accumulator, _ = check_instructions(instructions)
    return accumulator


def part_2(instructions: List[str]) -> int:
    return try_terminate(instructions)


if __name__ == '__main__':
    print('Day 8')
    instructions = read_lines('../inputs/day8.txt')
    print('\tPart 1: {}'.format(part_1(instructions)))
    print('\tPart 2: {}'.format(part_2(instructions)))
Exemplo n.º 3
0
        def helper(color):
            if mem.get(color) is None:
                if len(self.inner_bags[color]) == 0:
                    mem[color] = 0
                else:
                    ans = 0
                    for inner_bag in self.inner_bags[color]:
                        ans += inner_bag[0]
                        ans += inner_bag[0] * helper(inner_bag[1])
                    mem[color] = ans
            return mem.get(color)

        return helper(color)


def part_1(graph: RegulationGraph):
    return graph.count_outermost_bag('shiny gold')


def part_2(graph: RegulationGraph):
    return graph.count_bag_inside('shiny gold')


if __name__ == '__main__':
    print('Day 7')
    data = read_lines('../inputs/day7.txt')
    graph = RegulationGraph(data)
    print('\tPart 1: {}'.format(part_1(graph)))
    print('\tPart 2: {}'.format(part_2(graph)))
Exemplo n.º 4
0
        self.assertEqual(
            math_calculate("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"),
            13632)

    def test_math_calculate_advanced(self):
        self.assertEqual(math_calculate("1 + 2 * 3 + 4 * 5 + 6", True), 231)
        self.assertEqual(math_calculate("1 + (2 * 3) + (4 * (5 + 6))", True),
                         51)
        self.assertEqual(math_calculate("2 * 3 + (4 * 5)", True), 46)
        self.assertEqual(math_calculate("5 + (8 * 3 + 9 + 3 * 4 * 3)", True),
                         1445)
        self.assertEqual(
            math_calculate("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))", True),
            669060)
        self.assertEqual(
            math_calculate("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2",
                           True), 23340)
        self.assertEqual(
            math_calculate(
                "3 + (2 * 2 + (7 * 3) * 2) + 7 + 4 + (2 + 6 * 4 + 9 * 4 * 5)",
                True), 2186)


if __name__ == '__main__':
    print('Day 18')
    expressions: List[str] = read_lines('../inputs/day18.txt')

    print('\tPart 1: {}'.format(part_1(expressions)))
    print('\tPart 2: {}'.format(part_2(expressions)))
    unittest.main()
Exemplo n.º 5
0
                    if self.layout[i][j] == '.':
                        continue
                    occupied_around = self.count_occupied_around(
                        i, j, first_visible)
                    if self.layout[i][j] == 'L' and occupied_around == 0:
                        new_layout[i][j] = '#'
                        changed = True
                    elif self.layout[i][j] == '#' and occupied_around >= rule:
                        new_layout[i][j] = 'L'
                        changed = True
                    else:
                        new_layout[i][j] = self.layout[i][j]
            new_layout, self.layout = self.layout, new_layout

        return sum([sum([i == '#' for i in row]) for row in self.layout])


def part_1(layout: Layout) -> int:
    return layout.count_occupied_seat()


def part_2(layout: Layout) -> int:
    return layout.count_occupied_seat(True, 5)


if __name__ == '__main__':
    print('Day 11')
    layout = Layout(read_lines('../inputs/day11.txt'))
    print('\tPart 1: {}'.format(part_1(layout)))
    print('\tPart 2: {}'.format(part_2(layout)))
Exemplo n.º 6
0
        # Order pairs of allergens and ingridents alphabetically
        result.sort()
        return ','.join(map(lambda pair: pair[1], result))


class Testing(unittest.TestCase):
    def test_solution(self):
        test_input = """mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)"""

        solution = Solution(test_input.splitlines())
        self.assertEqual(solution.solve_part_1(), 5)
        self.assertEqual(solution.solve_part_2(), 'mxmxvkd,sqjhc,fvjkl')


if __name__ == '__main__':
    print('Day 21')
    import time
    start = time.time()
    foods: List[str] = read_lines('../inputs/day21.txt')

    solution = Solution(foods)
    print('\tPart 1: {}'.format(solution.solve_part_1()))
    print('\tPart 2: {}'.format(solution.solve_part_2()))

    print("\tTime: {:.4f}ms".format((time.time() - start) * 1000))
    unittest.main()
Exemplo n.º 7
0
neeswseenwwswnwswswnw
nenwswwsewswnenenewsenwsenwnesesenew
enewnwewneswsewnwswenweswnenwsenwsw
sweneswneswneneenwnewenewwneswswnese
swwesenesewenwneswnwwneseswwne
enesenwswwswneneswsenwnewswseenwsese
wnwnesenesenenwwnenwsewesewsesesew
nenewswnwewswnenesenwnesewesw
eneswnwswnwsenenwnwnwwseeswneewsenese
neswnwewnwnwseenwseesewsenwsweewe
wseweeenwnesenwwwswnew"""

    def test_part_1(self):
        tiles = solve_part_1(self.test_input.splitlines())
        self.assertEqual(len(tiles), 10)
        self.assertEqual(solve_part_2(tiles), 2208)


if __name__ == '__main__':
    print('Day 24')
    import time
    start = time.time()
    lines = read_lines('../inputs/day24.txt')

    tiles = solve_part_1(lines)
    print('\tPart 1: {}'.format(len(tiles)))
    print('\tPart 2: {}'.format(solve_part_2(tiles)))

    print("\tTime: {:.4f}ms".format((time.time() - start) * 1000))
    unittest.main()