1- import { getPuzzle } from "../../utils" ;
1+ import { timePart1 , timePart2 } from "../../utils/time-part " ;
22
3- const puzzleInput = getPuzzle ( __dirname ) . trim ( ) ;
3+ const parseInput = ( input : string ) =>
4+ input . split ( "\n" ) . map ( ( row ) => row . split ( "" ) ) ;
45
5- const map = puzzleInput . split ( "\n" ) . map ( ( row ) => row . split ( "" ) ) ;
6+ const measureMap = ( map : ReturnType < typeof parseInput > ) => {
7+ const WIDTH = map [ 0 ] . length ;
8+ const HEIGHT = map . length ;
69
7- const WIDTH = map [ 0 ] . length ;
8- const HEIGHT = map . length ;
9-
10- // Part 1
11- ( ( ) => {
12- console . time ( "part 1" ) ;
10+ return { WIDTH , HEIGHT } ;
11+ } ;
1312
13+ export const part1 = timePart1 ( ( input : string ) => {
14+ const map = parseInput ( input ) ;
15+ const { HEIGHT , WIDTH } = measureMap ( map ) ;
1416 const antennas = new Map < string , Set < string > > ( ) ;
1517
1618 for ( let y = 0 ; y < HEIGHT ; y ++ ) {
@@ -22,7 +24,7 @@ const HEIGHT = map.length;
2224 }
2325
2426 if ( antennas . has ( cell ) ) {
25- antennas . get ( cell ) . add ( `${ x } ,${ y } ` ) ;
27+ antennas . get ( cell ) ! . add ( `${ x } ,${ y } ` ) ;
2628 } else {
2729 antennas . set ( cell , new Set < string > ( [ `${ x } ,${ y } ` ] ) ) ;
2830 }
@@ -83,13 +85,12 @@ const HEIGHT = map.length;
8385 }
8486 }
8587
86- console . log ( "part 1 antinode count ::" , antinodes . size ) ;
87- console . timeEnd ( "part 1" ) ;
88- } ) ( ) ;
88+ return antinodes . size ;
89+ } ) ;
8990
90- // Part 2
91- ( ( ) => {
92- console . time ( "part 2" ) ;
91+ export const part2 = timePart2 ( ( input : string ) => {
92+ const map = parseInput ( input ) ;
93+ const { HEIGHT , WIDTH } = measureMap ( map ) ;
9394 const antennas = new Map < string , Set < string > > ( ) ;
9495
9596 for ( let y = 0 ; y < HEIGHT ; y ++ ) {
@@ -101,7 +102,7 @@ const HEIGHT = map.length;
101102 }
102103
103104 if ( antennas . has ( cell ) ) {
104- antennas . get ( cell ) . add ( `${ x } ,${ y } ` ) ;
105+ antennas . get ( cell ) ! . add ( `${ x } ,${ y } ` ) ;
105106 } else {
106107 antennas . set ( cell , new Set < string > ( [ `${ x } ,${ y } ` ] ) ) ;
107108 }
@@ -198,6 +199,5 @@ const HEIGHT = map.length;
198199 }
199200 }
200201
201- console . log ( "part 2 antinode count ::" , antinodes . size ) ;
202- console . timeEnd ( "part 2" ) ;
203- } ) ( ) ;
202+ return antinodes . size ;
203+ } ) ;
0 commit comments