-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021-07Whales.py
38 lines (30 loc) · 1.01 KB
/
2021-07Whales.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
from collections import Counter
def fuelpart1(positions, to):
fuel_needed = 0
positions_dict = Counter(positions)
for key, value in positions_dict.items():
if key < to:
fuel_needed += (to - key) * value
else:
fuel_needed += (key - to) * value
return fuel_needed
def fuelpart2(positions, to):
fuel_needed = 0
positions_dict = Counter(positions)
for key, value in positions_dict.items():
if key < to:
fuel_needed += (to - key)*(to - key +1)/2 * value
else:
fuel_needed += (key - to)*(key - to +1)/2 * value
return fuel_needed
with open('2021-07Whales.txt') as f:
lines = f.read().splitlines()
positions = list(map(int, lines[0].split(",")))
cost = []
for i in range(max(positions)):
cost.append(fuelpart1(positions, i))
print("Part 1, the minimum fuel is", min(cost))
cost = []
for i in range(max(positions)):
cost.append(fuelpart2(positions, i))
print("Part 1, the minimum fuel is", int(min(cost)))