Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b79ddbe

Browse filesBrowse files
committed
feat: add day 1 & 2
1 parent e753d21 commit b79ddbe
Copy full SHA for b79ddbe

File tree

Expand file treeCollapse file tree

5 files changed

+120
-9
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+120
-9
lines changed

‎day 1/index.js

Copy file name to clipboardExpand all lines: day 1/index.js
-9Lines changed: 0 additions & 9 deletions
This file was deleted.

‎day 1/input.txt

Copy file name to clipboardExpand all lines: day 1/input.txt
Whitespace-only changes.

‎day 1/sample.txt

Copy file name to clipboardExpand all lines: day 1/sample.txt
Whitespace-only changes.

‎day1/index.js

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export const part1 = ({ input }) => {
2+
let result = 0;
3+
4+
const { list1, list2 } = parseInto2lists(input);
5+
list1.sort();
6+
list2.sort();
7+
8+
list1.forEach((item, index) => {
9+
result += Math.abs(item - list2[index]);
10+
});
11+
12+
return result;
13+
};
14+
15+
export const part2 = ({ input }) => {
16+
let result = 0;
17+
18+
const { list1, list2 } = parseInto2lists(input);
19+
20+
list1.forEach((item) => {
21+
const count = list2.filter((matchItem) => matchItem == item).length;
22+
if (count > 0) {
23+
result += item * count;
24+
}
25+
});
26+
27+
return result;
28+
};
29+
30+
const parseInto2lists = (input) => {
31+
const list1 = [];
32+
const list2 = [];
33+
34+
input.forEach((item) => {
35+
const split = item.split(" ");
36+
list1.push(parseInt(split[0]));
37+
list2.push(parseInt(split[1]));
38+
});
39+
40+
return { list1, list2 };
41+
};

‎day2/index.js

Copy file name to clipboard
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
export const part1 = ({ input }) => {
2+
let result = 0;
3+
4+
const safeReports = [];
5+
input.forEach((report) => {
6+
const levels = report.split(" ");
7+
if (isValidRange(levels)) {
8+
safeReports.push(report);
9+
}
10+
});
11+
12+
result = safeReports.length;
13+
14+
return result;
15+
};
16+
17+
export const part2 = ({ input }) => {
18+
let result = 0;
19+
const safeReports = [];
20+
input.forEach((report) => {
21+
const levels = report.split(" ");
22+
if (isValidRange(levels)) {
23+
safeReports.push(report);
24+
} else {
25+
for (let i = 0; i < levels.length; i++) {
26+
const withoutLevel = levels.filter((level, index) => index !== i);
27+
if (isValidRange(withoutLevel)) {
28+
safeReports.push(report);
29+
break;
30+
}
31+
}
32+
}
33+
});
34+
35+
result = safeReports.length;
36+
37+
return result;
38+
};
39+
40+
const isValidRange = (levels) => {
41+
let previousNumber = null;
42+
let direction = null;
43+
let isValid = false;
44+
levels.forEach((level, index) => {
45+
if (direction === "wrong") {
46+
return;
47+
}
48+
49+
if (!previousNumber) {
50+
previousNumber = parseInt(level);
51+
return;
52+
}
53+
54+
if (!direction) {
55+
direction = previousNumber < parseInt(level) ? "up" : "down";
56+
}
57+
58+
if ((direction === "up" && previousNumber > parseInt(level)) || (direction === "down" && previousNumber < parseInt(level))) {
59+
return (direction = "wrong");
60+
}
61+
62+
if (Math.abs(previousNumber - parseInt(level)) === 0) {
63+
return (direction = "wrong");
64+
}
65+
66+
if (Math.abs(previousNumber - parseInt(level)) > 3) {
67+
direction = "wrong";
68+
return;
69+
}
70+
71+
previousNumber = parseInt(level);
72+
73+
if (levels.length - 1 === index) {
74+
return (isValid = true);
75+
}
76+
});
77+
78+
return isValid;
79+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.