mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 08:21:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
35
adventofcode/2021/day7/day7.py
Normal file
35
adventofcode/2021/day7/day7.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
def part1(crabs):
|
||||
moves = OrderedDict()
|
||||
for pos in range(min(crabs), max(crabs) + 1):
|
||||
# calculate total fuel required to move to pos:
|
||||
for crab in crabs:
|
||||
fuel_cost = abs(pos - crab)
|
||||
moves[pos] = moves.get(pos, 0) + fuel_cost
|
||||
|
||||
min_move = min(moves, key=moves.get)
|
||||
print(f"Part 1, min move {min_move}, cost {moves[min_move]}")
|
||||
|
||||
def part2(crabs):
|
||||
moves = OrderedDict()
|
||||
for pos in range(min(crabs), max(crabs) + 1):
|
||||
# calculate total fuel required to move to pos:
|
||||
for crab in crabs:
|
||||
dx = abs(pos - crab)
|
||||
# S = (n+1)(u0 + un)/2
|
||||
fuel_cost = dx * (dx+1)//2
|
||||
moves[pos] = moves.get(pos, 0) + fuel_cost
|
||||
|
||||
min_move = min(moves, key=moves.get)
|
||||
print(f"Part 1, min move {min_move}, cost {moves[min_move]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
|
||||
with open(infile) as f:
|
||||
crabs = [int(x) for x in f.readline().split(",")]
|
||||
part1(crabs)
|
||||
part2(crabs)
|
||||
|
Reference in New Issue
Block a user