Added Haskell Implementations and changed directory structure
This commit is contained in:
parent
9f40c27eb9
commit
cb9dee889b
14 changed files with 392 additions and 0 deletions
23
Python/2021/07/day7.py
Normal file
23
Python/2021/07/day7.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import statistics
|
||||
with open("input.txt", 'r') as f:
|
||||
data = f.readlines()
|
||||
data = list(map(int, data[0].strip().split(",")))
|
||||
|
||||
# Part 1
|
||||
median = statistics.median(data)
|
||||
fuelCost = 0
|
||||
for crab in data:
|
||||
fuelCost += abs(median - crab)
|
||||
|
||||
print(fuelCost)
|
||||
|
||||
# Part 2
|
||||
flist = []
|
||||
# Definitely a better way than checking all of these
|
||||
for pos in range(0, max(data)+1):
|
||||
fuel = 0
|
||||
for crab in data:
|
||||
fuel += (abs(pos - crab)*(abs(pos - crab)+1))/2
|
||||
flist.append(fuel)
|
||||
|
||||
print(min(flist))
|
||||
20
Python/2021/07/day7_initial.py
Normal file
20
Python/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