Skip to content

Instantly share code, notes, and snippets.

@KimSarabia
Created October 10, 2017 20:30
Show Gist options
  • Save KimSarabia/88d4f9adb58bf894172d70f1608a6811 to your computer and use it in GitHub Desktop.
Save KimSarabia/88d4f9adb58bf894172d70f1608a6811 to your computer and use it in GitHub Desktop.
Best Travel
// Work in Progress
function chooseBestSum(t, k, ls) {
if(ls.length <= 1){return null}
else {
return Array
.apply(0, { length: Math.pow(k+1, ls.length) })
.map(Number.call, Number)
.map(a => a
.toString(ls.length)
.split('')
.sort()
.filter((a, b, c) => c.indexOf(a) == b)
.join(''))
.filter((a, b, c) => a.length == k && c.indexOf(a) == b)
.map(x => x.split('').map(y => ls[+y]))
.map(x => x.reduce((pv,cv) => pv+cv, 0))
.filter(x => x <= t)
.reduce((a,b) => Math.max(a,b))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment