diff --git a/day3.peggy b/day3.peggy new file mode 100644 index 0000000..da76dbd --- /dev/null +++ b/day3.peggy @@ -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) } \ No newline at end of file diff --git a/day3.ts b/day3.ts new file mode 100644 index 0000000..d2d7dff --- /dev/null +++ b/day3.ts @@ -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(args); + return [part1(inp), part2(inp)]; +} diff --git a/inputs b/inputs index bdebd85..b7be029 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit bdebd854d01ecc38593c79b52c7a7957ca9238d0 +Subproject commit b7be02964df0f17226d6d73ab316e1817183a61e diff --git a/test/day3.js b/test/day3.js new file mode 100644 index 0000000..83d102e --- /dev/null +++ b/test/day3.js @@ -0,0 +1 @@ +export default [187194524, 127092535];