Skip to content

Commit 499a269

Browse files
committed
07/2024
1 parent 3289d5d commit 499a269

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

2024/07/index.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { getPuzzle } from "../../utils";
2+
3+
const puzzleInput = getPuzzle(__dirname).trim();
4+
5+
const equations = puzzleInput.split("\n").map((row) => {
6+
const [testValue, numbers] = row.split(": ");
7+
8+
return {
9+
testValue: parseInt(testValue),
10+
numbers: numbers.split(" ").map((n) => parseInt(n)),
11+
};
12+
});
13+
14+
// Part 1
15+
(() => {
16+
console.time("part 1");
17+
let total = 0;
18+
19+
const compare = ({ numbers, testValue }: (typeof equations)[number]) => {
20+
if (numbers.length === 1) {
21+
return testValue === numbers[0];
22+
}
23+
24+
const [current, next, ...rest] = numbers;
25+
26+
if (current > testValue) {
27+
return false;
28+
}
29+
30+
const multiply = current * next;
31+
const sum = current + next;
32+
33+
if (multiply > testValue && sum > testValue) {
34+
return false;
35+
}
36+
37+
return (
38+
compare({ numbers: [multiply, ...rest], testValue }) ||
39+
compare({ numbers: [sum, ...rest], testValue })
40+
);
41+
};
42+
43+
for (let i = 0; i < equations.length; i++) {
44+
const equation = equations[i];
45+
46+
if (compare(equation)) {
47+
total += equation.testValue;
48+
}
49+
}
50+
51+
console.log("part 1 total ::", total);
52+
console.timeEnd("part 1");
53+
})();
54+
55+
// Part 2
56+
(() => {
57+
console.time("part 2");
58+
let total = 0;
59+
60+
const compare = ({ numbers, testValue }: (typeof equations)[number]) => {
61+
if (numbers.length === 1) {
62+
return testValue === numbers[0];
63+
}
64+
65+
const [current, next, ...rest] = numbers;
66+
67+
if (current > testValue) {
68+
return false;
69+
}
70+
71+
const multiply = current * next;
72+
const sum = current + next;
73+
const concat = Number(`${current}${next}`);
74+
75+
return (
76+
(multiply <= testValue &&
77+
compare({ numbers: [multiply, ...rest], testValue })) ||
78+
(sum <= testValue && compare({ numbers: [sum, ...rest], testValue })) ||
79+
(concat <= testValue &&
80+
compare({ numbers: [concat, ...rest], testValue }))
81+
);
82+
};
83+
84+
for (let i = 0; i < equations.length; i++) {
85+
const equation = equations[i];
86+
87+
if (compare(equation)) {
88+
total += equation.testValue;
89+
}
90+
}
91+
92+
console.log("part 2 total ::", total);
93+
console.timeEnd("part 2");
94+
})();

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :---------------------------------------: | :----: | :----: |
9+
| [07](https://adventofcode.com/2024/day/7) |||
910
| [06](https://adventofcode.com/2024/day/6) |||
1011
| [05](https://adventofcode.com/2024/day/5) ||  ✅ |
1112
| [04](https://adventofcode.com/2024/day/4) ||  ✅ |

0 commit comments

Comments
 (0)