Solve 2024-03

This commit is contained in:
2024-12-03 10:00:46 +01:00
parent 58c62fac1b
commit 07b6299f4b
4 changed files with 50 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2024-03" type="BunRunConfiguration">
<option name="program" value="2024/03/run.ts" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
+13
View File
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'bun:test';
import { solveFirst, solveSecond } from './index.ts';
describe('2024-03', () => {
it('first', () => {
const testInput = `xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))`;
expect(solveFirst(testInput)).toBe(161);
});
it('second', () => {
const testInput = `xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))`;
expect(solveSecond(testInput)).toBe(48);
});
});
+18
View File
@@ -0,0 +1,18 @@
import { sumArray } from '../../utils/array';
export function solveFirst(input: string): number {
return sumArray(findAndEvalMul(input));
}
function findAndEvalMul(line?: string | null | undefined) {
return Array.from(
(line || '').matchAll(/mul\((\d{1,3}),(\d{1,3})\)/gm),
(m) => Number(m[1]) * Number(m[2])
);
}
export function solveSecond(input: string): number {
const splits = input.split('do()').map((l) => l.split("don't()")[0]);
return sumArray(splits.flatMap(findAndEvalMul));
}
+12
View File
@@ -0,0 +1,12 @@
import { join } from 'path';
import { solveFirst, solveSecond } from './index.ts';
const input = await Bun.file(join(__dirname, 'input.txt')).text();
const firstAnswer = solveFirst(input);
console.log(firstAnswer);
const secondAnswer = solveSecond(input);
console.log(secondAnswer);