diff --git a/2025/7.1.ts b/2025/7.1.ts new file mode 100644 index 0000000..e724a5b --- /dev/null +++ b/2025/7.1.ts @@ -0,0 +1,65 @@ +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)) \ No newline at end of file diff --git a/2025/7.2.ts b/2025/7.2.ts new file mode 100644 index 0000000..1e2271d --- /dev/null +++ b/2025/7.2.ts @@ -0,0 +1,94 @@ +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[] = [] + +let origin: Node | null = null +class Node { + isBeam = false + isSplitter = false + left: Node | null = null + right: Node | null = null + weight = 0 + constructor(public x: number, public y: number, public character: string) { + if (this.character == "^") { + this.isSplitter = true + nodes.push(this) + } + if (this.character == "S") { + this.isBeam = true + origin = this + } + } + + buildGraph() { + let searchY = this.y + 1 + while (searchY < table.length && (!this.left || !this.right)) { + if (this.x > 0 && !this.left && table[searchY][this.x - 1].isSplitter) { + this.left = table[searchY][this.x - 1] + } + if (this.x + 1 < table[searchY].length && !this.right && table[searchY][this.x + 1].isSplitter) { + this.right = table[searchY][this.x + 1] + } + searchY++ + } + } + + calculateWeight() { + if (this.weight) { + return this.weight + } + let leftC = 1 + if (this.left) { + leftC = this.left.calculateWeight() + } + let rightC = 1 + if (this.right) { + rightC = this.right.calculateWeight() + } + this.weight = leftC + rightC + return this.weight + } + + 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 + } + if (colN != 0) { + if (!table[lineN][colN - 1].isBeam) { + table[lineN][colN - 1].isBeam = true + } + } + if (colN + 1 < line.length) { + if (!table[lineN][colN + 1].isBeam) { + table[lineN][colN + 1].isBeam = true + } + } + }) +}) +nodes.forEach(n => n.buildGraph()); + +origin = origin as unknown as Node +origin.buildGraph() + +console.log(origin.calculateWeight()) \ No newline at end of file diff --git a/2025/inputs/7 b/2025/inputs/7 new file mode 100644 index 0000000..d35f953 --- /dev/null +++ b/2025/inputs/7 @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^...^.^................................................................... +............................................................................................................................................. +..................................................................^.^.^...^.................................................................. +............................................................................................................................................. +.................................................................^...^.^.^.^................................................................. +............................................................................................................................................. +................................................................^.^.^.^.^.^.^................................................................ +............................................................................................................................................. +...............................................................^.^.^...^.^...^............................................................... +............................................................................................................................................. +..............................................................^.^.^...^.^.^...^.............................................................. +............................................................................................................................................. +.............................................................^.^.^.....^.^.^.^.^............................................................. +............................................................................................................................................. +............................................................^.^.^.^...^.^.....^.^............................................................ +............................................................................................................................................. +...........................................................^.^.......^...^.^.^.^.^........................................................... +............................................................................................................................................. +..........................................................^.^...^.^.^...^.^.^.^.^.^.......................................................... +............................................................................................................................................. +.........................................................^.^.^...^.^.^.^.^.^.^.^.^.^......................................................... +............................................................................................................................................. +........................................................^.^.^.^.........^.^...^...^.^........................................................ +............................................................................................................................................. +.......................................................^.^.^...^.^.....^.^.^.......^.^....................................................... +............................................................................................................................................. +......................................................^.^.....^.^.^.....^.^.^.....^.^.^...................................................... +............................................................................................................................................. +.....................................................^...^.^.^.^.^.^.....^.^.^.^.^.^.^.^..................................................... +............................................................................................................................................. +....................................................^.^...^...^.^.^.^.^.^.^.....^.^.....^.................................................... +............................................................................................................................................. +...................................................^.^...^.^.^.^...^...^.....^.^...^.....^................................................... +............................................................................................................................................. +..................................................^.......^.^...^.^.^.^.^.^.....^.^...^...^.................................................. +............................................................................................................................................. +.................................................^...^.....^...^...^...^.^.^.^...^.^.^.^...^................................................. +............................................................................................................................................. +................................................^.^.^.^.^...^.....^...^.^.^.^.^.^.^...^.^.^.^................................................ +............................................................................................................................................. +...............................................^.^.^.^.......^...^...^.^.^...^.^.^.^.^...^.^.^............................................... +............................................................................................................................................. +..............................................^.^.^...^.^.^.^.^...^.^.....^.^.^.^.^.....^.^.^.^.............................................. +............................................................................................................................................. +.............................................^.^.^.^.^.^.^...^.^.....^.^.^.^.^...^.....^...^.^.^............................................. +............................................................................................................................................. +............................................^.^.^.^.^.^.^...^.^.^...^.............^.....^.....^.^............................................ +............................................................................................................................................. +...........................................^.^.^.....^...^.^.^.^...^.^...^.....^...^.....^.^.^.^.^........................................... +............................................................................................................................................. +..........................................^.^.^.^.^.^.^.^.^...^.^...^.^.......^.^.....^.^.^...^.^.^.......................................... +............................................................................................................................................. +.........................................^...^.^.^.......^.....^...^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^......................................... +............................................................................................................................................. +........................................^.^.^.^.^.^.....^...^.....^.^.....^.^.^.^.^...^...^.^.^.^...^........................................ +............................................................................................................................................. +.......................................^.^.^.^.^.^.^.^.^.^.^.^.^.^.......^.......^.^.....^...^.^...^.^....................................... +............................................................................................................................................. +......................................^.^.^.^.^.^.^...^.^...^.^.^...^...^.....^.^.......^.^.....^...^.^...................................... +............................................................................................................................................. +.....................................^.^.^.^.^.^.^.^...^...^.^.^.^...^.^.^.^...^.^.^.^.^.^.^.^...^.^...^..................................... +............................................................................................................................................. +....................................^...^.^...^...^.^.....^.^...^.^.^.^.^...^.^.^.^.^...^.^.^.^...^.^.^.^.................................... +............................................................................................................................................. +...................................^.^...^...^.^.....^.....^.....^...^.^.^.^.^...^.^.^.^...^...^.^.^.^...^................................... +............................................................................................................................................. +..................................^...^.^.^.^.....^...^.....^.^.^.........^.^.^.....^.^.^.^.^...^.^.^.^.^.^.................................. +............................................................................................................................................. +.................................^.....^.^.^.......^.^.....^.^.^.^.......^.^.^...^.....^.......^.^...^.....^................................. +............................................................................................................................................. +................................^...^.^.^.^.^.^.^.^.....^...^.....^.^.^.....^.^...^.^.^.....^.^.......^.^.^.^................................ +............................................................................................................................................. +...............................^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^...^.........^.^.^.^.....^.^.^.^...^...^.^............................... +............................................................................................................................................. +..............................^...^.............^...^.^...^...^.^.....^.^.....^...^.^...^.^.^...^...^.^.^.^...^.............................. +............................................................................................................................................. +.............................^...^...^...^...^.^.^.^.........^...^.....^.^.^.^.^.^.^.^.....^.......^.^...^...^.^............................. +............................................................................................................................................. +............................^.^.^.^.^.^.....^.^...^.^.............^.^.^.^.^.^...^.^.^...^.^...^...^.^.^...^...^.^............................ +............................................................................................................................................. +...........................^...^...^...^.^.^.^.......^.^.^...^...^...^.^.^...^.^.^...^.^.......^.^.^.^.^.^.....^.^........................... +............................................................................................................................................. +..........................^.^.^.^.^.^.^.^...^.^...^.^...^.^.......^.^...^.^.^.^...^.^...^...^.^...^.^.^.^.^.^.^...^.......................... +............................................................................................................................................. +.........................^.....^.^.....^.^...^...^.^.^...^.......^.^.^...^.^.....^.....^...^.^...^.....^...^.^.^.^.^......................... +............................................................................................................................................. +........................^.^.^.^.^.^.^.^.^.....^...^...^...^.^.^.^...^.......^.......^...^...^.^.^...^.^.....^...^.^.^........................ +............................................................................................................................................. +.......................^.^...^.^.....^.^...^.....^.^.^.^.^.^.^...........^.......^.^.^.......^.^.^.^.^.^.....^.......^....................... +............................................................................................................................................. +......................^.....^...^.^...^.^.^...^.^.^...^.^...^.^.^...^.....^...^.....^.^.^.^.^.^...^.....^.^.^.^...^.^.^...................... +............................................................................................................................................. +.....................^.^.^.^...^.^...^...^.^...^.^.^.^.^.^...^.^.^.^...^.^.......^.^...^.........^.^.^.^...^.....^.^.^.^..................... +............................................................................................................................................. +....................^.^.^.^.^.^...^...........^.^.^.^.^.......^.....^.^...^...^.^.^...^.^.^.^...^.^.^.^.^...^.....^.^...^.................... +............................................................................................................................................. +...................^.^.....^.^.^.^.^.^.^.^.^.^.^.......^.....^.........^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^.^...^.^.^.^................... +............................................................................................................................................. +..................^.......^.^...^.^...^.^.^.....^.^.^.^.....^.......^...^.^.....^.^.....^.^...^.^.^.^.^.....^.^.^.......^.^.................. +............................................................................................................................................. +.................^...^.^.^.^.^.....^.^.^...^.^...^...^...^.^.^.^.^.^...^.....^.^.^...^.^.....^.^.^.^.^.^.^.^.^.....^.......^................. +............................................................................................................................................. +................^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^...^.^.^...^...^.^.....^...^.^.^.^...^.^.....^.^................ +............................................................................................................................................. +...............^...^.^.^.^.^.^.^...^.^.....^...^.^.....^.^.....^.^.....^.^.^.^...^.^.^...^.^.....^.^.....^.....^.....^.^.^.^.^............... +............................................................................................................................................. +..............^.^.^...^.........^.^.^.^...^.......^.^.^.^.....^.^.^.^.....^.^.^.....^.^.^.........^.....^.^...^.^...^.^.^.^.^.^.............. +............................................................................................................................................. +.............^.^.^.^...^...^.^...^...^.......^.^.^.....^.^.^.^...^...^.^.^.........^.....^.^.....^.......^.^.......^.^.^.^.^.^.^............. +............................................................................................................................................. +............^...^.^.^...............^.^.^.^...^.^.^.^.......^...^.^.^...^.^.^.^...^.^.^.^...^.^.....^.^.^...^.^.....^.^.^.^.^.^.^............ +............................................................................................................................................. +...........^.^.^...^.^...^.^.^.^...^.^...^.^.^.....^...^.....^.^.^.^.^...^.^.^.^.....^.^...^.^...^.....^.^.^...^.^.......^...^.^.^........... +............................................................................................................................................. +..........^.^.^.^...^.^.^...^...^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^...^.^.......^...^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.......... +............................................................................................................................................. +.........^.....^.^...^.^.....^.^.^...^.^.^...^.^.....^.^.^...^.^.^.^...^.^.^.^.^.^...^...^.^.^.^.^.^.^.....^.^...^.^.^.^...^.^.^...^......... +............................................................................................................................................. +........^...^.^.^.^.^.^.^.^.....^.^.^...^.^.........^.^.^.^.......^...^...^.^.^.^.^...^...^...^...^.......^...^.^.^.^.^...^.^.^.^.^.^........ +............................................................................................................................................. +.......^.....^.^.^...^.^...^...^.....^...^.^.......^.....^.^.^.^.^.^.^.^.......^.^.^.^...^...^.^.^.^.^...^.^.^.^.^.^...^.^.^.^.^.^.^.^....... +............................................................................................................................................. +......^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^.^.....^.^...^...^.^...^.^.^...^.....^.......^.^.^.....^...^.^.^...^...^.^...^.^...^.^...^...... +............................................................................................................................................. +.....^...^.......^.^.^.^.^.^.^...^...^...^.^.^...^.^.^.^...^...^.^.^.....^.^.....^...^.....^.....^.^.^...^.^.^...^.^.^...^...^...^.^...^..... +............................................................................................................................................. +....^.^.^...^.^...^.^...^.^...^.^...^.^.^...^.....^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^...^.^...^.^.^...^.^.^.^.^...^...^.^.^.....^.... +............................................................................................................................................. +...^.^...^.^.^.^...^.^.^.^.^.^.^...........^.^.^.^...^...^.^.^...^.^.....^.^.^.^.....^...^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.......^... +............................................................................................................................................. +..^.^.^.^...^...^.^.^.....^.^.^.^.^...^.^.^.^...^...^.^.^.^...^...^.......^.^.^.^.^.^.^.^.^.^.^...^.^.^...^...^.^...^...^.^.^.^...^.^.^...^.. +............................................................................................................................................. +.^.^.^.......^.....^.^.............^...^.....^.^.^.^.^.........^.^.^.........^.^.^.^.^.^.^...^...^.^...^.......^.^...^.^.^.^...^.^.^...^.^.^. +............................................................................................................................................. \ No newline at end of file diff --git a/2025/inputs/7.example b/2025/inputs/7.example new file mode 100644 index 0000000..8868910 --- /dev/null +++ b/2025/inputs/7.example @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... \ No newline at end of file