2025(03): solve puzzle part 2
This commit is contained in:
62
2025/03/part2.py
Normal file
62
2025/03/part2.py
Normal file
@ -0,0 +1,62 @@
|
||||
# I did not do anything to prevent the main code from running in part1.
|
||||
#from part1 import bruteforce_joltage
|
||||
|
||||
debug=False
|
||||
|
||||
def joltage_limit_safety_override(battery, jol_len=12):
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
jol_indices = []
|
||||
idx_seek_start = 0
|
||||
for i in range(jol_len):
|
||||
if debug:
|
||||
print(f"Look for number {i}")
|
||||
# Find, in order, the highest possible number allowed.
|
||||
for num in range(10):
|
||||
needle = str(9 - num)
|
||||
if debug:
|
||||
print(f"Try to find number {needle} in:")
|
||||
subbattery = battery[idx_seek_start:-(jol_len - i + 0)]
|
||||
if debug:
|
||||
print(subbattery)
|
||||
if not needle in subbattery:
|
||||
if debug:
|
||||
print("not found")
|
||||
continue
|
||||
idx_needle = subbattery.index(needle)
|
||||
jol_indices.append(idx_needle + idx_seek_start)
|
||||
idx_seek_start = idx_needle + idx_seek_start + 1
|
||||
if debug:
|
||||
print("found")
|
||||
break
|
||||
if debug:
|
||||
print(jol_indices)
|
||||
print(int("".join([battery[i] for i in jol_indices])))
|
||||
return int("".join([battery[i] for i in jol_indices]))
|
||||
|
||||
# Test testinput
|
||||
with open("testinput", "r") as fp:
|
||||
batteries = fp.readlines()
|
||||
|
||||
for idx_battery, joltage in enumerate([987654321111, 811111111119, 434234234278, 888911112111]):
|
||||
assert joltage_limit_safety_override(batteries[idx_battery]) == joltage
|
||||
|
||||
output_joltage = 0
|
||||
for battery in batteries:
|
||||
joltage_battery_max = joltage_limit_safety_override(battery)
|
||||
output_joltage += joltage_battery_max
|
||||
|
||||
assert output_joltage == 3121910778619
|
||||
|
||||
# Perform final stuff
|
||||
with open("input", "r") as fp:
|
||||
batteries = fp.readlines()
|
||||
|
||||
output_joltage = 0
|
||||
for battery in batteries:
|
||||
joltage_battery_max = joltage_limit_safety_override(battery)
|
||||
output_joltage += joltage_battery_max
|
||||
|
||||
print(output_joltage)
|
||||
Reference in New Issue
Block a user