-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06_pt1.js
42 lines (39 loc) · 1.02 KB
/
day06_pt1.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
const input = require("fs").readFileSync("day06_input.txt").toString().split(" ")
let memory = input.map((x) => parseInt(x))
let memoryStates = []
let dupeState = false
while (!dupeState) {
reallocateMemory(memory)
memoryStates.push(memory.toString())
dupeState = detectDupeStates(memoryStates)
console.log({ memory }, { memoryStates })
console.log(`the answer is: ${memoryStates.length}`)
}
function reallocateMemory(memory) {
let max = Math.max(...memory)
//console.log(max)
let index = returnMaxBankIndex(memory, max)
//console.log(index)
for (let i = 0; i <= max; i++) {
if (i === 0) {
memory[index] = 0
} else {
memory[(index + i) % memory.length]++
}
}
}
function detectDupeStates(memoryStates) {
let setMemoryStates = new Set(memoryStates)
if (memoryStates.length === setMemoryStates.size) {
return false
} else {
return true
}
}
function returnMaxBankIndex(memory, max) {
for (let i = 0; i < memory.length; i++) {
if (memory[i] === max) {
return i
}
}
}