adventofcode/2021/07/build/main.js

49 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-11-30 22:36:04 +01:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const process_1 = require("process");
function second(input) {
const average = (input.reduce((a, x) => a += x) / input.length);
console.log(`Average: ${average}`);
console.log(`Summed Distance Rounded: ${input.map(x => increasingDistanceToAverage(x, Math.floor(average))).reduce((a, x) => a += x)}`);
console.log(`Summed Distance +1: ${input.map(x => increasingDistanceToAverage(x, Math.floor(average) + 1)).reduce((a, x) => a += x)}`);
console.log(`Summed Distance -1: ${input.map(x => increasingDistanceToAverage(x, Math.floor(average) - 1)).reduce((a, x) => a += x)}`);
}
function first(input) {
const med = findMedian(input.sort((a, b) => compareInt(a, b)));
console.log(`Median: ${med}`);
const distance = distanceToMedian(input, med);
console.log(`Fuel: ${distance}`);
}
function increasingDistanceToAverage(input, average) {
const diff = difference(input, average);
console.log(JSON.stringify(diff));
return diff;
}
function distanceToMedian(input, median) {
let fuel = 0;
input.forEach(x => fuel += difference(x, median));
return fuel;
}
function findMedian(input) {
const length = input.length;
let location = Math.floor(length / 2);
const value = input[location];
return value;
}
const difference = (a, b) => { return Math.abs(a - b); };
const compareInt = (a, b) => { return a - b; };
function main() {
if (process_1.argv[2] && process_1.argv[2] != "") {
const values = (0, fs_1.readFileSync)(process_1.argv[2], "utf8");
const inputs = values.replace('\n', '').split(',').map(x => parseInt(x));
first(inputs);
second(inputs);
}
else {
console.error("No filename");
(0, process_1.exit)();
}
}
main();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL21haW4udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwyQkFBaUM7QUFDakMscUNBQW9DO0FBSXBDLFNBQVMsTUFBTSxDQUFDLEtBQWU7SUFDOUIsTUFBTSxPQUFPLEdBQU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