|
1 | | -import { getPuzzle } from "../../utils"; |
| 1 | +import { timePart1, timePart2 } from "../../utils/time-part"; |
2 | 2 |
|
3 | | -const puzzleInput = getPuzzle(__dirname).trim(); |
| 3 | +const parseInput = (input: string) => { |
| 4 | + return input.split("\n").map((row) => { |
| 5 | + const [testValue, numbers] = row.split(": "); |
4 | 6 |
|
5 | | -const equations = puzzleInput.split("\n").map((row) => { |
6 | | - const [testValue, numbers] = row.split(": "); |
| 7 | + return { |
| 8 | + testValue: parseInt(testValue), |
| 9 | + numbers: numbers.split(" ").map((n) => parseInt(n)), |
| 10 | + }; |
| 11 | + }); |
| 12 | +}; |
7 | 13 |
|
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"); |
| 14 | +export const part1 = timePart1((input: string) => { |
17 | 15 | let total = 0; |
| 16 | + const equations = parseInput(input); |
18 | 17 |
|
19 | | - const compare = ({ numbers, testValue }: (typeof equations)[number]) => { |
| 18 | + const compare = ({ |
| 19 | + numbers, |
| 20 | + testValue, |
| 21 | + }: (typeof equations)[number]): boolean => { |
20 | 22 | if (numbers.length === 1) { |
21 | 23 | return testValue === numbers[0]; |
22 | 24 | } |
@@ -48,16 +50,17 @@ const equations = puzzleInput.split("\n").map((row) => { |
48 | 50 | } |
49 | 51 | } |
50 | 52 |
|
51 | | - console.log("part 1 total ::", total); |
52 | | - console.timeEnd("part 1"); |
53 | | -})(); |
| 53 | + return total; |
| 54 | +}); |
54 | 55 |
|
55 | | -// Part 2 |
56 | | -(() => { |
57 | | - console.time("part 2"); |
| 56 | +export const part2 = timePart2((input: string) => { |
58 | 57 | let total = 0; |
| 58 | + const equations = parseInput(input); |
59 | 59 |
|
60 | | - const compare = ({ numbers, testValue }: (typeof equations)[number]) => { |
| 60 | + const compare = ({ |
| 61 | + numbers, |
| 62 | + testValue, |
| 63 | + }: (typeof equations)[number]): boolean => { |
61 | 64 | if (numbers.length === 1) { |
62 | 65 | return testValue === numbers[0]; |
63 | 66 | } |
@@ -89,6 +92,5 @@ const equations = puzzleInput.split("\n").map((row) => { |
89 | 92 | } |
90 | 93 | } |
91 | 94 |
|
92 | | - console.log("part 2 total ::", total); |
93 | | - console.timeEnd("part 2"); |
94 | | -})(); |
| 95 | + return total; |
| 96 | +}); |
0 commit comments