File tree Expand file tree Collapse file tree 9 files changed +96
-13
lines changed
.github/badges/typescript Expand file tree Collapse file tree 9 files changed +96
-13
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"schemaVersion" : 1 ,
3
3
"label" : " Advent of TypeScript 2024" ,
4
- "message" : " 2 /25" ,
4
+ "message" : " 3 /25" ,
5
5
"color" : " orange"
6
6
}
Original file line number Diff line number Diff line change 45
45
"p1" : " RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts" ,
46
46
"p1:example" : " RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts" ,
47
47
"p2" : " RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts" ,
48
- "p2:example" : " RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1 .txt tsx src/p2.ts"
48
+ "p2:example" : " RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.2 .txt tsx src/p2.ts"
49
49
},
50
50
"exports" : {
51
51
"./bench" : {
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { p1 } from './p1.js';
6
6
import { p2 } from './p2.js' ;
7
7
8
8
await defaultBench (
9
- '2024 - Day 2 ' ,
9
+ '2024 - Day 3 ' ,
10
10
add ( 'Part One' , async ( ) => {
11
11
const { input } = await loadTaskResources ( packageJson . aoc ) ;
12
12
return ( ) => p1 ( input ) ;
Original file line number Diff line number Diff line change @@ -7,14 +7,21 @@ describe('2024 01 p1', () => {
7
7
describe ( 'the input' , ( ) => {
8
8
it ( 'should solve the input' , async ( ) => {
9
9
const resources = await loadTaskResources ( packageJson . aoc ) ;
10
- expect ( p1 ( resources . input ) ) . toEqual ( 299 ) ;
10
+ expect ( p1 ( resources . input ) ) . toEqual ( 179834255 ) ;
11
11
} ) ;
12
12
} ) ;
13
13
14
14
describe ( 'example 1' , ( ) => {
15
15
it ( 'should be solved' , async ( ) => {
16
16
const resources = await loadTaskResources ( packageJson . aoc , 'example.1.txt' ) ;
17
- expect ( p1 ( resources . input ) ) . toEqual ( 2 ) ;
17
+ expect ( p1 ( resources . input ) ) . toEqual ( 161 ) ;
18
+ } ) ;
19
+ } ) ;
20
+
21
+ describe ( 'example 2' , ( ) => {
22
+ it ( 'should be solved' , async ( ) => {
23
+ const resources = await loadTaskResources ( packageJson . aoc , 'example.2.txt' ) ;
24
+ expect ( p1 ( resources . input ) ) . toEqual ( 161 ) ;
18
25
} ) ;
19
26
} ) ;
20
27
} ) ;
Original file line number Diff line number Diff line change 1
1
import { task } from '@alexaegis/advent-of-code-lib' ;
2
2
import packageJson from '../package.json' assert { type : 'json' } ;
3
3
4
- export const p1 = ( input : string ) : number => { } ;
4
+ const mulRegex = / m u l \( ( \d + ) , ( \d + ) \) / g ;
5
5
6
- await task ( p1 , packageJson . aoc ) ; // 0 ~0ms
6
+ export const p1 = ( input : string ) : number => {
7
+ const mulMatches = [ ...input . matchAll ( mulRegex ) ] ;
8
+
9
+ return mulMatches . map ( ( m ) => parseInt ( m [ 1 ] ?? '1' , 10 ) * parseInt ( m [ 2 ] ?? '1' , 10 ) ) . sum ( ) ;
10
+ } ;
11
+
12
+ await task ( p1 , packageJson . aoc ) ; // 179834255 ~0.09ms
Original file line number Diff line number Diff line change @@ -7,14 +7,21 @@ describe('2024 01 p2', () => {
7
7
describe ( 'the input' , ( ) => {
8
8
it ( 'should solve the input' , async ( ) => {
9
9
const { input } = await loadTaskResources ( packageJson . aoc ) ;
10
- expect ( p2 ( input ) ) . toEqual ( 364 ) ;
10
+ expect ( p2 ( input ) ) . toEqual ( 80570939 ) ;
11
11
} ) ;
12
12
} ) ;
13
13
14
14
describe ( 'example 1' , ( ) => {
15
15
it ( 'should be solved' , async ( ) => {
16
16
const { input } = await loadTaskResources ( packageJson . aoc , 'example.1.txt' ) ;
17
- expect ( p2 ( input ) ) . toEqual ( 4 ) ;
17
+ expect ( p2 ( input ) ) . toEqual ( 161 ) ;
18
+ } ) ;
19
+ } ) ;
20
+
21
+ describe ( 'example 2' , ( ) => {
22
+ it ( 'should be solved' , async ( ) => {
23
+ const { input } = await loadTaskResources ( packageJson . aoc , 'example.2.txt' ) ;
24
+ expect ( p2 ( input ) ) . toEqual ( 48 ) ;
18
25
} ) ;
19
26
} ) ;
20
27
} ) ;
Original file line number Diff line number Diff line change 1
1
import { task } from '@alexaegis/advent-of-code-lib' ;
2
2
import packageJson from '../package.json' assert { type : 'json' } ;
3
3
4
+ const instructionRegex = / ( m u l | d o n ' t | d o ) \( ( \d + ) ? , ? ( \d + ) ? \) / g;
5
+
6
+ interface DoInstruction {
7
+ operator : Operator . DO ;
8
+ }
9
+
10
+ interface DontInstruction {
11
+ operator : Operator . DONT ;
12
+ }
13
+
14
+ interface MulInstruction {
15
+ operator : Operator . MUL ;
16
+ operandA : number ;
17
+ operandB : number ;
18
+ }
19
+
20
+ type Instruction = DoInstruction | DontInstruction | MulInstruction ;
21
+
22
+ enum Operator {
23
+ MUL = 'mul' ,
24
+ DO = 'do' ,
25
+ DONT = "don't" ,
26
+ }
27
+
28
+ const parseInstruction = ( match : RegExpExecArray ) : Instruction => {
29
+ if ( match [ 1 ] === Operator . MUL ) {
30
+ return {
31
+ operator : match [ 1 ] ,
32
+ operandA : parseInt ( match [ 2 ] ?? '1' , 10 ) ,
33
+ operandB : parseInt ( match [ 3 ] ?? '1' , 10 ) ,
34
+ } as MulInstruction ;
35
+ } else if ( match [ 1 ] === Operator . DO ) {
36
+ return {
37
+ operator : match [ 1 ] ,
38
+ } as DoInstruction ;
39
+ } else if ( match [ 1 ] === Operator . DONT ) {
40
+ return {
41
+ operator : match [ 1 ] ,
42
+ } as DontInstruction ;
43
+ } else {
44
+ throw new Error ( 'unsupported instruction: ' + match [ 0 ] ) ;
45
+ }
46
+ } ;
47
+
4
48
export const p2 = ( input : string ) : number => {
5
- return 0 ;
49
+ const instructions = [ ...input . matchAll ( instructionRegex ) ] . map ( parseInstruction ) ;
50
+
51
+ let enabled = true ;
52
+ let sum = 0 ;
53
+ for ( const i of instructions ) {
54
+ switch ( i . operator ) {
55
+ case Operator . DO :
56
+ enabled = true ;
57
+ break ;
58
+ case Operator . DONT :
59
+ enabled = false ;
60
+ break ;
61
+ case Operator . MUL :
62
+ if ( enabled ) {
63
+ sum += i . operandA * i . operandB ;
64
+ }
65
+ break ;
66
+ }
67
+ }
68
+ return sum ;
6
69
} ;
7
70
8
- await task ( p2 , packageJson . aoc ) ; // 364 ~0.90ms
71
+ await task ( p2 , packageJson . aoc ) ; // 80570939 ~0.11ms
Original file line number Diff line number Diff line change 10
10
| --------------------------------------- | -------------------------------------------------- | -------------------------------------------------- |
11
11
| [ Day 1] ( /solutions/typescript/2024/01/ ) | [ 16.04ms] ( /solutions/typescript/2024/01/src/p1.ts ) | [ 18.95ms] ( /solutions/typescript/2024/01/src/p2.ts ) |
12
12
| [ Day 2] ( /solutions/typescript/2024/02/ ) | [ 0.58ms] ( /solutions/typescript/2024/02/src/p1.ts ) | [ 0.90ms] ( /solutions/typescript/2024/02/src/p2.ts ) |
13
- | Day 3 | - | - |
13
+ | [ Day 3] ( /solutions/typescript/2024/03/ ) | [ 0.09ms ] ( /solutions/typescript/2024/03/src/p1.ts ) | [ 0.11ms ] ( /solutions/typescript/2024/03/src/p2.ts ) |
14
14
| Day 4 | - | - |
15
15
| Day 5 | - | - |
16
16
| Day 6 | - | - |
You can’t perform that action at this time.
0 commit comments