-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
33 lines (25 loc) · 858 Bytes
/
day7.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
with open("../inputs/day7.txt") as f:
crabs = [int(n) for n in f.readline().strip().split(",")]
def check_fuel_spent(positions, checkValue):
cost = 0
for p in positions:
cost += abs(p - checkValue)
return cost
def check_fuel_spent_fixed(positions, checkValue):
cost = 0
for p in positions:
move = abs(p - checkValue)
if move > 0:
cost += (1 + move) * (move / 2)
return int(cost)
bestHorizontal = None
bestHorizontal2 = None
for i in range(min(crabs), max(crabs)):
cost = check_fuel_spent(crabs, i)
if bestHorizontal == None or cost < bestHorizontal:
bestHorizontal = cost
cost = check_fuel_spent_fixed(crabs, i)
if bestHorizontal2 == None or cost < bestHorizontal2:
bestHorizontal2 = cost
print(f"7-1: {bestHorizontal}")
print(f"7-2: {bestHorizontal2}")