Try an interactive version of this dialog: Sign up at solve.it.com, click Upload, and pass this URL.

Code: 60

from aocd import get_data
inp = get_data(year=2025, day=3)
len(inp), len(inp.splitlines()), len(inp.splitlines()[0]), inp[:100]

Code: 42 ()

samp = """987654321111111
811111111111119
234234234234278
818181911112111"""

Note: 112

Within each bank, you need to turn on exactly two batteries; the joltage that the bank produces is equal to the number formed by the digits on the batteries you've turned on. For example, if you have a bank like 12345 and you turn on batteries 2 and 4, the bank would produce 24 jolts. (You cannot rearrange batteries.)

Note: 52

You'll need to find the largest possible joltage each bank can produce. In the above example:

e.g. line one samp should be 98, line 2 89

Code: 19 ()

lines = samp.splitlines()
x = lines[0]
x

Output: 33

'987654321111111'

Code: 18 ()

l = sorted(list(set(x)))[-1]
l

Output: 27

'9'

Code: 12 ()

idx = x.find(largest)
idx

Output: 16

0

Code: 28 ()

rest = x[idx+1:]
nl = sorted(list(set(rest)))[-1]
nl

Output: 27

'8'

Code: 18 ()

joltage = int(l+nl)
joltage

Output: 16

98

Code: 96 ()

def get_joltage(x):
    l = sorted(list(set(x[:-1])))[-1]
    idx = x.find(l)
    nl = sorted(list(set(x[idx+1:])))[-1]
    return int(l+nl)

[get_joltage(x) for x in samp.splitlines()]

Output: 30

[98, 89, 78, 92]

Code: 40 ()

def solvea(samp): return sum([get_joltage(x) for x in samp.splitlines()])
solvea(samp)

Output: 16

357

Code: 6 ()

solvea(inp)

Output: 18

17332

Note: 19

Out two-digit joltage could never start on the last charachter

Note: 6

Part 2

Note: 10

We want 12 numbers total now

Code: 10 ()

x = lines[2]
x

Output: 33

'234234234234278'

Code: 6 ()

idx = 0

Code: 12 ()

ss = x[idx:-11]
ss

Output: 28

'2342'

Code: 18 ()

d = sorted(list(set(ss)))[-1]
d

Output: 27

'4'

Code: 16 ()

idx = ss.find(d)+idx+1
idx

Output: 16

3

Code: 12 ()

ss = x[idx:-10]
ss

Output: 27

'23'

Code: 133 ()

def get_joltage(x):
    idx = 0
    digits = []
    for i in range(11, -1, -1):
        ss = x[idx:-i] if i > 0 else x[idx:]
        d = sorted(list(set(ss)))[-1]
        digits.append(d)
        idx = ss.find(d)+idx+1
    return "".join(digits)


get_joltage(x)

Output: 31

'434234234278'

Code: 6 ()

434234234278

Output: 21

434234234278

Code: 42 ()

def solvea(samp): return sum([int(get_joltage(x)) for x in samp.splitlines()])
solvea(samp)

Output: 22

3121910778619

Code: 6 ()

solvea(inp)

Output: 22

172516781546707

Prompt: 15

Can you see any things we could do better?

Assistant: 315

Looking at your code, I can see a few areas where you might improve things:

  1. The x[:-1] in part 1: In get_joltage, you're using x[:-1] when finding the largest digit, which excludes the last character. This works for the constraint that a two-digit number can't start on the last character, but it's a bit subtle. Is this intentional, or might it be clearer to use a different approach?

  2. Variable naming: Some names like l, nl, d, and ss are quite short. Would more descriptive names help you understand the code later?

  3. The slice logic in part 2: The x[idx:-i] if i > 0 else x[idx:] works, but it's a bit tricky to follow. Have you considered whether there's a clearer way to express this?

Which of these would you like to explore first, or is there a specific part of your solution you're wondering about?

Prompt: 19

Can you see a nicer way for 3? Show me!

Assistant: 222

Sure! For the slicing logic, you could calculate how many characters you need to leave at the end and use that directly:

Copied!
remaining = 12 - len(digits)  # or (11 - i)
ss = x[idx:] if remaining == 0 else x[idx:-remaining]

Or even simpler, you could calculate the end index explicitly:

Copied!
end_idx = len(x) - (11 - i)
ss = x[idx:end_idx]

This makes it clearer that you're selecting from idx up to a specific position, leaving exactly the right number of characters for the remaining digits.

Does this make sense? Would you like to try refactoring your loop with one of these approaches?