Compare commits

...

2 Commits

Author SHA1 Message Date
2cc595523d day 3 refactor + comments 2025-12-03 06:17:14 +00:00
ecbbd39af1 day 3 simplify 2025-12-03 05:55:47 +00:00

View File

@ -1,34 +1,26 @@
input = open("input") input = open("input")
t = 0 part1 = 0
part2 = 0
shift = [0] + [10**i for i in range(12)]
for line in input: for line in input:
cells = list(line.strip()) digits = list(line.strip())
cells.reverse() # max[n] is the value of the maximal n digit subsequence
b = cells[0] max = [0] * 13
max = 0 # iterate over the digits in least-significant to most-significant order.
for c in cells[1:]: # k is the number of digits already processed.
j = int(c+b) for (k,c) in enumerate(reversed(digits)):
if j > max: c = int(c)
max = j # the maximal n-digit subsequence we can make that starts with the
if c > b: # current digit (c) is equal to the concatenation of c and the
b = c # maximal (n-1)-digit subsequence.
#print(max) # if that's more than the best value we've seen so far, update max[n].
t += max # we have to update max in reverse order in order to avoid reusing digits.
print(t)
# Part 2
input = open("input")
t = 0
for line in input:
cells = list(line.strip())
cells.reverse()
max = ["0"*i for i in range(12+1)]
for (k,c) in enumerate(cells):
for i in reversed(range(1,min(k+2,len(max)))): for i in reversed(range(1,min(k+2,len(max)))):
j = c+max[i-1] joltage = c*shift[i] + max[i-1]
if j > max[i]: if joltage > max[i]:
max[i] = j max[i] = joltage
print(max[12]) #print(max[12])
t += int(max[12]) part1 += int(max[2])
print(t) part2 += int(max[12])
print(part1)
print(part2)