generated from eric/adventofcode2023
day six part one
This commit is contained in:
63
06/code.js
Normal file
63
06/code.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let total = 0;
|
||||
|
||||
const problems = [];
|
||||
for (const row of usedArray) {
|
||||
const contents = row.trim().split(/\s+/);
|
||||
console.log(contents);
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const element = contents[i];
|
||||
if (!problems[i]) {
|
||||
problems[i] = [];
|
||||
}
|
||||
problems[i].push(element);
|
||||
}
|
||||
}
|
||||
|
||||
for (let problem of problems) {
|
||||
problem = problem.reverse();
|
||||
const operator = problem.shift();
|
||||
let answer = operator === '+' ? 0 : 1;
|
||||
for (const number of problem) {
|
||||
if (operator === '+') {
|
||||
answer += Number.parseInt(number);
|
||||
} else {
|
||||
answer *= Number.parseInt(number);
|
||||
}
|
||||
}
|
||||
total += answer;
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(total);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
for (const element of usedArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log();
|
||||
|
||||
// functions
|
||||
Reference in New Issue
Block a user