@@ -4,31 +4,34 @@ import { EOL } from "node:os";
4
4
const inputLines = ( await Bun . file ( "input" ) . text ( ) ) . trim ( ) . split ( EOL ) ;
5
5
6
6
type Grid = Array < Array < string > > ;
7
- type Vector = [ number , number ] ; // [x, y]
8
7
9
- const STRAIGHT_DIRECTIONS : Array < Vector > = [ [ - 1 , 0 ] , [ 1 , 0 ] , [ 0 , - 1 ] , [ 0 , 1 ] ]
10
- const DIAGONAL_DIRECTIONS : Array < Vector > = [ [ - 1 , - 1 ] , [ 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , 1 ] ]
8
+ class Vector {
9
+ x : number ;
10
+ y : number ;
11
11
12
- function VALID_DIRECTIONS ( part2 : boolean ) : Array < Vector > {
13
- return part2 ? DIAGONAL_DIRECTIONS : STRAIGHT_DIRECTIONS . concat ( DIAGONAL_DIRECTIONS ) ;
14
- }
12
+ constructor ( x : number , y : number ) {
13
+ this . x = x ;
14
+ this . y = y ;
15
+ }
15
16
16
- function vecAdd ( one : Vector , two : Vector ) : Vector {
17
- return [ one [ 0 ] + two [ 0 ] , one [ 1 ] + two [ 1 ] ] ;
17
+ add ( other : Vector ) : Vector { return new Vector ( this . x + other . x , this . y + other . y ) ; }
18
+ neg ( ) : Vector { return new Vector ( this . x * - 1 , this . y * - 1 ) ; }
18
19
}
20
+ const vec = ( x : number , y : number ) => new Vector ( x , y ) ;
19
21
20
- function vecNeg ( vec : Vector ) : Vector {
21
- return [ vec [ 0 ] * - 1 , vec [ 1 ] * - 1 ] ;
22
- }
22
+ const STRAIGHT_DIRECTIONS : Array < Vector > = [ vec ( - 1 , 0 ) , vec ( 1 , 0 ) , vec ( 0 , - 1 ) , vec ( 0 , 1 ) ] ;
23
+ const DIAGONAL_DIRECTIONS : Array < Vector > = [ vec ( - 1 , - 1 ) , vec ( 1 , - 1 ) , vec ( - 1 , 1 ) , vec ( 1 , 1 ) ] ;
23
24
24
- function getFromGrid ( point : Vector , grid : Grid ) : String {
25
- return grid [ point [ 1 ] ] [ point [ 0 ] ] ;
25
+ function VALID_DIRECTIONS ( part2 : boolean ) : Array < Vector > {
26
+ return part2 ? DIAGONAL_DIRECTIONS : STRAIGHT_DIRECTIONS . concat ( DIAGONAL_DIRECTIONS ) ;
26
27
}
27
28
29
+ const getFromGrid = ( point : Vector , grid : Grid ) : String => grid [ point . y ] [ point . x ] ;
30
+
28
31
function isInBounds ( point : Vector , grid : Grid ) {
29
32
return (
30
- point [ 1 ] >= 0 && point [ 1 ] < grid . length
31
- && point [ 0 ] >= 0 && point [ 0 ] < grid [ 0 ] . length
33
+ point . y >= 0 && point . y < grid . length
34
+ && point . x >= 0 && point . x < grid [ 0 ] . length
32
35
)
33
36
}
34
37
@@ -37,7 +40,7 @@ function locateHints(grid: Grid, hintSymbol: String): Array<Vector> {
37
40
for ( let y : number = 0 ; y < grid . length ; y ++ ) {
38
41
const line = grid [ y ] ;
39
42
for ( let x : number = 0 ; x < line . length ; x ++ ) {
40
- if ( line [ x ] == hintSymbol ) hints . push ( [ x , y ] ) ;
43
+ if ( line [ x ] == hintSymbol ) hints . push ( new Vector ( x , y ) ) ;
41
44
}
42
45
}
43
46
return hints ;
@@ -48,7 +51,7 @@ function investigateHint(hint: Vector, searchString: String, part2: boolean = fa
48
51
for ( let searchDirection of VALID_DIRECTIONS ( part2 ) ) {
49
52
const directionString : Array < String > = [ ] ;
50
53
51
- let searchLocation = part2 ? vecAdd ( hint , vecNeg ( searchDirection ) ) : hint ;
54
+ let searchLocation = part2 ? hint . add ( searchDirection . neg ( ) ) : hint ;
52
55
let searchStringIndex = 0 ;
53
56
while (
54
57
isInBounds ( searchLocation , inputGrid )
@@ -57,7 +60,7 @@ function investigateHint(hint: Vector, searchString: String, part2: boolean = fa
57
60
// TODO: Maybe fail the direction on first wrong letter?
58
61
59
62
directionString . push ( getFromGrid ( searchLocation , inputGrid ) ) ;
60
- searchLocation = vecAdd ( searchLocation , searchDirection ) ;
63
+ searchLocation = searchLocation . add ( searchDirection ) ;
61
64
searchStringIndex += 1 ;
62
65
}
63
66
if ( directionString . join ( "" ) == searchString ) hitCount += 1 ;
0 commit comments