File tree 4 files changed +62
-8
lines changed
4 files changed +62
-8
lines changed Original file line number Diff line number Diff line change 1
1
export const part1 = ( { input } ) => {
2
- const result = 0 ;
2
+ let result = 0 ;
3
+ const regex = / \d / g;
4
+
5
+ input . forEach ( ( row ) => {
6
+ const matches = row . match ( regex ) ;
7
+ result += parseInt ( matches [ 0 ] + matches [ matches . length - 1 ] ) ;
8
+ } ) ;
9
+
3
10
return result ;
4
11
} ;
5
12
6
13
export const part2 = ( { input } ) => {
7
- const result = 0 ;
14
+ let result = 0 ;
15
+ const regex = / (?: \d | o n e | t w o | t h r e e | f o u r | f i v e | s i x | s e v e n | e i g h t | n i n e ) / g;
16
+
17
+ const numberMap = {
18
+ one : "1" ,
19
+ two : "2" ,
20
+ three : "3" ,
21
+ four : "4" ,
22
+ five : "5" ,
23
+ six : "6" ,
24
+ seven : "7" ,
25
+ eight : "8" ,
26
+ nine : "9" ,
27
+ } ;
28
+
29
+ input . forEach ( ( row ) => {
30
+ const matches = row . match ( regex ) ;
31
+ const firstNumber = matches [ 0 ] ;
32
+
33
+ const first = numberMap [ firstNumber ] || firstNumber ;
34
+
35
+ let lastNumber = false ;
36
+ let count = - 1 ;
37
+
38
+ while ( lastNumber === false ) {
39
+ const testString = row . slice ( count ) ;
40
+ if ( testString . match ( regex ) ) {
41
+ lastNumber = testString . match ( regex ) [ 0 ] ;
42
+ }
43
+ count -- ;
44
+ }
45
+
46
+ const last = numberMap [ lastNumber ] || lastNumber ;
47
+ const sum = parseInt ( first + last ) ;
48
+ result += sum ;
49
+ } ) ;
50
+
8
51
return result ;
9
52
} ;
Original file line number Diff line number Diff line change
1
+ two1nine
2
+ eightwothree
3
+ abcone2threexyz
4
+ xtwone3four
5
+ 4nineeightseven2
6
+ zoneight234
7
+ 7pqrstsixteen
Original file line number Diff line number Diff line change @@ -20,17 +20,17 @@ export async function argsChecker(args) {
20
20
process . exit ( 1 ) ;
21
21
}
22
22
23
- let dataType = "sample " ;
24
- if ( args [ 1 ] === "input " ) {
25
- dataType = "input " ;
23
+ let dataType = "input " ;
24
+ if ( args [ 1 ] === "sample " ) {
25
+ dataType = "sample " ;
26
26
}
27
27
28
28
return { dataType } ;
29
29
}
30
30
31
31
export async function partRunner ( { part, input } ) {
32
32
console . time ( part . name ) ;
33
- const result = part ( input ) ;
33
+ const result = await part ( { input } ) ;
34
34
console . timeEnd ( part . name ) ;
35
35
console . log ( result ) ;
36
36
return result ;
Original file line number Diff line number Diff line change @@ -13,6 +13,10 @@ if (!(await fileExists(`./${dayFile}/index.js`))) {
13
13
const { part1, part2 } = await import ( `./${ dayFile } /index.js` ) ;
14
14
const data = await loadData ( { day : dayFile , dataType } ) ;
15
15
16
- partRunner ( { part : part1 , input : data } ) ;
16
+ await partRunner ( { part : part1 , input : data } ) ;
17
17
18
- partRunner ( { part : part2 , input : data } ) ;
18
+ if ( dataType === "sample" && ( await fileExists ( `./${ dayFile } /sample2.txt` ) ) ) {
19
+ await partRunner ( { part : part2 , input : await loadData ( { day : dayFile , dataType : "sample2" } ) } ) ;
20
+ } else {
21
+ await partRunner ( { part : part2 , input : data } ) ;
22
+ }
You can’t perform that action at this time.
0 commit comments