Skip to content

Instantly share code, notes, and snippets.

@Mellen
Created December 7, 2021 09:04
Show Gist options
  • Save Mellen/7d1fb9af13fc3f14ec8372b7beec7e7e to your computer and use it in GitHub Desktop.
Save Mellen/7d1fb9af13fc3f14ec8372b7beec7e7e to your computer and use it in GitHub Desktop.
(function(inp)
{
let crabs = inp.split(',').map(v => parseInt(v,10));
let bestCrab = Math.floor(crabs.reduce((s,v)=>s+v,0)/crabs.length)
let fuel = crabs.map(crab => Math.abs(crab-bestCrab)).map(c => c*(c+1)/2).reduce((s,v)=>s+v,0);
return fuel;
})(document.querySelector('pre').innerHTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment