Compare commits

..

No commits in common. "2cc595523d8af3f5488533d82f016021d3c2c75c" and "3698cf9c35074fc179a76b5d60df3d852fa1f7a5" have entirely different histories.

View File

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