namespace AdventOfCode2019_12_1 { const DAY = 12; const PROBLEM = 1; export async function run() { let input = await AdventOfCode.getInput(DAY); if (input == null) return; let moons = input .trim() .split(new RegExp('\r?\n')) .map(p => p.replace(new RegExp('[<>\\sxyz=]', 'g'), "")) .map(p => p.split(",").map(p => parseInt(p))) .map(p => new Moon(p[0], p[1], p[2])); for(let i=0; i<1000;i++) { output(i, moons); step(moons); } output(1000, moons); const energy = moons.map(m => m.getEnergy()).reduce((a,b) => a+b); AdventOfCode.output(DAY, PROBLEM, energy.toString()); } function output(i: number, moons: Moon[]) { AdventOfCode.outputConsole("======== "+i+" ========"); for(let m of moons) AdventOfCode.outputConsole(m.toString()); AdventOfCode.outputConsole(); } function step(moons: Moon[]) { for(let i1=0; i1moons[i2].x) { moons[i1].dx--; moons[i2].dx++; } if (moons[i1].ymoons[i2].y) { moons[i1].dy--; moons[i2].dy++; } if (moons[i1].zmoons[i2].z) { moons[i1].dz--; moons[i2].dz++; } } for(let i=0; i, vel= [pot=${this.getPotEnergy()}|kin=${this.getKinEnergy()}] => ${this.getEnergy()}`; } public getPotEnergy(): number { return Math.abs(this.x)+Math.abs(this.y)+Math.abs(this.z); } public getKinEnergy(): number { return Math.abs(this.dx)+Math.abs(this.dy)+Math.abs(this.dz); } public getEnergy(): number { return this.getPotEnergy() * this.getKinEnergy(); } } }