File tree Expand file tree Collapse file tree 9 files changed +61
-26
lines changed
.github/badges/typescript Expand file tree Collapse file tree 9 files changed +61
-26
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" : " 1 /25" ,
4
+ "message" : " 2 /25" ,
5
5
"color" : " orange"
6
6
}
Original file line number Diff line number Diff line change 43
43
"format" : " turbo run format_ --concurrency 16 --filter @alexaegis/advent-of-code-2024-02" ,
44
44
"format_" : " prettier --cache-location .cache/prettier --plugin prettier-plugin-svelte --plugin prettier-plugin-tailwindcss --write ." ,
45
45
"p1" : " RUN=1 NODE_NO_WARNINGS=1 tsx src/p1.ts" ,
46
- "p2" : " RUN=1 NODE_NO_WARNINGS=1 tsx src/p2.ts"
46
+ "p1:example" : " RUN=1 NODE_NO_WARNINGS=1 RESOURCE=example.1.txt tsx src/p1.ts" ,
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"
47
49
},
48
50
"exports" : {
49
51
"./bench" : {
Original file line number Diff line number Diff line change @@ -7,14 +7,14 @@ describe('2023 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 ( 54_644 ) ;
10
+ expect ( p1 ( resources . input ) ) . toEqual ( 299 ) ;
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 ( 142 ) ;
17
+ expect ( p1 ( resources . input ) ) . toEqual ( 2 ) ;
18
18
} ) ;
19
19
} ) ;
20
20
} ) ;
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
+ import { isReportSafe , parseReports } from './parse.js' ;
3
4
4
- export const p1 = ( input : string ) : number => {
5
- return 0 ;
6
- } ;
7
- await task ( p1 , packageJson . aoc ) ; // 0 ~0ms
5
+ export const p1 = ( input : string ) : number => parseReports ( input ) . filter ( isReportSafe ) . length ;
6
+
7
+ await task ( p1 , packageJson . aoc ) ; // 299 ~0.58ms
Original file line number Diff line number Diff line change @@ -7,21 +7,14 @@ describe('2023 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 ( 53_348 ) ;
10
+ expect ( p2 ( input ) ) . toEqual ( 364 ) ;
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 ( 142 ) ;
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 ( 281 ) ;
17
+ expect ( p2 ( input ) ) . toEqual ( 4 ) ;
25
18
} ) ;
26
19
} ) ;
27
20
} ) ;
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
+ import { isReportSafe , parseReports } from './parse.js' ;
3
4
4
- export const p2 = ( input : string ) : number => {
5
- return 0 ;
6
- } ;
7
- await task ( p2 , packageJson . aoc ) ; // 0 ~0ms
5
+ export const p2 = ( input : string ) : number =>
6
+ parseReports ( input ) . filter ( ( report ) => {
7
+ let safe = isReportSafe ( report ) ;
8
+ for ( let i = 0 ; i < report . length && ! safe ; i ++ ) {
9
+ const copyOfReport = [ ...report ] ;
10
+ copyOfReport . splice ( i , 1 ) ;
11
+ safe = isReportSafe ( copyOfReport ) ;
12
+ }
13
+ return safe ;
14
+ } ) . length ;
15
+
16
+ await task ( p2 , packageJson . aoc ) ; // 364 ~0.90ms
Original file line number Diff line number Diff line change 1
- export interface Parsed { }
1
+ export type Report = number [ ] ;
2
2
3
- export const parse = ( input : string ) : Parsed => {
4
- return undefined ;
3
+ export const parseReports = ( input : string ) : Report [ ] =>
4
+ input . lines ( ) . map ( ( line ) => line . splitToInt ( ) ) ;
5
+
6
+ export const isReportSafe = ( report : Report ) : boolean => {
7
+ let monotoneAscending = true ;
8
+ let monotoneDescending = true ;
9
+ let safeDiff = true ;
10
+ let last = undefined ;
11
+ for ( const v of report ) {
12
+ if ( last !== undefined && v <= last ) {
13
+ monotoneAscending = false ;
14
+ }
15
+
16
+ if ( last !== undefined && v >= last ) {
17
+ monotoneDescending = false ;
18
+ }
19
+
20
+ if ( last !== undefined ) {
21
+ const diff = Math . abs ( last - v ) ;
22
+ if ( diff < 1 || diff > 3 ) {
23
+ safeDiff = false ;
24
+ break ;
25
+ }
26
+ }
27
+
28
+ if ( ! monotoneAscending && ! monotoneDescending ) {
29
+ break ;
30
+ }
31
+
32
+ last = v ;
33
+ }
34
+
35
+ return ( monotoneAscending || monotoneDescending ) && safeDiff ;
5
36
} ;
Original file line number Diff line number Diff line change 9
9
| Day | Part One | Part Two |
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
- | Day 2 | - | - |
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
13
| Day 3 | - | - |
14
14
| Day 4 | - | - |
15
15
| Day 5 | - | - |
You can’t perform that action at this time.
0 commit comments