-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from hildjj/day3
Day 3
- Loading branch information
Showing
4 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]; | ||
} |
Submodule inputs
updated
from bdebd8 to b7be02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default [187194524, 127092535]; |