Solve 2024-03
This commit is contained in:
@@ -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>
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user