end of day 3
This commit is contained in:
43
day03/js/day03a.js
Normal file
43
day03/js/day03a.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import { promises as fsp } from "node:fs";
|
||||
|
||||
async function asyncReadFile(filename) {
|
||||
try {
|
||||
const contents = await fsp.readFile(filename, "utf-8");
|
||||
|
||||
return contents.trim();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
const findHighestOutput = (bank) => {
|
||||
let arr = Array.from(bank);
|
||||
let highest = 0;
|
||||
arr.forEach((val, idx) => {
|
||||
if (arr.length === idx + 1) return;
|
||||
for (let i = idx + 1; i < arr.length; i++) {
|
||||
const output = `${val}${arr[i]}` * 1;
|
||||
if (output > highest) highest = output;
|
||||
}
|
||||
});
|
||||
console.log(highest);
|
||||
return highest;
|
||||
};
|
||||
|
||||
/*
|
||||
const dataToParse = `987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111`;
|
||||
*/
|
||||
|
||||
const dataToParse = await asyncReadFile("../input.txt");
|
||||
|
||||
const banks = dataToParse.split("\n");
|
||||
let total = 0;
|
||||
|
||||
banks.forEach((bank) => {
|
||||
total += findHighestOutput(bank);
|
||||
});
|
||||
|
||||
console.log(`Total: ${total}`);
|
||||
Reference in New Issue
Block a user