this post was submitted on 03 Dec 2025
26 points (100.0% liked)

Advent Of Code

1199 readers
15 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 3: Lobby

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Hammerheart@programming.dev 2 points 3 weeks ago

I'm really proud of my solution to part 2. When I first read it, I was freaking stumped, I had no idea how to approach it. I was wondering if I was going to wind up resorting to brute forcing it in factorial time. But then I had an idea on the way home, and I one shotted it! (Got a few tests on the example but first try on the actual input)

from day3_p1 import get_input, get_banks, sample

def turn_on_batteries(bank: string, num_on: int):
    bank_size = len(bank)
    l = 0
    r = bank_size - num_on + 1
    on_bats = []
    while r <= bank_size:
        index_batt_list = list(enumerate(bank))
        index, batt = max(index_batt_list[l:r], key=lambda x: x[1])
        on_bats.append(batt)
        old_l = l
        l = index + 1
        r += 1
    return int("".join(on_bats))


actual = get_input("input")

if __name__ == "__main__":
    all_banks = get_banks(actual, "\n")
    res = 0
    for bank in all_banks:
        res += turn_on_batteries(bank, 12)
    print(res)

Part 1 for completeness:

def get_input(path: str) -> str:
    with open("input") as f:
        data = f.read()
    return data.strip()

def get_banks(data: str, sep=" ") -> list[str]:
    return data.split(sep)

def find_max_battery(bank: str, heap_size=2) -> int:
    batteries = list(enumerate([int(c) for c in bank]))
    first_digit = max(batteries, key=lambda x: x[1])
    if first_digit[0] == len(bank) - 1:
        first_digit = max(batteries[:-1], key=lambda x: x[1])
    second_digit = max(batteries[first_digit[0]+1:], key=lambda x: x[1])
    return first_digit[1] * 10 + second_digit[1]



sample = "987654321111111 811111111111119 234234234234278 818181911112111" 
actual = get_input("input")

DATA = actual
if __name__ == "__main__":
    all_banks = get_banks(DATA, "\n")
    res = 0
    for bank in all_banks:
        res += find_max_battery(bank)
    print(res)