this repo has no description
0
fork

Configure Feed

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

Solve D04P01

modamo-gh 69c1dc30 148d356c

+50
+50
day04/part1.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 numberOfAccessibleRolls = 0; 8 + 9 + const countAdjacentRolls = (r: number, c: number) => { 10 + const directions = [ 11 + { dr: -1, dc: 0 }, 12 + { dr: -1, dc: 1 }, 13 + { dr: 0, dc: 1 }, 14 + { dr: 1, dc: 1 }, 15 + { dr: 1, dc: 0 }, 16 + { dr: 1, dc: -1 }, 17 + { dr: 0, dc: -1 }, 18 + { dr: -1, dc: -1 } 19 + ]; 20 + 21 + let numberOfAdjacentRolls = 0; 22 + 23 + for (const direction of directions) { 24 + const newR = r + direction.dr; 25 + const newC = c + direction.dc; 26 + 27 + if ( 28 + newR >= 0 && 29 + newR < grid.length && 30 + newC >= 0 && 31 + newC < grid[newR].length 32 + ) { 33 + numberOfAdjacentRolls += grid[newR][newC] === "@" ? 1 : 0; 34 + } 35 + } 36 + 37 + return numberOfAdjacentRolls; 38 + }; 39 + 40 + for (let r = 0; r < grid.length; r++) { 41 + for (let c = 0; c < grid[r].length; c++) { 42 + if (grid[r][c] === "@") { 43 + const numberOfAdjacentRolls = countAdjacentRolls(r, c); 44 + 45 + numberOfAccessibleRolls += numberOfAdjacentRolls < 4 ? 1 : 0; 46 + } 47 + } 48 + } 49 + 50 + console.log(numberOfAccessibleRolls);