53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
# with open('./2021/07.test') as input_file:
|
|
with open('./2021/07.input') as input_file:
|
|
raw_input = input_file.read()
|
|
|
|
input_data = raw_input.split(',')
|
|
input_data = [int(x) for x in input_data]
|
|
|
|
print(input_data)
|
|
print(max(input_data))
|
|
print(min(input_data))
|
|
|
|
# Calculate the fuel cost of each position. Keep the lowest.
|
|
|
|
best_position = min(input_data)
|
|
best_fuel = 0
|
|
|
|
for one_position in range(min(input_data), max(input_data) + 1, 1):
|
|
fuel_cost = 0
|
|
for one_crab in input_data:
|
|
fuel_cost += abs(one_position - one_crab)
|
|
|
|
if best_position == one_position:
|
|
best_fuel = fuel_cost
|
|
if fuel_cost < best_fuel:
|
|
best_position = one_position
|
|
best_fuel = fuel_cost
|
|
|
|
print("Linear Growth")
|
|
print(best_position)
|
|
print(best_fuel)
|
|
|
|
best_position = min(input_data)
|
|
best_fuel = 0
|
|
|
|
for one_position in range(min(input_data), max(input_data) + 1, 1):
|
|
fuel_cost = 0
|
|
for one_crab in input_data:
|
|
for one_fuel in range(abs(one_position - one_crab) + 1):
|
|
fuel_cost += one_fuel
|
|
if best_fuel > 0 and fuel_cost > best_fuel:
|
|
break
|
|
|
|
if best_position == one_position:
|
|
best_fuel = fuel_cost
|
|
if fuel_cost < best_fuel:
|
|
best_position = one_position
|
|
best_fuel = fuel_cost
|
|
print(one_position, end='\r')
|
|
|
|
print("Crab Growth")
|
|
print(best_position)
|
|
print(best_fuel)
|