Initial advent of code (2021 06,07,08)
This commit is contained in:
parent
ac3c96dbe3
commit
9f40c27eb9
5 changed files with 132 additions and 0 deletions
20
2021/07/day7_initial.py
Normal file
20
2021/07/day7_initial.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
with open("input.txt", 'r') as f:
|
||||
data = f.readlines()
|
||||
data = list(map(int, data[0].strip().split(",")))
|
||||
|
||||
def crabMove(crab, pos):
|
||||
difference = abs(pos - crab)
|
||||
# return difference
|
||||
return (difference*(difference+1))/2
|
||||
|
||||
bestPos = 0
|
||||
bestFuel = 1000000000
|
||||
for i in range(min(data), max(data)):
|
||||
fuelCost = 0
|
||||
for crab in data:
|
||||
fuelCost += crabMove(crab, i)
|
||||
if fuelCost < bestFuel:
|
||||
bestFuel = fuelCost
|
||||
bestPos = i
|
||||
|
||||
print(bestFuel)
|
||||
Loading…
Add table
Add a link
Reference in a new issue