Skip to content

Instantly share code, notes, and snippets.

@kevincharm
Last active December 17, 2017 06:22
Show Gist options
  • Save kevincharm/479111a96b3d5d0733db0738ecdbef27 to your computer and use it in GitHub Desktop.
Save kevincharm/479111a96b3d5d0733db0738ecdbef27 to your computer and use it in GitHub Desktop.
rank 329/700
// -*- 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