AOC 2021 - Day 7
The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?
I miss the bingo squid.
Times for today: about 5 minutes. This one was easy; I just had to good the closed-form solution to triangle numbers for part 2. The naive solution would probably have worked just fine though. Generators are the best; they’re one of my favorite python tools, but I almost never use them in production code. They’re perfect for stuff like this though.
def find_min_fuel(crabs):
return min(sum(abs(c-i) for c in crabs) for i in range(max(crabs)))
def find_min_fuelv2(crabs):
return min(sum((abs(c-i)*(abs(c-i) + 1)) / 2 for c in crabs) for i in range(max(crabs)))