File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { part1 , part2 } from "./day05" ;
2+
3+ describe ( '2025 Day 5' , ( ) => {
4+ test ( 'Part 1' , async ( ) => {
5+ expect ( await part1 ( 'testInput1' ) ) . toEqual ( 3 ) ;
6+ expect ( await part1 ( 'input' ) ) . toEqual ( 733 ) ;
7+ } ) ;
8+
9+ test ( 'Part 2' , async ( ) => {
10+ // expect(await part2('testInput1')).toEqual(31);
11+ // expect(await part2('input')).toEqual(29379307);
12+ } ) ;
13+ } ) ;
Original file line number Diff line number Diff line change 1+ import path from "node:path" ;
2+ import { readInputLineByLine } from "@utils/io" ;
3+
4+ export async function part1 ( inputFile : string ) {
5+ return await day5 ( inputFile , findFreshIngredients ) ;
6+ }
7+
8+ export async function part2 ( inputFile : string ) {
9+ return await day5 ( inputFile ) ;
10+ }
11+
12+ type IdRange = { from : number ; to : number } ;
13+
14+ async function day5 ( inputFile : string , calcFn ?: ( idRanges : IdRange [ ] , ingredientIds : number [ ] ) => number ) {
15+ const inputPath = path . join ( __dirname , inputFile ) ;
16+ const lines = await readInputLineByLine ( inputPath ) ;
17+
18+ const idRanges : IdRange [ ] = [ ] ;
19+ const ingredientIds : number [ ] = [ ] ;
20+ for ( const line of lines ) {
21+ if ( line === '' )
22+ continue ;
23+ if ( line . indexOf ( '-' ) !== - 1 ) {
24+ const split = line . split ( '-' ) . map ( Number ) ;
25+ idRanges . push ( { from : split [ 0 ] , to : split [ 1 ] } ) ;
26+ } else {
27+ ingredientIds . push ( Number ( line ) ) ;
28+ }
29+ }
30+
31+ return calcFn ?.( idRanges , ingredientIds ) ;
32+ }
33+
34+ function findFreshIngredients ( idRanges : IdRange [ ] , ingredientIds : number [ ] ) : number {
35+ let freshCount = 0 ;
36+ for ( const id of ingredientIds )
37+ for ( const idRange of idRanges ) {
38+ if ( id < idRange . from || id > idRange . to )
39+ continue ;
40+ else {
41+ freshCount ++
42+ break ;
43+ }
44+ }
45+ return freshCount ;
46+ }
Original file line number Diff line number Diff line change 1+ 3-5
2+ 10-14
3+ 16-20
4+ 12-18
5+
6+ 1
7+ 5
8+ 8
9+ 11
10+ 17
11+ 32
You can’t perform that action at this time.
0 commit comments