this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Solve D07P02

modamo 2157a31c 1c765155

+69
+69
day07/part2.ts
··· 1 + import { readFileSync } from "fs"; 2 + 3 + const diagram = readFileSync("./input.txt", "utf8") 4 + .split(/\n/) 5 + .map((line) => line.split("")); 6 + 7 + let beams = new Map<string, number>(); 8 + 9 + beams.set(JSON.stringify({ r: 0, c: diagram[0].indexOf("S") }), 1); 10 + 11 + for (let i = 0; i < diagram.length - 1; i++) { 12 + const newBeams = new Map(); 13 + 14 + for (const [beam, tachyonCount] of beams) { 15 + const parsedBeam = JSON.parse(beam); 16 + 17 + if (diagram[parsedBeam.r + 1][parsedBeam.c] === "^") { 18 + if (parsedBeam.c - 1 >= 0) { 19 + newBeams.set( 20 + JSON.stringify({ 21 + r: parsedBeam.r + 1, 22 + c: parsedBeam.c - 1 23 + }), 24 + tachyonCount + 25 + (newBeams.get( 26 + JSON.stringify({ 27 + r: parsedBeam.r + 1, 28 + c: parsedBeam.c - 1 29 + }) 30 + ) || 0) 31 + ); 32 + } 33 + 34 + if (parsedBeam.c + 1 < diagram[0].length) { 35 + newBeams.set( 36 + JSON.stringify({ 37 + r: parsedBeam.r + 1, 38 + c: parsedBeam.c + 1 39 + }), 40 + tachyonCount + 41 + (newBeams.get( 42 + JSON.stringify({ 43 + r: parsedBeam.r + 1, 44 + c: parsedBeam.c + 1 45 + }) 46 + ) || 0) 47 + ); 48 + } 49 + } else { 50 + newBeams.set( 51 + JSON.stringify({ 52 + r: parsedBeam.r + 1, 53 + c: parsedBeam.c 54 + }), 55 + tachyonCount + 56 + (newBeams.get( 57 + JSON.stringify({ 58 + r: parsedBeam.r + 1, 59 + c: parsedBeam.c 60 + }) 61 + ) || 0) 62 + ); 63 + } 64 + } 65 + 66 + beams = newBeams; 67 + } 68 + 69 + console.log([...beams.values()].reduce((a, c) => a + c, 0));