mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-16 20:49:24 +00:00
day 3 (refactor)
This commit is contained in:
@@ -3,16 +3,16 @@ import * as R from "remeda";
|
|||||||
const regex = new RegExp(/mul\((\d{1,3},\d{1,3}?)\)/g);
|
const regex = new RegExp(/mul\((\d{1,3},\d{1,3}?)\)/g);
|
||||||
|
|
||||||
export function day3(input: string) {
|
export function day3(input: string) {
|
||||||
const matches = input.matchAll(regex);
|
return R.sum(
|
||||||
const numberPairs = [];
|
input
|
||||||
for (const match of matches) {
|
.matchAll(regex)
|
||||||
numberPairs.push(match[1].split(",").map((v) => Number.parseInt(v, 10)));
|
.map((match) => {
|
||||||
}
|
// match[1] looks like "2,4"
|
||||||
return R.pipe(
|
const [a, b] = match[1].split(",").map((v) => Number.parseInt(v, 10));
|
||||||
numberPairs,
|
return a * b;
|
||||||
R.map(([a, b]) => a * b),
|
})
|
||||||
R.sum,
|
.toArray(),
|
||||||
) as unknown as number;
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function day3part2(input: string) {
|
export function day3part2(input: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user