-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_02.py
43 lines (33 loc) · 1.1 KB
/
07_02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def load_data():
data_file = open("data/" + "7.txt", "r")
lines = data_file.readlines()
crabs = [int(s) for s in lines[0].split(",")]
return crabs
def check_fuel(position):
total_fuel_usage = 0
for crab in crab_data:
steps = 0
steps += abs(position - crab)
fuel_usage = sum(range(steps+1))
total_fuel_usage += fuel_usage
#if position == 5:
#print (f'Start {position} end {crab} steps {steps} usage {fuel_usage}')
return total_fuel_usage
crab_data = load_data()
#crab_data.sort()
max_value = max(crab_data)
min_value = min(crab_data)
#print(f'There are {len(crab_data)}. The Highest: {max_value} The Lowest: {min_value}')
best_position = None
best_fuel = None
for i in range(min_value, max_value + 1):
calculated_fuel = check_fuel(i)
#print(calculated_fuel)
if best_fuel == None:
best_position = i
best_fuel = calculated_fuel
else:
if best_fuel > calculated_fuel:
best_position = i
best_fuel = calculated_fuel
print(f'The Best Position is {best_position} with {best_fuel}')