|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2025 Day 3 |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | +from functools import cache |
| 8 | + |
| 9 | +from aoc.common import InputData |
| 10 | +from aoc.common import SolutionBase |
| 11 | +from aoc.common import aoc_samples |
| 12 | +from aoc.common import log |
| 13 | + |
| 14 | +Input = InputData |
| 15 | +Output1 = int |
| 16 | +Output2 = int |
| 17 | + |
| 18 | + |
| 19 | +TEST = """\ |
| 20 | +987654321111111 |
| 21 | +811111111111119 |
| 22 | +234234234234278 |
| 23 | +818181911112111 |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +class Solution(SolutionBase[Input, Output1, Output2]): |
| 28 | + def parse_input(self, input_data: InputData) -> Input: |
| 29 | + return input_data |
| 30 | + |
| 31 | + def part_1(self, inputs: Input) -> Output1: |
| 32 | + ans = 0 |
| 33 | + for line in inputs: |
| 34 | + first = max(enumerate(line[:-1]), key=lambda e: (e[1], -e[0])) |
| 35 | + second = max( |
| 36 | + enumerate(line[first[0] + 1 :]), key=lambda e: (e[1], -e[0]) |
| 37 | + ) |
| 38 | + ans += int(first[1] + second[1]) |
| 39 | + return ans |
| 40 | + |
| 41 | + def part_2(self, inputs: Input) -> Output2: |
| 42 | + @cache |
| 43 | + def dfs(left: int, start: int) -> int: |
| 44 | + if start >= len(line): |
| 45 | + return 0 |
| 46 | + if left == 1: |
| 47 | + if start == len(line) - 1: |
| 48 | + return int(line[-1]) |
| 49 | + return max(int(line[start]), dfs(1, start + 1)) |
| 50 | + cur = dfs(left - 1, start + 1) |
| 51 | + nxt = dfs(left, start + 1) |
| 52 | + if cur > 0: |
| 53 | + return max(int(line[start] + str(cur)), nxt) |
| 54 | + return nxt |
| 55 | + |
| 56 | + dfs.cache_clear() |
| 57 | + line = "1" |
| 58 | + test = dfs(1, 0) |
| 59 | + assert test == 1, test |
| 60 | + dfs.cache_clear() |
| 61 | + line = "21" |
| 62 | + test = dfs(1, 0) |
| 63 | + assert test == 2, test |
| 64 | + dfs.cache_clear() |
| 65 | + line = "12" |
| 66 | + test = dfs(1, 0) |
| 67 | + assert test == 2, test |
| 68 | + dfs.cache_clear() |
| 69 | + line = "12" |
| 70 | + test = dfs(2, 0) |
| 71 | + assert test == 12, test |
| 72 | + dfs.cache_clear() |
| 73 | + line = "123" |
| 74 | + test = dfs(2, 0) |
| 75 | + assert test == 23, test |
| 76 | + dfs.cache_clear() |
| 77 | + line = "321" |
| 78 | + test = dfs(2, 0) |
| 79 | + assert test == 32, test |
| 80 | + ans = 0 |
| 81 | + for line in inputs: # noqa:B007 |
| 82 | + dfs.cache_clear() |
| 83 | + best = dfs(12, 0) |
| 84 | + log(best) |
| 85 | + ans += best |
| 86 | + return ans |
| 87 | + |
| 88 | + @aoc_samples( |
| 89 | + ( |
| 90 | + ("part_1", TEST, 357), |
| 91 | + ("part_2", TEST, 3121910778619), |
| 92 | + ) |
| 93 | + ) |
| 94 | + def samples(self) -> None: |
| 95 | + pass |
| 96 | + |
| 97 | + |
| 98 | +solution = Solution(2025, 3) |
| 99 | + |
| 100 | + |
| 101 | +def main() -> None: |
| 102 | + solution.run(sys.argv) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments