• 38 Posts
  • 1.28K Comments
Joined 3 years ago
cake
Cake day: June 12th, 2023

help-circle



  • I haven’t timed it, but easily below a second.
    Could that be optimised? Most certainly.

    Due to the ranges being in a set, rather than a list, the input order doesn’t matter anyway. And the set really does a lot of heavy lifting for making the code so concise. You’ll need a bunch of boilerplate for list maintenance, especially of you continuously keep it sorted.
    The set also removed 8 duplicates I had in the input.


  • Managed to keep it compact, but boy, do I hate cephalopod math >_<

    Python

    from csv import reader
    from functools import reduce
    from itertools import pairwise
    from operator import mul
    from pathlib import Path
    from typing import Any, List, Sequence
    
    
    def _calc(values: List[str]) -> int:
        match values[-1]:
            case "+":
                return sum(map(int, values[:-1]))
            case "*":
                return reduce(mul, map(int, values[:-1]))
            case _:
                return 0
    
    
    def _transpose(values: Sequence[Sequence[Any]]) -> List[List[Any]]:
        return [[values[row][col] for row in range(len(values))] for col in range(len(values[0]))]
    
    
    def part_one(input: str) -> int:
        def _parse_input(input: str) -> List[List[str]]:
            return _transpose(list(map(lambda r: list(filter(None, r)), reader(input.splitlines(), delimiter=" "))))
    
        return sum(map(_calc, _parse_input(input)))
    
    
    def part_two(input: str) -> int:
        def _parse_input(input: str) -> List[List[str]]:
            data = list(input.splitlines())
            columns = [t[0] for t in filter(lambda t: t[1] != " ", enumerate(data[-1]))] + [len(data[0])]
            numbers = [[line[a:b] for line in data[:-1]] for a, b in pairwise(columns)]
            numbers = [list(filter(None, ["".join(num).strip() for num in column])) for column in map(_transpose, numbers)]
            return list(map(lambda t: t[0] + [t[1]], zip(numbers, list(filter(None, data[-1].split(" "))))))
    
        return sum(map(_calc, _parse_input(input)))
    
    
    if __name__ == "__main__":
        input = Path("_2025/_6/input").read_text("utf-8")
        print(part_one(input))
        print(part_two(input))
    

  • Again part 2 took me way longer than I would’ve liked and also than feels appropriate for the simplicity of the solution I finally came up with.
    Turned out quite fast, thanks to the ranges.

    Python

    from pathlib import Path
    from typing import List
    from itertools import combinations
    
    def parse_input(input: str) -> tuple[set[range], list[int]]:
        parts = input.split("\n\n")
        fresh = set((lambda r: range(int(r[0]), int(r[1]) + 1))(line.split("-")) for line in parts[0].splitlines())
        return (fresh, list(map(int, parts[1].splitlines())))
    
    
    def merge_ranges(a: range, b: range) -> List[range]:
        if a.stop <= b.start or b.stop <= a.start:
            return [a, b]
        return [range(min(a.start, b.start), max(a.stop, b.stop))]
    
    
    def part_one(input: str) -> int:
        fresh, available = parse_input(input)
        return len(list(filter(None, [any(i in r for r in fresh) for i in available])))
    
    
    def part_two(input: str) -> int:
        fresh, _ = parse_input(input)
        while True:
            for a, b in combinations(fresh, 2):
                if len(m := merge_ranges(a, b)) == 1:
                    fresh.remove(a)
                    fresh.remove(b)
                    fresh.add(m[0])
                    break
            else:
                break
        return sum(map(len, fresh))
    
    
    if __name__ == "__main__":
        input = Path("_2025/_5/input").read_text("utf-8")
        print(part_one(input))
        print(part_two(input))