Initial advent of code (2021 06,07,08)

This commit is contained in:
Julia Lange 2021-12-08 15:29:52 -08:00
parent ac3c96dbe3
commit 9f40c27eb9
Signed by: Julia
SSH key fingerprint: SHA256:KI8YxpkPRbnDRkXPgCuQCVz181++Vy7NAvmQj8alOhM
5 changed files with 132 additions and 0 deletions

23
2021/07/day7.py Normal file
View 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
2021/07/day7_initial.py Normal file
View 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)