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 11{
22 "schemaVersion" : 1 ,
33 "label" : " Advent of TypeScript 2024" ,
4- "message" : " 2 /25" ,
4+ "message" : " 3 /25" ,
55 "color" : " orange"
66}
Original file line number Diff line number Diff line change 4545 "p1" : " RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts" ,
4646 "p1:example" : " RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts" ,
4747 "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"
4949 },
5050 "exports" : {
5151 "./bench" : {
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { p1 } from './p1.js';
66import { p2 } from './p2.js' ;
77
88await defaultBench (
9- '2024 - Day 2 ' ,
9+ '2024 - Day 3 ' ,
1010 add ( 'Part One' , async ( ) => {
1111 const { input } = await loadTaskResources ( packageJson . aoc ) ;
1212 return ( ) => p1 ( input ) ;
Original file line number Diff line number Diff line change @@ -7,14 +7,21 @@ describe('2024 01 p1', () => {
77 describe ( 'the input' , ( ) => {
88 it ( 'should solve the input' , async ( ) => {
99 const resources = await loadTaskResources ( packageJson . aoc ) ;
10- expect ( p1 ( resources . input ) ) . toEqual ( 299 ) ;
10+ expect ( p1 ( resources . input ) ) . toEqual ( 179834255 ) ;
1111 } ) ;
1212 } ) ;
1313
1414 describe ( 'example 1' , ( ) => {
1515 it ( 'should be solved' , async ( ) => {
1616 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 ) ;
1825 } ) ;
1926 } ) ;
2027} ) ;
Original file line number Diff line number Diff line change 11import { task } from '@alexaegis/advent-of-code-lib' ;
22import packageJson from '../package.json' assert { type : 'json' } ;
33
4- export const p1 = ( input : string ) : number => { } ;
4+ const mulRegex = / m u l \( ( \d + ) , ( \d + ) \) / g ;
55
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', () => {
77 describe ( 'the input' , ( ) => {
88 it ( 'should solve the input' , async ( ) => {
99 const { input } = await loadTaskResources ( packageJson . aoc ) ;
10- expect ( p2 ( input ) ) . toEqual ( 364 ) ;
10+ expect ( p2 ( input ) ) . toEqual ( 80570939 ) ;
1111 } ) ;
1212 } ) ;
1313
1414 describe ( 'example 1' , ( ) => {
1515 it ( 'should be solved' , async ( ) => {
1616 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 ) ;
1825 } ) ;
1926 } ) ;
2027} ) ;
Original file line number Diff line number Diff line change 11import { task } from '@alexaegis/advent-of-code-lib' ;
22import packageJson from '../package.json' assert { type : 'json' } ;
33
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+
448export 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 ;
669} ;
770
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 1010| --------------------------------------- | -------------------------------------------------- | -------------------------------------------------- |
1111| [ Day 1] ( /solutions/typescript/2024/01/ ) | [ 16.04ms] ( /solutions/typescript/2024/01/src/p1.ts ) | [ 18.95ms] ( /solutions/typescript/2024/01/src/p2.ts ) |
1212| [ 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 ) |
1414| Day 4 | - | - |
1515| Day 5 | - | - |
1616| Day 6 | - | - |
You can’t perform that action at this time.
0 commit comments