-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2925.js
executable file
·44 lines (33 loc) · 902 Bytes
/
2925.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8")
.split("\n")
.map((line) => Number.parseInt(line, 10))
const MEMO = new Map([
[0, 0n],
[1, 1n],
])
function reverseDigits(num = 0n, base = 10, bits = 64) {
return BigInt.asUintN(
bits,
BigInt(num.toString(base).split("").reverse().join(""))
)
}
/** @param {number} index */
function mirroring(index) {
if (index <= 0) return 0n
if (index == 1) return 1n
if (index == 2) return 2n
const prevA = index - 1
const prevB = index - 2
if (!MEMO.has(prevA)) MEMO.set(prevA, mirroring(prevA))
if (!MEMO.has(prevB)) MEMO.set(prevB, mirroring(prevB))
return reverseDigits(MEMO.get(prevA)) + reverseDigits(MEMO.get(prevB))
}
function main() {
const responses = []
for (const nth of input)
if (isNaN(nth)) break
else responses.push(mirroring(nth))
console.log(responses.join("\n"))
}
main()