Skip to content

Instantly share code, notes, and snippets.

@spielkind
Created December 7, 2021 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spielkind/3761dbd7bb916e80a984e2f870ee6ac4 to your computer and use it in GitHub Desktop.
Save spielkind/3761dbd7bb916e80a984e2f870ee6ac4 to your computer and use it in GitHub Desktop.
#!/bin/python
with open('day7.txt') as f:
crabs = list(map(int, next(f).strip().split(',')))
pl, ph = min(crabs), max(crabs)+1
fn1 = list(range(ph))
fn2 = {num: num*(num+1)//2 for num in range(ph)}
def opt(fn):
return min(sum(fn[abs(tp-cp)] for cp in crabs) for tp in range(pl, ph))
print('Minimum fuel required part one:', opt(fn1))
print('Minimum fuel required part two:', opt(fn2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment