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 "./day06" ;
2+
3+ describe ( '2025 Day 6' , ( ) => {
4+ test ( 'Part 1' , async ( ) => {
5+ expect ( await part1 ( 'testInput1' ) ) . toEqual ( 4277556 ) ;
6+ expect ( await part1 ( 'input' ) ) . toEqual ( 5877594983578 ) ;
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+ import { forEach } from "mathjs" ;
4+
5+ export async function part1 ( inputFile : string ) {
6+ return await day6 ( inputFile , calcGrantTotal ) ;
7+ }
8+
9+ export async function part2 ( inputFile : string ) {
10+ return await day6 ( inputFile ) ;
11+ }
12+
13+ async function day6 ( inputFile : string , calcFn ?: ( numberRows : number [ ] [ ] , ops : string [ ] ) => number ) {
14+ const inputPath = path . join ( __dirname , inputFile ) ;
15+ const lines = await readInputLineByLine ( inputPath ) ;
16+
17+ const numberRows : number [ ] [ ] = lines . slice ( 0 , - 1 ) . map ( line => line . trim ( ) . split ( / \s + / ) . map ( Number ) ) ;
18+ const ops : string [ ] = lines [ lines . length - 1 ] . split ( / \s + / ) ;
19+
20+ return calcFn ?.( numberRows , ops ) ;
21+ }
22+
23+ function calcGrantTotal ( numberRows : number [ ] [ ] , ops : string [ ] ) : number {
24+ let total : number = 0 ;
25+ const cols = numberRows [ 0 ] . length ;
26+ for ( let i = 0 ; i < cols ; i ++ ) {
27+ const op = ops [ i ] ;
28+ const column : number [ ] = numberRows . map ( row => row [ i ] ) ;
29+ const result = column . reduce ( ( acc , cur ) => op === '+' ? acc + cur : acc * cur , op === '+' ? 0 : 1 ) ;
30+ total += result ;
31+ }
32+ return total ;
33+ }
Original file line number Diff line number Diff line change 1+ 123 328 51 64
2+ 45 64 387 23
3+ 6 98 215 314
4+ * + * +
You can’t perform that action at this time.
0 commit comments