Skip to content

Commit

Permalink
Merge pull request #12 from hildjj/day3
Browse files Browse the repository at this point in the history
Day 3
  • Loading branch information
hildjj authored Dec 3, 2024
2 parents 07b9e9a + 3645e20 commit f9512f3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions day3.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
commands
= cmd:command* { return cmd.filter(d => d != null) }

command
= "don't()" { return false }
/ "do()" { return true }
/ "mul(" @num "," @num ")"
/ . { return null }

num
= n:$[0-9]+ { return parseInt(n, 10) }
26 changes: 26 additions & 0 deletions day3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { type MainArgs, parseFile } from './lib/utils.ts';

type Parsed = (boolean | number[])[];

function part1(inp: Parsed): number {
return inp
.filter((i) => Array.isArray(i))
.reduce((t, [l, r]) => t + (l * r), 0);
}

function part2(inp: Parsed): number {
let on = true;
return inp.reduce((t, v) => {
if (typeof v === 'boolean') {
on = v;
} else if (on) {
t += v[0] * v[1];
}
return t;
}, 0);
}

export default async function main(args: MainArgs): Promise<[number, number]> {
const inp = await parseFile<Parsed>(args);
return [part1(inp), part2(inp)];
}
2 changes: 1 addition & 1 deletion inputs
1 change: 1 addition & 0 deletions test/day3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [187194524, 127092535];

0 comments on commit f9512f3

Please sign in to comment.