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
[โ€“] Pyro@programming.dev 4 points 3 weeks ago* (last edited 3 weeks ago)

Python

This was the easier one for me out of the first 3 days. Cleaned up my solution before posting for better readability:

# get joltage of picked batteries
def get_joltage(batteries_picked: list[int]):
    bank_joltage = 0
    for batt in batteries_picked:
        bank_joltage = bank_joltage * 10 + batt
    return bank_joltage

# get maximum joltage of a bank
def get_bank_joltage(bank: str, pick_limit = 2) -> int:
    # pick first <pick_limit> batteries
    batteries_picked = [int(bank[i]) for i in range(pick_limit)]
    max_joltage = get_joltage(batteries_picked)

    # iterate over remaining batteries
    for i in range(pick_limit, len(bank)):
        batt = int(bank[i])        
        # we add batt for selection consideration
        batteries_picked.append(batt)
        # If all batteries are in descending order and batt is the lowest, 
        #   we will eventually discard batt
        to_discard = pick_limit

        # However if not, we discard the leftmost MSB battery which has lower joltage than its successor
        #   and shift all batteries left with batt added at the end.
        # This guarantees that we keep the maximum lexicographical order of picked batteries
        #   regardless of batt's value.
        for i in range(pick_limit):
            if batteries_picked[i] < batteries_picked[i+1]:
                to_discard = i
                break
        batteries_picked.pop(to_discard)

        # update max_joltage, it may have increased
        max_joltage = max(max_joltage, get_joltage(batteries_picked))

    return max_joltage

# part 1 asserts
assert get_bank_joltage("987654321111111", pick_limit=2) == 98
assert get_bank_joltage("811111111111119", pick_limit=2) == 89
assert get_bank_joltage("234234234234278", pick_limit=2) == 78
assert get_bank_joltage("818181911112111", pick_limit=2) == 92

# part 2 asserts
assert get_bank_joltage("987654321111111", pick_limit=12) == 987654321111
assert get_bank_joltage("811111111111119", pick_limit=12) == 811111111119
assert get_bank_joltage("234234234234278", pick_limit=12) == 434234234278
assert get_bank_joltage("818181911112111", pick_limit=12) == 888911112111

# get total joltage of a set of banks
def solve(data: str, pick_limit = 2):
    total_joltage = 0
    for bank in data.splitlines():
        total_joltage += get_bank_joltage(bank, pick_limit)
    return total_joltage

# asserts for sample data
sample = """987654321111111
811111111111119
234234234234278
818181911112111"""
assert solve(sample, pick_limit=2) == 357               # part 1
assert solve(sample, pick_limit=12) == 3121910778619    # part 2