65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
import fs from 'node:fs';
|
|
|
|
|
|
const file = fs.readFileSync('./2025/inputs/7').toString();
|
|
const lines = file.split("\n").map(e => e.split(""))
|
|
|
|
|
|
const table: Node[][] = []
|
|
const nodes: Node[] = []
|
|
|
|
class Node {
|
|
isBeam = false
|
|
isSplitter = false
|
|
hasSplit = false
|
|
constructor(public x: number, public y: number, public character: string) {
|
|
if (this.character == "^") {
|
|
this.isSplitter = true
|
|
}
|
|
if (this.character == "S") {
|
|
this.isBeam = true
|
|
}
|
|
nodes.push(this)
|
|
}
|
|
toString() {
|
|
if (this.isSplitter) { return "^" }
|
|
if (this.isBeam) { return "|" }
|
|
return "."
|
|
}
|
|
}
|
|
|
|
table.push(...lines.map((l, i) => l.map((e, j) => new Node(j, i, e))))
|
|
|
|
|
|
table.forEach((line, lineN) => {
|
|
if (lineN == 0) {
|
|
return
|
|
}
|
|
line.forEach((node, colN) => {
|
|
if (!table[lineN - 1][colN].isBeam) { return }
|
|
if (!node.isSplitter) {
|
|
node.isBeam = true
|
|
return
|
|
}
|
|
let s = 0
|
|
if (colN != 0) {
|
|
if (!table[lineN][colN - 1].isBeam) {
|
|
node.hasSplit = true
|
|
table[lineN][colN - 1].isBeam = true
|
|
}
|
|
}
|
|
if (colN + 1 < line.length) {
|
|
if (!table[lineN][colN + 1].isBeam) {
|
|
node.hasSplit = true
|
|
table[lineN][colN + 1].isBeam = true
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
console.log(nodes.reduce((acc, n) => {
|
|
if (n.hasSplit) {
|
|
acc++
|
|
}
|
|
return acc
|
|
}, 0)) |