Last active
December 3, 2020 17:53
-
-
Save koox00/9d8de6200c454af767c79089756da86e 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
var ar = document.getElementsByTagName('pre')[0].textContent.split('\n').filter(Boolean) | |
function makeWalk(steps) { | |
return function walk(l, pos) { | |
const lastPos = l.length - 1; | |
if ((pos.x + steps) > lastPos) { | |
return makeWalk(pos.x + steps - lastPos - 1)(l, {x: 0, y: pos.y }) | |
} | |
return { | |
y: pos.y, | |
x: pos.x + steps | |
}; | |
} | |
} | |
function makeTraverse(right, down) { | |
const walk = makeWalk(right); | |
return function traverse(list) { | |
let cursor = { x: 0, y: 0 }; | |
let count = 0; | |
while(cursor.y < list.length - 1) { | |
cursor = walk(list[cursor.y], cursor); | |
cursor.y = cursor.y + down; | |
const node = list[cursor.y][cursor.x]; | |
if(node === '#') { | |
count += 1; | |
} | |
} | |
return count; | |
} | |
} | |
console.log( | |
makeTraverse(1, 1)(ar) * | |
makeTraverse(3, 1)(ar) * | |
makeTraverse(5, 1)(ar) * | |
makeTraverse(7, 1)(ar) * | |
makeTraverse(1, 2)(ar) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment