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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


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