-
-
Save Mellen/7d1fb9af13fc3f14ec8372b7beec7e7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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