40 lines
971 B
Python
40 lines
971 B
Python
# Test testinput
|
|
with open("testinput", "r") as fp:
|
|
batteries = fp.readlines()
|
|
|
|
def bruteforce_joltage(battery):
|
|
"""
|
|
Makes all combinations and returns the largests
|
|
|
|
Complexity: O(len(battery))
|
|
"""
|
|
|
|
joltage_battery_max = 0
|
|
for i in range(len(battery) - 1):
|
|
for j in range(i + 1, len(battery)):
|
|
joltage = int(battery[i] + battery[j])
|
|
if joltage > joltage_battery_max:
|
|
joltage_battery_max = joltage
|
|
return joltage_battery_max
|
|
|
|
for idx_battery, joltage in enumerate([98, 89, 78, 92]):
|
|
assert bruteforce_joltage(batteries[idx_battery]) == joltage
|
|
|
|
output_joltage = 0
|
|
for battery in batteries:
|
|
joltage_battery_max = bruteforce_joltage(battery)
|
|
output_joltage += joltage_battery_max
|
|
|
|
assert output_joltage == 357
|
|
|
|
# Perform final stuff
|
|
with open("input", "r") as fp:
|
|
batteries = fp.readlines()
|
|
|
|
output_joltage = 0
|
|
for battery in batteries:
|
|
joltage_battery_max = bruteforce_joltage(battery)
|
|
output_joltage += joltage_battery_max
|
|
|
|
print(output_joltage)
|