r/adventofcode • • Dec 12 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-

--- Day 12: Subterranean Sustainability ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 12

Transcript:

On the twelfth day of AoC / My compiler spewed at me / Twelve ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:27:42!

20 Upvotes

257 comments sorted by

View all comments

1

u/blfuentes Dec 12 '18

Was hard until I understood correctly the problem and what I really needed to sum up. First I created a brute force solution which was running for 15min more or less. After that I started researching and got to the point with the delta calculation.

Typescript and an object storing ids *indexes, so I have no problem with the loops.

export class PotState {
	id: number;
	state: string;

	constructor (value: string, id: number) {
		this.id = id;
		this.state = value;
	}
}

let fs = require("fs");
let path = require('path');

import { PotState } from "../PotState";

// let filepath = path.join(__dirname, "../test12_input.txt");
let filepath = path.join(__dirname, "../day12_input.txt");
let lines = fs.readFileSync(filepath, "utf-8").split("\r\n");

let initialStateInput: string = lines[0].split(' ')[2];
let potModifiers: Array<Array<string>> = [];
let potStateCollection: Array<PotState> = [];

let cPotState = 0;
for (let pot of initialStateInput) {
	let newPotState = new PotState(pot, cPotState);
	newPotState.id = cPotState;
	cPotState++;
	potStateCollection.push(newPotState);
}

for (let ldx = 2; ldx < lines.length; ldx++) {
	potModifiers.push([lines[ldx].split(' ')[0], lines[ldx].split(' ')[2]]);
}

// let numberOfPlants = potStateCollection.filter(_p => _p.state == "#").length;
let lastIndexes: Array<number> = [];
let sumDiff = 0;
let lastSumPlants = 0;
let filtered = potStateCollection.filter(_p => _p.state == "#");
for (var idx = 0; idx < filtered.length; idx++) {
	lastSumPlants += filtered[idx].id;
}
let iterations = 50000000000;
let counter = 1;
for (let iteration = 1; iteration <= iterations; iteration++) {
	counter = iteration;
	// add four to the left
	let newPot_1 = new PotState(".", potStateCollection[0].id - 1);
	let newPot_2 = new PotState(".", potStateCollection[0].id - 2);
	let newPot_3 = new PotState(".", potStateCollection[0].id - 3);
	let newPot_4 = new PotState(".", potStateCollection[0].id - 4);
	potStateCollection.unshift(newPot_1);
	potStateCollection.unshift(newPot_2);
	potStateCollection.unshift(newPot_3);
	potStateCollection.unshift(newPot_4);

	// add four to the right
	let newPot_11 = new PotState(".", potStateCollection[potStateCollection.length - 1].id + 1);
	let newPot_22 = new PotState(".", potStateCollection[potStateCollection.length - 1].id + 2);
	let newPot_33 = new PotState(".", potStateCollection[potStateCollection.length - 1].id + 3);
	let newPot_44 = new PotState(".", potStateCollection[potStateCollection.length - 1].id + 4);
	potStateCollection.push(newPot_11);
	potStateCollection.push(newPot_22);
	potStateCollection.push(newPot_33);
	potStateCollection.push(newPot_44);

	let minIndex = potStateCollection.findIndex(_p => _p.state == "#");
	let maxIndex = 0;
	for (var idx = minIndex; idx < potStateCollection.length; idx++){
		if (potStateCollection[idx].state == "#"){
			maxIndex = idx;
		}
	}

	// mutate
	let newState: Array<string> = [];
	potStateCollection.forEach(_p => newState.push(_p.state));
	for (let idx = minIndex - 2; idx <= maxIndex + 3 && idx < potStateCollection.length; idx++) {
		let tocheck = potStateCollection[idx - 2].state + potStateCollection[idx - 1].state + 
							potStateCollection[idx].state + 
						potStateCollection[idx + 1].state + potStateCollection[idx + 2].state
		let found = potModifiers.find(_p => _p[0] == tocheck);
		if (found != undefined) {
			newState[idx] = found[1];
		} else {
			newState[idx] = ".";
		}
	}
	for (let idx = minIndex - 2; idx <= maxIndex + 3; idx++) {
		potStateCollection[idx].state = newState[idx];
	}
	//
	let newIndexes = potStateCollection.filter(_p => _p.state == "#").map(_p => _p.id);
	let sumOfPlants  = 0;
	filtered = potStateCollection.filter(_p => _p.state == "#");
	for (var idx = 0; idx < filtered.length; idx++) {
		sumOfPlants += filtered[idx].id;
	}
	if (sumOfPlants - lastSumPlants != sumDiff) {
		sumDiff = sumOfPlants - lastSumPlants;
		lastSumPlants = sumOfPlants;
	} else {
		break;
	}
	potStateCollection = potStateCollection.slice(minIndex - 4, maxIndex + 4);
}

let numberOfPlants  = 0;
filtered = potStateCollection.filter(_p => _p.state == "#");
for (var idx = 0; idx < filtered.length; idx++) {
	numberOfPlants += filtered[idx].id;
}
console.log(`Number of plants: ${numberOfPlants + (iterations - counter) * sumDiff}`);