-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06_pt2.js
61 lines (57 loc) · 1.47 KB
/
day06_pt2.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
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)
}
let dupedMemory = [...memory]
console.log({ dupedMemory }, { memory })
let cycleCount = 0
reallocateMemory(memory)
cycleCount++
console.log({ dupedMemory }, { memory })
let initialMemoryState = false
while (!initialMemoryState) {
reallocateMemory(memory)
cycleCount++
initialMemoryState = checkForInitialMemoryState(dupedMemory, memory)
}
console.log(`The answer is ${cycleCount}`)
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
}
}
}
function checkForInitialMemoryState(initialMemory, memory) {
if (initialMemory.toString() === memory.toString()) {
return true
} else {
return false
}
}