Skip to content

Commit b57e06f

Browse files
committed
refactor
1 parent 157ce61 commit b57e06f

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/2025/day02.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
export function part1(input) {
1+
function invalidSum(input, regex) {
22
let ranges = input.split(",").map(range => range.split("-").map(Number));
33
let sum = 0;
44
for (let range of ranges) {
55
for (let i = range[0]; i <= range[1]; i++) {
66
let str = i.toString();
7-
if (str.match(/^(\d+)\1$/)) {
7+
if (str.match(regex)) {
88
sum += i;
99
}
1010
}
1111
}
1212
return sum;
1313
}
1414

15+
export function part1(input) {
16+
return invalidSum(input, /^(\d+)\1$/);
17+
}
18+
1519
export function part2(input) {
16-
let ranges = input.split(",").map(range => range.split("-").map(Number));
17-
let sum = 0;
18-
for (let range of ranges) {
19-
for (let i = range[0]; i <= range[1]; i++) {
20-
let str = i.toString();
21-
if (str.match(/^(\d+)\1+$/)) {
22-
sum += i;
23-
}
24-
}
25-
}
26-
return sum;
20+
return invalidSum(input, /^(\d+)\1+$/);
2721
}

0 commit comments

Comments
 (0)