adventofcode/2021/06/build/main.js

69 lines
5.0 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const process_1 = require("process");
class LanternFish {
constructor(_days) {
this.isNew = false;
this.justBirthed = false;
this.days = _days;
this.isNew = true;
this.justBirthed = false;
}
eligibileForNewFish() {
return this.days == 0;
}
passDay() {
if (this.isNew) {
this.isNew = false;
return;
}
if (this.justBirthed) {
this.justBirthed = false;
return;
}
this.days--;
}
createNew() {
this.days = 6;
this.justBirthed = true;
return new LanternFish(8);
}
}
function createBaseFishPopulation(baseDays) {
const fish = baseDays.map(x => new LanternFish(x));
fish.map(x => x.isNew = false);
return fish;
}
function simulateDay(fish) {
const newFish = fish.filter(x => x.eligibileForNewFish()).map(x => x.createNew());
const fishesToHandle = [...fish, ...newFish];
fishesToHandle.forEach(x => x.passDay());
return fishesToHandle;
}
function iterateOverGivenDaySpan(days, fish) {
for (let i = 0; i < days; i++) {
fish = simulateDay(fish);
console.log(`Day ${i + 1}|Fishes: ${fish.length}`);
}
return fish;
}
function printAllFishDays(fish) {
return fish.days.toString();
}
function main() {
console.log(JSON.stringify(process_1.argv));
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));
let fish = createBaseFishPopulation(inputs);
fish = iterateOverGivenDaySpan(256, fish);
console.log(`Fishes ${fish.length}`);
}
else {
console.log("No filename");
(0, process_1.exit)();
}
}
main();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL21haW4udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwyQkFBaUM7QUFDakMscUNBQW9DO0FBRXBDLE1BQU0sV0FBVztJQUloQixZQUFZLEtBQWE7UUFGekIsVUFBSyxHQUFZLEtBQUssQ0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