Skip to content

Commit a0ce130

Browse files
committed
day 1
1 parent 8ba9690 commit a0ce130

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

day1/index.js

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
11
export const part1 = ({ input }) => {
2-
const result = 0;
2+
let result = 0;
3+
const regex = /\d/g;
4+
5+
input.forEach((row) => {
6+
const matches = row.match(regex);
7+
result += parseInt(matches[0] + matches[matches.length - 1]);
8+
});
9+
310
return result;
411
};
512

613
export const part2 = ({ input }) => {
7-
const result = 0;
14+
let result = 0;
15+
const regex = /(?:\d|one|two|three|four|five|six|seven|eight|nine)/g;
16+
17+
const numberMap = {
18+
one: "1",
19+
two: "2",
20+
three: "3",
21+
four: "4",
22+
five: "5",
23+
six: "6",
24+
seven: "7",
25+
eight: "8",
26+
nine: "9",
27+
};
28+
29+
input.forEach((row) => {
30+
const matches = row.match(regex);
31+
const firstNumber = matches[0];
32+
33+
const first = numberMap[firstNumber] || firstNumber;
34+
35+
let lastNumber = false;
36+
let count = -1;
37+
38+
while (lastNumber === false) {
39+
const testString = row.slice(count);
40+
if (testString.match(regex)) {
41+
lastNumber = testString.match(regex)[0];
42+
}
43+
count--;
44+
}
45+
46+
const last = numberMap[lastNumber] || lastNumber;
47+
const sum = parseInt(first + last);
48+
result += sum;
49+
});
50+
851
return result;
952
};

day1/sample2.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
two1nine
2+
eightwothree
3+
abcone2threexyz
4+
xtwone3four
5+
4nineeightseven2
6+
zoneight234
7+
7pqrstsixteen

helper.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ export async function argsChecker(args) {
2020
process.exit(1);
2121
}
2222

23-
let dataType = "sample";
24-
if (args[1] === "input") {
25-
dataType = "input";
23+
let dataType = "input";
24+
if (args[1] === "sample") {
25+
dataType = "sample";
2626
}
2727

2828
return { dataType };
2929
}
3030

3131
export async function partRunner({ part, input }) {
3232
console.time(part.name);
33-
const result = part(input);
33+
const result = await part({ input });
3434
console.timeEnd(part.name);
3535
console.log(result);
3636
return result;

index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ if (!(await fileExists(`./${dayFile}/index.js`))) {
1313
const { part1, part2 } = await import(`./${dayFile}/index.js`);
1414
const data = await loadData({ day: dayFile, dataType });
1515

16-
partRunner({ part: part1, input: data });
16+
await partRunner({ part: part1, input: data });
1717

18-
partRunner({ part: part2, input: data });
18+
if (dataType === "sample" && (await fileExists(`./${dayFile}/sample2.txt`))) {
19+
await partRunner({ part: part2, input: await loadData({ day: dayFile, dataType: "sample2" }) });
20+
} else {
21+
await partRunner({ part: part2, input: data });
22+
}

0 commit comments

Comments
 (0)