Day03
This commit is contained in:
58
2025/gareth/day03/poc.py
Normal file
58
2025/gareth/day03/poc.py
Normal file
@@ -0,0 +1,58 @@
|
||||
def part1(input_str):
|
||||
lines = input_str.strip().split("\n")
|
||||
output = 0
|
||||
|
||||
for bank in lines:
|
||||
best = 0
|
||||
length = len(bank)
|
||||
|
||||
for i in range(length):
|
||||
tens = int(bank[i])
|
||||
for j in range(i + 1, length):
|
||||
ones = int(bank[j])
|
||||
value = tens * 10 + ones
|
||||
if value > best:
|
||||
best = value
|
||||
|
||||
output += best
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def part2(input_str, k = 12):
|
||||
lines = input_str.strip().split("\n")
|
||||
total = 0
|
||||
|
||||
for bank in lines:
|
||||
bank = bank.strip()
|
||||
n = len(bank)
|
||||
start = 0
|
||||
chosen = []
|
||||
|
||||
for picked in range(k):
|
||||
last = n - (k - picked)
|
||||
best_char = bank[start]
|
||||
best_idx = start
|
||||
|
||||
for i in range(start + 1, last + 1):
|
||||
if bank[i] > best_char:
|
||||
best_char = bank[i]
|
||||
best_idx = i
|
||||
|
||||
if best_char == '9':
|
||||
break
|
||||
|
||||
chosen.append(best_char)
|
||||
start = best_idx + 1
|
||||
|
||||
total += int("".join(chosen))
|
||||
|
||||
return total
|
||||
|
||||
|
||||
|
||||
with open("input.txt") as f:
|
||||
data = f.read()
|
||||
|
||||
print("Part1:", part1(data))
|
||||
print("Part2:", part2(data, 12))
|
||||
Reference in New Issue
Block a user