-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.ts
68 lines (49 loc) · 2.23 KB
/
one.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { duration, fetchInputByDay } from "@utils";
/* -------------------------------------------------------------------------- */
type Equation = { goal: number; numbers: number[] };
/* -------------------------------------------------------------------------- */
const operations = ["+", "*"];
/* -------------------------------------------------------------------------- */
let equations: Array<Equation> = [];
/* -------------------------------------------------------------------------- */
function solve(goal: number, total: number, numbers: number[]): boolean {
const current = numbers[0];
const remaining = numbers.slice(1);
for (let i = 0; i < operations.length; i++) {
const operation = operations[i];
const newTotal = operation === "+" ? total + current : total * current;
// If we reached the goal and we have no numbers remaining, we have a correct equation
if (newTotal === goal && remaining.length === 0) return true;
// If we have not reached the end (goal or remaining numbers)
if (newTotal <= goal && remaining.length > 0) {
// If we can solve the remainder, the equation is solvable
// Otherwise we go continue with the next operation
if (solve(goal, newTotal, remaining)) return true;
else continue;
}
}
// If we didn't return true already,
// we tried all possibilites of the equation and they were not solvable.
return false;
}
function canSolve(equation: Equation): boolean {
return solve(equation.goal, equation.numbers[0], equation.numbers.slice(1));
}
/* -------------------------------------------------------------------------- */
function findSolution(input: string): number {
input.split("\n").map((line) => {
const [result, numbers] = line.split(":");
equations.push({
goal: Number(result),
numbers: numbers.trim().split(" ").map(Number),
});
});
return equations.reduce((sum, equation) => {
return (sum += canSolve(equation) ? equation.goal : 0);
}, 0);
}
/* -------------------------------------------------------------------------- */
const rawInput = await fetchInputByDay(7);
const result = duration(findSolution)(rawInput);
/* -------------------------------------------------------------------------- */
console.log(result);