Skip to content

Instantly share code, notes, and snippets.

@zwhitchcox
Last active March 8, 2023 19:37
Show Gist options
  • Save zwhitchcox/7eec4923db0505e3fb3258a56eb98825 to your computer and use it in GitHub Desktop.
Save zwhitchcox/7eec4923db0505e3fb3258a56eb98825 to your computer and use it in GitHub Desktop.
findStart
function findStart(strArr) {
const stations = strArr.slice(1).map((station) => station.split(':').map(parseInt))
for (let i = 0; i < stations.length; i++) {
for (let i = 0; i < stations.length; i++) {
let curGal = 0
let total = 0;
for (let j = 0; j < stations.length; j++) {
const [gallons] = stations[i + j % stations.length]
const [_, needed] = stations[(i + j + 1) % stations.length]
curGal += gallons;
if (curGal < needed) {
break;
} else {
total++;
curGal -= needed;
}
}
if (total === stations.length) {
return i + 1;
}
}
}
return "impossible";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment