@@ -7,7 +7,7 @@ export async function part1(inputFile: string) {
77}
88
99export async function part2 ( inputFile : string ) {
10- return await day4 ( inputFile ) ;
10+ return await day4 ( inputFile , findAndRemoveAccessiblePaperRolls ) ;
1111}
1212
1313async function day4 ( inputFile : string , calcFn ?: ( grid : Grid ) => number ) {
@@ -17,17 +17,34 @@ async function day4(inputFile: string, calcFn?: (grid: Grid) => number) {
1717 return calcFn ?.( grid ) ;
1818}
1919
20- function findAccessiblePaperRolls ( grid : Grid ) {
21- let paperRolls = 0 ;
22-
20+ function getRollCoords ( grid : Map < string , string > ) : Coord [ ] {
21+ let paperRolls : Coord [ ] = [ ] ;
2322 grid . forEach ( ( value , coord ) => {
2423 if ( value === '@' ) {
2524 const adjacentCells = getNeighbors ( Coord . deserialize ( coord ) , grid , true ) ;
2625 const rolls = adjacentCells . filter ( cell => cell === '@' ) . length ;
2726 if ( rolls < 4 )
28- paperRolls += 1 ;
27+ paperRolls . push ( Coord . deserialize ( coord ) ) ;
2928 }
3029 } ) ;
31-
3230 return paperRolls ;
31+ }
32+
33+ function findAccessiblePaperRolls ( grid : Grid ) {
34+ return getRollCoords ( grid ) . length ;
35+ }
36+
37+ function findAndRemoveAccessiblePaperRolls ( grid : Grid ) {
38+ let totalAccessiblePaperRolls = 0 ;
39+
40+ let paperRollCoords : Coord [ ] = getRollCoords ( grid ) ;
41+ while ( paperRollCoords . length > 0 ) {
42+ totalAccessiblePaperRolls += paperRollCoords . length ;
43+
44+ paperRollCoords . forEach ( ( p ) => grid . set ( p . serialize ( ) , '.' ) ) ;
45+
46+ paperRollCoords = getRollCoords ( grid ) ;
47+ }
48+
49+ return totalAccessiblePaperRolls ;
3350}
0 commit comments