File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ #.##..##.
2
+ ..#.##.#.
3
+ ##......#
4
+ ##......#
5
+ ..#.##.#.
6
+ ..##..##.
7
+ #.#.##.#.
8
+
9
+ #...##..#
10
+ #....#..#
11
+ ..##..###
12
+ #####.##.
13
+ #####.##.
14
+ ..##..###
15
+ #....#..#
Original file line number Diff line number Diff line change
1
+ const handlePattern = ( pattern : string , part2 = false ) => {
2
+ const rows = pattern . split ( '\n' )
3
+
4
+ let cols : string [ ] = Array ( rows [ 0 ] . length ) . fill ( '' )
5
+ for ( const row of rows ) {
6
+ for ( const [ j , char ] of [ ...row ] . entries ( ) ) {
7
+ cols [ j ] += char
8
+ }
9
+ }
10
+
11
+ // Check horizontally
12
+ for ( let i = 1 ; i < rows . length ; i ++ ) {
13
+ if ( rows [ i - 1 ] === rows [ i ] ) {
14
+ let x = 0
15
+ while ( rows [ i - 1 - x ] === rows [ i + x ] ) {
16
+ x ++
17
+ if ( i - 1 - x < 0 || i + x > rows . length - 1 ) {
18
+ return i * 100
19
+ }
20
+ }
21
+ }
22
+ }
23
+
24
+ // Check vertically
25
+ for ( let i = 1 ; i < cols . length ; i ++ ) {
26
+ if ( cols [ i - 1 ] === cols [ i ] ) {
27
+ let x = 0
28
+ while ( cols [ i - 1 - x ] === cols [ i + x ] ) {
29
+ x ++
30
+ if ( i - 1 - x < 0 || i + x > cols . length - 1 ) {
31
+ return i
32
+ }
33
+ }
34
+ }
35
+ }
36
+
37
+ throw new Error ( "shouldn't happened..." )
38
+ }
39
+
40
+ const file = Bun . file ( 'input' ) ;
41
+ const input = await file . text ( ) ;
42
+ const patterns = input . trimEnd ( ) . split ( '\n\n' )
43
+
44
+ const total1 = patterns . reduce ( ( acc , curr ) => acc + handlePattern ( curr ) , 0 )
45
+ console . log ( `Result for part 1 is : ${ total1 } ` )
46
+
47
+ const total2 = 0
48
+ console . log ( `Result for part 2 is : ${ total2 } ` )
You can’t perform that action at this time.
0 commit comments