-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09_pt2.js
59 lines (54 loc) · 1.17 KB
/
day09_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
const input = require("fs").readFileSync("day09_input.txt").toString()
let test = []
for (let i = 0; i < input.length; i++) {
test.push(input[i])
}
let state = {
nestingLevel: 0,
garbage: false,
total: 0,
}
//test is primary iterative source
//target is copy of test, that we will mutate
// parse basic nested {}
let target = [...test]
for (let i = 0; i < test.length; i++) {
//console.log(`char: ${test[i]}`)
if (state.garbage) {
switch (test[i]) {
case ">":
//console.log("> case triggered")
state.garbage = false
break
case "!":
i++
break
default:
target.splice(i, 1, "*")
}
} else {
switch (test[i]) {
case "{":
state.nestingLevel++
break
case "}":
state.total += state.nestingLevel
state.nestingLevel--
break
case "<":
state.garbage = true
break
}
}
//console.log({ state })
}
//now that we have marked target with placeholder removal "!" we will remove it from target
let count = 0
target.forEach((x) => {
if (x === "*") {
count++
} else {
}
})
console.log(target)
console.log(`The answer is ${count}`)