-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1441.js
executable file
·101 lines (77 loc) · 2.37 KB
/
1441.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//// READING FILE | STREAMS ////
const { createReadStream } = require("node:fs")
const { createInterface } = require("node:readline")
const PATH = "/dev/stdin"
const ENCODING = "utf8"
/**
* @param {string} path
* @param {BufferEncoding} encoding
*/
function createReadLineInterface(path, encoding) {
return createInterface({
input: createReadStream(path, encoding),
crlfDelay: Infinity,
terminal: false
})
}
const RLI = createReadLineInterface(PATH, ENCODING)
/** @param {import("readline").Interface} readLineInterface */
const processLineByLine = function (readLineInterface) {
let EOF = false
const nextLineGenerator = (async function* () {
for await (const line of readLineInterface) yield line
EOF = true
})()
return {
hasNextLine: () => !EOF,
nextLine: async (fn) => {
const { value } = (await nextLineGenerator.next())
return (typeof fn === "function") ? fn(value) : value
}
}
}
//// UTILS ////
const isOdd = (num = 0) => Math.abs(Number(num)) % 2 === 1
const isEven = (num = 1) => Math.abs(Number(num)) % 2 === 0
//// GRANIZO CLASS ////
class Granizo {
static #memo = {}
/**
* Returns the max value of the given sequence initialized for the given `h`
* @param {number} h The initial value of the granizo's sequence
*/
static max(h) {
// If the maximum value of the sequence already calculated, then return it
if (Reflect.has(Granizo.#memo, h)) return Reflect.get(Granizo.#memo, h)
const H = h
let maximum = 1
while (h != 1) {
// hn = { ½ x hn-1 se hn-1 é par;
// hn = { 3 x hn-1 + 1 se hn-1 é ímpar.
// The biggest value in sequence always will be an even number
if (h > maximum) maximum = h
if (isEven(h)) { h /= 2 }
else if (isOdd(h)) { h = 3 * h + 1 }
if (Reflect.has(Granizo.#memo, h)) {
maximum = Math.max(maximum, Reflect.get(Granizo.#memo, h))
break
}
}
return (Reflect.set(Granizo.#memo, H, maximum), Reflect.get(Granizo.#memo, H))
}
// static get memo() { return Granizo.#memo }
}
//// MAIN ////
async function main() {
const output = []
const readLineInstance = processLineByLine(RLI)
const readLine = readLineInstance.nextLine.bind(undefined, (n = "") => Number.parseInt(n, 10))
while (readLineInstance.hasNextLine()) {
const H = await readLine()
if (H === 0) break
else if (1 <= H && H <= 500) output.push(Granizo.max(H))
else continue
}
console.log(output.join("\n"))
}
main()