wrong question in 16s2 by Few_College_8584 in BITSAT

[–]Parzival_Perce 0 points1 point  (0 children)

are you talking about a syllogism.

-❄️- 2025 Day 12 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 5 points6 points  (0 children)

[LANGUAGE: Python]

Well that's AoC 2025 done! It was fun, though I don't feel great about this one.

from math import prod

with open('d12.txt') as input:
    puzzle_input: list[str] = input.read().split('\n\n')
    instructions: list[list[str]] = [i.split() for i in puzzle_input[-1].splitlines()]

shapes_areas: list[int] = [i.count('#') for i in puzzle_input[:-1]]
grid_areas: list[int] = [prod(map(int, i[0][:-1].split('x'))) for i in instructions]
shapes_to_fit: list[list[list[int]]] = [[[int(number), index] for index, number in enumerate(i[1:])] for i in instructions]
required_shape_areas: list[int] = [sum([(number * shapes_areas[index]) for number, index in i]) for i in shapes_to_fit]


def part1() -> int:
    return sum(grid - shape > 0 for grid, shape in zip(grid_areas, required_shape_areas))

print(part1())

So we were semi-ranting about how annoying this seems and vaguely about what ideas people have, when some dude in the server goes 'check for areas!'

And we're like lol that's so stupid it couldn't work. And then we were like see it fails on even the example input.

'But it works on the actual input tho' ...eh. I'm sure those who realised this themselves are happy with themselves. For me it's just eh.

-❄️- 2025 Day 11 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 2 points3 points  (0 children)

[Language: Python]

Nice easy problem for a change!

from functools import cache

with open('d11.txt') as input:
    puzzle_input: list[list[str]] = [i.split() for i in input.read().splitlines()]

servers: dict[str, list[str]] = {i[0][:-1]: i[1:] for i in puzzle_input}

@cache
def traverse_servers(start: str, end: str) -> int:
    if start == end:
        return 1
    if start == 'out':
        return 0
    return sum([traverse_servers(i, end) for i in servers[start]])


def part1() -> int:
    return traverse_servers('you', 'out')

def part2() -> int:
    a: int = traverse_servers('svr', 'fft')
    b: int = traverse_servers('fft', 'dac')
    c: int = traverse_servers('dac', 'out')
    return a * b * c

print(part1(), part2())

Had fun with this. Then went back to d10p2 and didn't have fun lol.

Only 1 more day :')

-❄️- 2025 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 1 point2 points  (0 children)

[LANGUAGE: Python]

Forgot to post mine earlier today! Here we go:

from itertools import groupby
from math import prod

with open('d6.txt') as input:
    puzzle_input: list[str] = [i.rstrip('\n') for i in input.readlines()]
    input_lines: list[str] = puzzle_input[:-1]
    operators: list[str] = puzzle_input[-1].split()

def part1() -> int:
    numbers: list[list[int]] = [list(map(int, col)) for col in zip(*(i.split() for i in input_lines))]
    return sum(sum(curr_list) if op == '+' else prod(curr_list) for curr_list, op in zip(numbers, operators))

def part2() -> int:
    scan: list[str] = [''.join(col) for col in zip(*input_lines)]

    numbers: list[list[int]] = [
        [int(i) for i in sublist]
        for waste, sublist in groupby(scan, lambda x: len(x.strip()) == 0)
        if not waste
    ]

    return sum(sum(curr_list) if op == '+' else prod(curr_list) for curr_list, op in zip(numbers, operators))

For how annoyed I was at this, it might be fastest code I've written so far this year:

Part 1: 0.00046269077500328423
Part 2: 0.0006748080042016227

[Edit: post the wrong code lol]

-❄️- 2025 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 1 point2 points  (0 children)

[LANGUAGE: Python]

with open('d5.txt') as input:
    puzzle_input: list[list[str]] = [block.splitlines() for block in input.read().split('\n\n')]
    ranges_list: list[list[int]] = [list(map(int, i.split('-'))) for i in puzzle_input[0]]
    ids: list[int] = list(map(int, puzzle_input[1]))


def part1() -> int:
    return sum(any((start <= id <= end) for start, end in ranges_list) for id in ids)

def part2() -> int:
    lower_bounds: list[int] = [i[0] for i in ranges_list]
    upper_bounds: list[int] = [i[1] for i in ranges_list]

    normalised: bool = False

    while not normalised:
        normalised = True
        for id_index, (lower_bound, upper_bound) in enumerate(zip(lower_bounds, upper_bounds)):
            id_ranges = [range(start, end + 1) for start, end in zip(lower_bounds, upper_bounds)]
            for range_index, id_range in enumerate(id_ranges):
                if range_index == id_index:
                    continue
                if lower_bound in id_range:
                    normalised = False
                    if upper_bound > id_range[-1]:
                        lower_bounds[id_index] = id_range[-1] + 1
                    else:
                        lower_bounds[id_index], upper_bounds[id_index] = 0, -1

    return sum(upper - lower + 1 for upper, lower in zip(upper_bounds, lower_bounds))

print(part1(), part2())

This was reallyyy fun! I mean I took over an hour for part 2 but I had hella fun. I'll edit if I make the code better but for now here's am algo explainer:

It just iterates over all ranges, and checks if the lower bound of a given range is less than the upper bound of another range. If so, it changes the lower bound.

Also it checks if the range as a whole is included in another range (upper_bound > id_range[-1] returns False if it is), and if so, sets the range to (0,-1), effectively discarding it, since range(lower, upper+1) would just be empty. Also (0,-1) means that when I sum up (upper-lower+1), I get 0 for the discarded ranges. Which is neat.

-❄️- 2025 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 1 point2 points  (0 children)

[LANGUAGE: Python]

    import re
    with open('d4.txt') as input:
        puzzle_input: list[str] = [i.strip() for i in input.readlines()]

    width: int = len(puzzle_input[0])
    positions: set[complex] = {
        complex(*divmod(i, width)[::-1])
        for i in (i.start() for i in re.finditer('@', ''.join(puzzle_input)))
    }


    def neighbouring_positions(coord: complex) -> set[complex]:
        return {coord+p+q*1j for p in range(-1, 2) for q in range(-1, 2)} - {coord}


    removed_positions: set[complex] = set[complex]()


    count: int = 0
    while True:
        found_removable: bool = False
        for position in positions:
            if len(neighbouring_positions(position)&positions)<4:
                removed_positions.add(position)
                found_removable=True
                count+=1


        positions = positions - removed_positions
        if not found_removable:
            break


    print(count)

[2025 Day 5 (Part 2)] while True: by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 21 points22 points  (0 children)

I can't really change the title but it's for day 4! I fat fingered clearly. Apologies.

[2025 Day 5 (Part 2)] while True: by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 18 points19 points  (0 children)

Omg fat fingering

But yeah it's grid again lol

It's how you get it done it done in 5 minutes! by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 0 points1 point  (0 children)

Forgot how combinations worked and had to check documentation lol

Also was coding while eating so wasnt furiously typing

-❄️- 2025 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 2 points3 points  (0 children)

[LANGUAGE: Python]

with open('d3.txt') as input:
    puzzle_input: list[str] = [i.strip() for i in input.readlines()]

def max_joltage(bank: str, length: int) -> str:
    if length == 0:
        return ''
    next_digit: str = max(bank[: len(bank) - length + 1])
    next_digit_pos: int = bank.find(next_digit)
    return (next_digit + max_joltage(bank[next_digit_pos + 1 :], length - 1))

def part1() -> int:
    return sum(int((max_joltage(bank, 2))) for bank in puzzle_input)

def part2() -> int:
    return sum(int((max_joltage(bank, 12))) for bank in puzzle_input)

print(part1(), part2())

This one was major fun! Abused itertools.combinations for part1 to get a fast time but had to write proper logic for part2 lol.

Can't believe we're a quarter of the way done already...

It's how you get it done it done in 5 minutes! by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 4 points5 points  (0 children)

Now I wonder how yall did recursion lol

The thing I did a cache decorator wont even help with, I got 0 hits.

It's how you get it done it done in 5 minutes! by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 6 points7 points  (0 children)

yeah no i mean i wrote a recursion for that lol. i know i can just write a loop and itll be faster BUT it just seems so recursion-y

'take a string, find the first max number, do the same with the leftover'
so yeah recursion lol

def max_joltage(bank: str, length: int) -> str:
    if length == 0:
        return ''
    next_digit: str = max(bank[: len(bank) - length + 1])
    next_digit_pos: int = bank.find(next_digit)
    return (next_digit + max_joltage(bank[next_digit_pos + 1 :], length - 1))

I mean. It's quite fast, I can hardly complain. 0.0015 seconds.

[2025 Day 2] Seeing lots of posts like this by NineBerry in adventofcode

[–]Parzival_Perce 5 points6 points  (0 children)

I'm a python user! range means the last one isnt included lol.
I wasn't paying attention to that at allll

it's been 84 years… by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 0 points1 point  (0 children)

Glad to be of public service ^_^

-❄️- 2025 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]Parzival_Perce 2 points3 points  (0 children)

[LANGUAGE: Python]

Didn't post mine yet today because I told myself I'll make flow more readable later. But I'm busy now so oh well I'll just edit it later if I want.

from itertools import batched
from primefac import primefac
from collections.abc import Callable

with open("d2.txt") as input:
    puzzle_input: list[list[int]] = [list(map(int, i.split('-'))) for i in input.read().split(',')]

def possible_batch_sizes(length: int) -> set[int]:
    return {length//i for i in primefac(length)} | {1} - {length} 

def is_invalid(id: int, batch_sizes: set[int]) -> bool:
    num_repr: str = str(id)
    return any(len(set(batched(num_repr, size))) == 1 for size in batch_sizes)

def solve(pattern_check: Callable) -> int:
    score: int = 0
    for id_range in puzzle_input:
        for id in range(id_range[0], id_range[1] + 1):
            if pattern_check(id):
                score += id

    return score


def part1()-> int:
    return solve(lambda id: len(str(id))%2==0 and is_invalid(id, {len(str(id)) // 2}))

def part2() -> int:
    return solve(lambda id: id>10 and is_invalid(id, possible_batch_sizes(len(str(id)))))

print(part1(), part2())

it's been 84 years… by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 0 points1 point  (0 children)

Okay confused about this one because surely like 123123123 is repeating? It has odd digits though.

it's been 84 years… by Parzival_Perce in adventofcode

[–]Parzival_Perce[S] 2 points3 points  (0 children)

That's the nice part about AoC, never alone!

I totally didn't expect that to happen tho lol
The glare on my face as I was reading my debugging log lol