Skip to content

Instantly share code, notes, and snippets.

@garciasdos
Created December 2, 2021 10:09
Show Gist options
  • Save garciasdos/e75414f4465d4d353852cfaac71e1139 to your computer and use it in GitHub Desktop.
Save garciasdos/e75414f4465d4d353852cfaac71e1139 to your computer and use it in GitHub Desktop.
const input = document.children[0].textContent.split('\n').filter(x => x.length).map(movement => movement.split(' '))
input.reduce((acc, currentTuple, index, array) => {
switch(currentTuple[0]) {
case "forward": acc[0] = acc[0] + parseInt(currentTuple[1]); break;
case "up" : acc[1] = acc[1] + parseInt(currentTuple[1]); break;
case "down": acc[1] = acc[1] - parseInt(currentTuple[1]); break;
}
return acc;
}, [0, 0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment