-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07_pt1.js
52 lines (46 loc) · 1.15 KB
/
day07_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
43
44
45
46
47
48
49
50
51
52
const input = require("fs")
.readFileSync("day07_input.txt")
.toString()
.split(/\r?\n/)
//console.log(input)
let tower = {}
//build tower object with "holding" values only
// programName: {
// holding: []
// heldBy: []
// }
input.forEach((line) => {
let lineArray = line.split(" ")
//console.log(lineArray)
let program = {
holding: [],
heldBy: "",
}
if (lineArray.length > 3) {
for (let i = 3; i < lineArray.length; i++) {
let heldProgram = lineArray[i].replace(",", "")
program.holding.push(heldProgram)
}
}
let towerName = lineArray[0]
tower[towerName] = program
})
//add heldBy to each program in the tower
for (const program in tower) {
//console.log(program)
if (tower[program].holding.length > 0) {
tower[program].holding.forEach((heldProgram) => {
//console.log(` ${heldProgram} is being updated `)
tower[heldProgram].heldBy = program
let test = tower[heldProgram].heldBy
})
}
}
let answer
for (const program in tower) {
if (tower[program].holding.length > 0 && tower[program].heldBy.length === 0) {
answer = program
}
}
console.log({ answer })
console.log({ tower })