mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-27 04:21:45 +00:00
31 lines
773 B
JavaScript
31 lines
773 B
JavaScript
import { readFileSync } from "fs";
|
|
const t0 = performance.now();
|
|
|
|
const lines = readFileSync("input.txt")
|
|
.toString()
|
|
.split(/\r*\n/)
|
|
.filter((e) => e.length > 0);
|
|
const powers = lines.map((e) => {
|
|
const pulls = e.split(":")[1].trim().split(";");
|
|
|
|
const min = { red: 0, green: 0, blue: 0 };
|
|
pulls.forEach((cubes) => {
|
|
const values = cubes.split(",").map((e) => e.trim());
|
|
values.forEach((e) => {
|
|
const splits = e.split(" ");
|
|
const color = splits[1];
|
|
const amount = parseInt(splits[0]);
|
|
min[color] = Math.max(min[color], amount);
|
|
});
|
|
});
|
|
|
|
return Object.keys(min)
|
|
.map((e) => min[e])
|
|
.reduce((a, i) => a * i);
|
|
});
|
|
const sum = powers.reduce((a, i) => a + i);
|
|
console.log(sum);
|
|
|
|
const t1 = performance.now();
|
|
console.log(`Runtime: ${t1 - t0}ms`);
|