Solve 2024-07
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2024-07" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2024/07/run.ts" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
describe('2024-07', () => {
|
||||||
|
const testInput = `190: 10 19
|
||||||
|
3267: 81 40 27
|
||||||
|
83: 17 5
|
||||||
|
156: 15 6
|
||||||
|
7290: 6 8 6 15
|
||||||
|
161011: 16 10 13
|
||||||
|
192: 17 8 14
|
||||||
|
21037: 9 7 18 13
|
||||||
|
292: 11 6 16 20`;
|
||||||
|
it('first', () => {
|
||||||
|
expect(solveFirst(testInput)).toBe(3749);
|
||||||
|
});
|
||||||
|
it('second', () => {
|
||||||
|
const secondSolution = solveSecond(testInput);
|
||||||
|
expect(secondSolution).toBe(11387);
|
||||||
|
expect(secondSolution).toBeGreaterThanOrEqual(solveFirst(testInput));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import { sumBy } from '../../utils/array.ts';
|
||||||
|
|
||||||
|
export function solveFirst(input: string): number {
|
||||||
|
const regex = /^(\d*): (.*)$/gm;
|
||||||
|
const match = input.matchAll(regex);
|
||||||
|
|
||||||
|
const rows = Array.from(match, (m) => ({
|
||||||
|
sum: Number(m[1]),
|
||||||
|
digits: m[2].split(' ').map((n) => Number(n))
|
||||||
|
}));
|
||||||
|
|
||||||
|
const possible = rows.filter((r) => {
|
||||||
|
for (let t = 0; t < Math.pow(2, r.digits.length - 1); t++) {
|
||||||
|
const sol = r.digits.reduce((sum, curr, i) =>
|
||||||
|
evalTry(t, i, sum + curr, sum * curr)
|
||||||
|
);
|
||||||
|
if (sol === r.sum) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
// console.log(possible);
|
||||||
|
return sumBy(possible, (p) => p.sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function evalTry<T>(t: number, index: number, ...choices: T[]): T {
|
||||||
|
const base = choices.length;
|
||||||
|
const choice = Math.max(0, Math.floor(t / Math.pow(base, index - 1)) % base);
|
||||||
|
|
||||||
|
return choices[choice];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveSecond(input: string): number {
|
||||||
|
const regex = /^(\d*): (.*)$/gm;
|
||||||
|
const match = input.matchAll(regex);
|
||||||
|
|
||||||
|
const rows = Array.from(match, (m) => ({
|
||||||
|
sum: Number(m[1]),
|
||||||
|
digits: m[2].split(' ').map((n) => Number(n))
|
||||||
|
}));
|
||||||
|
|
||||||
|
const possible = rows.filter((r) => {
|
||||||
|
for (let t = 0; t < Math.pow(3, r.digits.length - 1); t++) {
|
||||||
|
const sol = r.digits.reduce((sum, curr, i) =>
|
||||||
|
evalTry(t, i, sum * curr, sum + curr, Number(`${sum}${curr}`))
|
||||||
|
);
|
||||||
|
if (sol === r.sum) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
// console.log(possible);
|
||||||
|
return sumBy(possible, (p) => p.sum);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
+2
-2
@@ -12,11 +12,11 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "latest",
|
"@types/bun": "latest",
|
||||||
"@types/prompts": "^2.4.9",
|
"@types/prompts": "^2.4.9",
|
||||||
|
"p-limit": "^6.1.0",
|
||||||
"prettier": "^3.4.0",
|
"prettier": "^3.4.0",
|
||||||
"prettier-plugin-organize-imports": "^4.1.0",
|
"prettier-plugin-organize-imports": "^4.1.0",
|
||||||
"prompts": "^2.4.2",
|
"prompts": "^2.4.2",
|
||||||
"replace-in-file": "^8.2.0",
|
"replace-in-file": "^8.2.0"
|
||||||
"p-limit": "^6.1.0"
|
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.7.2"
|
"typescript": "^5.7.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user