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 a95c406

Browse filesBrowse files
committed
part 1
1 parent a0ce130 commit a95c406
Copy full SHA for a95c406

File tree

Expand file treeCollapse file tree

1 file changed

+43
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+43
-0
lines changed

‎day2/index.js

Copy file name to clipboard
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export const part1 = ({ input }) => {
2+
let result = 0;
3+
const constraints = { red: 12, green: 13, blue: 14 };
4+
5+
input.forEach((row) => {
6+
let success = true;
7+
const { hands, gameNumber } = splitInput({ row });
8+
9+
hands.forEach((hand) => {
10+
["red", "green", "blue"].forEach((color) => {
11+
if (hand.hasOwnProperty(color) && hand[color] > constraints[color]) {
12+
success = false;
13+
}
14+
});
15+
});
16+
17+
if (success) {
18+
result += gameNumber;
19+
}
20+
});
21+
22+
return result;
23+
};
24+
25+
export const part2 = ({ input }) => {
26+
const result = 0;
27+
return result;
28+
};
29+
30+
const splitInput = ({ row }) => {
31+
const game = row.split(": ")[0];
32+
const gameNumber = parseInt(game.split(" ")[1]);
33+
let hands = row.substring(game.length + 2).split("; ");
34+
hands = hands.map((hand) => {
35+
let splitHands = hand.split(", ");
36+
return splitHands.reduce((acc, nums) => {
37+
const [count, color] = nums.split(" ");
38+
acc[color] = Number(count);
39+
return acc;
40+
}, {});
41+
});
42+
return { gameNumber, hands };
43+
};

0 commit comments

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