Last active
December 17, 2017 06:22
-
-
Save kevincharm/479111a96b3d5d0733db0738ecdbef27 to your computer and use it in GitHub Desktop.
rank 329/700
This file contains hidden or 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
// -*- node.jz -*- | |
let input = '' | |
process.stdin.on('readable', () => input += process.stdin.read() || '') | |
process.stdin.on('end', () => main()) | |
function part1(steps) { | |
const buf = [0] | |
let pos = 0 | |
for (let i=1; i<=2017; i++) { | |
pos = (pos + steps + 1) % buf.length | |
buf.splice(pos+1, 0, i) | |
} | |
return buf[(pos+2) % buf.length] | |
} | |
function part2(steps) { | |
let val | |
let pos = 0 | |
for (let i=1; i<=50e6; i++) { | |
pos = (pos + steps + 1) % i | |
if (pos === 0) val = i | |
} | |
return val | |
} | |
function main() { | |
const steps = ~~input | |
console.log(`Part 1: ${part1(steps)}`) | |
console.log(`Part 2: ${part2(steps)}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment