this repo has no description
0
fork

Configure Feed

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

Solve D04P02

modamo-gh cb266a98 69c1dc30

+67
+67
day04/part2.ts
··· 1 + import { readFileSync } from "fs"; 2 + 3 + const grid = readFileSync("./input.txt", "utf8") 4 + .split(/\n/) 5 + .map((line) => line.trim().split("")); 6 + 7 + let totalRemovedRolls = 0; 8 + let numberOfAccessibleRolls; 9 + 10 + do { 11 + numberOfAccessibleRolls = 0; 12 + 13 + const removableRollsIndices: { r: number; c: number }[] = []; 14 + 15 + const countAdjacentRolls = (r: number, c: number) => { 16 + const directions = [ 17 + { dr: -1, dc: 0 }, 18 + { dr: -1, dc: 1 }, 19 + { dr: 0, dc: 1 }, 20 + { dr: 1, dc: 1 }, 21 + { dr: 1, dc: 0 }, 22 + { dr: 1, dc: -1 }, 23 + { dr: 0, dc: -1 }, 24 + { dr: -1, dc: -1 } 25 + ]; 26 + 27 + let numberOfAdjacentRolls = 0; 28 + 29 + for (const direction of directions) { 30 + const newR = r + direction.dr; 31 + const newC = c + direction.dc; 32 + 33 + if ( 34 + newR >= 0 && 35 + newR < grid.length && 36 + newC >= 0 && 37 + newC < grid[newR].length 38 + ) { 39 + numberOfAdjacentRolls += grid[newR][newC] === "@" ? 1 : 0; 40 + } 41 + } 42 + 43 + return numberOfAdjacentRolls; 44 + }; 45 + 46 + for (let r = 0; r < grid.length; r++) { 47 + for (let c = 0; c < grid[r].length; c++) { 48 + if (grid[r][c] === "@") { 49 + const numberOfAdjacentRolls = countAdjacentRolls(r, c); 50 + 51 + if (numberOfAdjacentRolls < 4) { 52 + numberOfAccessibleRolls++; 53 + 54 + removableRollsIndices.push({ r, c }); 55 + } 56 + } 57 + } 58 + } 59 + 60 + totalRemovedRolls += numberOfAccessibleRolls; 61 + 62 + for (const { r, c } of removableRollsIndices) { 63 + grid[r][c] = "."; 64 + } 65 + } while (numberOfAccessibleRolls); 66 + 67 + console.log(totalRemovedRolls);