Browse Source

Solve 2023 day 2 part 2

master
Garrit Franke 5 months ago
parent
commit
9179a8b5d2
Signed by: garrit
GPG Key ID: BB54AF7EB0939F3D
  1. 27
      2023/Day2/javascript/solve.js

27
2023/Day2/javascript/solve.js

@ -12,7 +12,7 @@ const part1 = (lines) => {
blue: 14,
};
games = lines.map((line) => {
const games = lines.map((line) => {
const [game, results] = line.split(": ");
const [raw, id] = game.match(/Game (\d+)/);
@ -36,4 +36,29 @@ const part1 = (lines) => {
.reduce((sum, { id }) => sum + id, 0);
};
const part2 = (lines) => {
const games = lines.map((line) => {
const [game, results] = line.split(": ");
const [raw, id] = game.match(/Game (\d+)/);
const sets = results.split("; ");
const highest = { red: 0, green: 0, blue: 0 };
for (const set of sets) {
const entries = set.split(", ");
for (const entry of entries) {
const [raw, amount, color] = entry.match(/(\d+) ([a-z]*)/);
const amountParsed = Number.parseInt(amount, 10);
if (amountParsed > highest[color]) {
highest[color] = amountParsed;
}
}
}
return highest.red * highest.blue * highest.green;
});
return games.reduce((acc, curr) => acc + curr, 0);
};
console.log(part1(input));
console.log(part2(input));

Loading…
Cancel
Save