day 3 refactor + comments
This commit is contained in:
parent
ecbbd39af1
commit
2cc595523d
23
day03/sol.py
23
day03/sol.py
@ -1,15 +1,24 @@
|
||||
input = open("input")
|
||||
part1 = 0
|
||||
part2 = 0
|
||||
shift = [0] + [10**i for i in range(12)]
|
||||
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):
|
||||
digits = list(line.strip())
|
||||
# max[n] is the value of the maximal n digit subsequence
|
||||
max = [0] * 13
|
||||
# iterate over the digits in least-significant to most-significant order.
|
||||
# k is the number of digits already processed.
|
||||
for (k,c) in enumerate(reversed(digits)):
|
||||
c = int(c)
|
||||
# the maximal n-digit subsequence we can make that starts with the
|
||||
# current digit (c) is equal to the concatenation of c and the
|
||||
# maximal (n-1)-digit subsequence.
|
||||
# if that's more than the best value we've seen so far, update max[n].
|
||||
# we have to update max in reverse order in order to avoid reusing digits.
|
||||
for i in reversed(range(1,min(k+2,len(max)))):
|
||||
j = c+max[i-1]
|
||||
if j > max[i]:
|
||||
max[i] = j
|
||||
joltage = c*shift[i] + max[i-1]
|
||||
if joltage > max[i]:
|
||||
max[i] = joltage
|
||||
#print(max[12])
|
||||
part1 += int(max[2])
|
||||
part2 += int(max[12])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user