Restructure folders and add day 3's solution
This commit is contained in:
parent
ecab4f263a
commit
519c1b63cc
6 changed files with 123 additions and 1 deletions
35
javascript/day_03/solution.mjs
Normal file
35
javascript/day_03/solution.mjs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { readFile } from "fs/promises";
|
||||
|
||||
if (process.argv.length <= 2) {
|
||||
console.error(`Needs more args >:(`).
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const fileToRead = process.argv[2];
|
||||
const file = await readFile(fileToRead, `utf-8`);
|
||||
|
||||
const pattern = /(?<cmd>mul|do|don't)\(((?<a>\d{1,3}),(?<b>\d{1,3}))?\)/g;
|
||||
|
||||
const commands = file.matchAll(pattern);
|
||||
|
||||
let part1Sum = 0;
|
||||
let part2Sum = 0;
|
||||
let enabled = true
|
||||
for (const command of commands) {
|
||||
const { cmd, a, b } = command.groups;
|
||||
|
||||
switch (cmd) {
|
||||
case "do": enabled = true; break
|
||||
case "don't": enabled = false; break
|
||||
case "mul": {
|
||||
part1Sum += a * b;
|
||||
if (enabled) {
|
||||
part2Sum += a * b;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
console.log(`Part 1 Answer: ${part1Sum}`);
|
||||
console.log(`Part 2 Answer: ${part2Sum}`);
|
||||
Loading…
Add table
Add a link
Reference in a new issue