r/adventofcode • • Dec 13 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-

--- Day 13: Mine Cart Madness ---


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 13

Transcript:

Elven chronomancy: for when you absolutely, positively have to ___.


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:44:25!

24 Upvotes

148 comments sorted by

View all comments

1

u/blfuentes Dec 13 '18

As everyday, Typescript and using object to help me storing the info. The CarPosition class keeps info about is alive or not, where is, the orientation (>, <, , v) number of intersections and some helpers functions, that allow me to now where it will be and set the orientation.

Typescript

export class CarPosition {
	cardId: number;
	state: string;
	coordX: number;
	coordY: number;
	numIntersections: number;
	numMoves: number;
	prevState: string;
	isAlive: boolean;

	// Intersections 
	// 0 => Left
	// 1 => Straight
	// 2 => Right

	constructor (id: number, value: string, coordinate: Array<number>) {
		this.cardId = id;
		this.state = value;
		this.coordX = coordinate[0];
		this.coordY = coordinate[1];
		this.numIntersections = 0;
		this.numMoves = 0;
		this.isAlive = true;
	}

	getNextMove() {
		return this.numIntersections % 3;
	}

	setOrientation(roadType: string) {
		let nextMove = this.getNextMove();
		switch (this.state) {
			case ">" :
				if ((nextMove == 0 && roadType == "+") || roadType == "/") {
					this.state = "^";
				} else if ((nextMove == 2 && roadType == "+") || roadType == "\\") {
					this.state = "v";
				}
				break;
			case "v" :
				if ((nextMove == 0 && roadType == "+") || roadType == "\\") {
					this.state = ">";
				} else if ((nextMove == 2 && roadType == "+") || roadType == "/") {
					this.state = "<";
				}
				break;
			case "<" :
				if ((nextMove == 0 && roadType == "+") || roadType == "/") {
					this.state = "v";
				} else if ((nextMove == 2 && roadType == "+") || roadType == "\\") {
					this.state = "^";
				}
				break;
			case "^" :
				if ((nextMove == 0 && roadType == "+") || roadType == "\\") {
					this.state = "<";
				} else if ((nextMove == 2 && roadType == "+") || roadType == "/") {
					this.state = ">";
				}   
				break;
		}
	}

	getNextCoordinate () {
		let nextCoord:Array<number> = [];
		switch (this.state) {
			case ">" :
				nextCoord = [this.coordX + 1, this.coordY];
				break;
			case "v" :
				nextCoord = [this.coordX, this.coordY + 1];
				break;
			case "<" :
				nextCoord = [this.coordX - 1, this.coordY];
				break;
			case "^" :
				nextCoord = [this.coordX, this.coordY - 1];
				break;
		}
		return nextCoord;
	}
}

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

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

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

function displayRoadMap (roadmap: Array<Array<string>>) {
	let rowIdx = 0;
	while (rowIdx < lines.length) {
		let rowDisplay = "";
		for (let colIdx = 0; colIdx < roadmap.length; colIdx++) {
			rowDisplay += roadmap[colIdx][rowIdx];
		}
		console.log(`${rowDisplay}`);
		rowIdx++;
	}
}

function elementIsCard (element: string) {
	return element == ">" || element == "<" || element == "^" || element == "v";
}

// INIT MAPROAD
let roadMap: Array<Array<string>> = [];
roadMap = Array(lines[0].length).fill(null).map(item => (new Array(lines.length).fill(" ")));
let roadMapNoCars: Array<Array<string>> = [];
roadMapNoCars = Array(lines[0].length).fill(null).map(item => (new Array(lines.length).fill(" ")));

let carsPositions: Array<CarPosition> = [];

// READ
let rowIdx = 0;
let cCard = 0;
for (let line of lines) {
	let lineParts = line.split('');
	for (let column = 0; column < lineParts.length; column++) {
		roadMap[column][rowIdx] = lineParts[column];
		roadMapNoCars[column][rowIdx] = lineParts[column].replace("^", "|").replace("v", "|").replace(">", "-").replace("<", "-");
		if (elementIsCard(lineParts[column])) {
			let newCar = new CarPosition(cCard++, lineParts[column], [column , rowIdx]);
			carsPositions.push(newCar);
		}
	}
	rowIdx++;
}

// 
function sortByPosition(a:CarPosition, b:CarPosition){
	if (a.coordX == b.coordX) return a.coordY - b.coordY;
	return a.coordX - b.coordX;
}

let crashed = false;
let coordCrashed: Array<number> = []
let carsLeft = carsPositions.filter(_c => _c.isAlive).length;
crashed = false;
do {
	carsPositions = carsPositions.filter(_c => _c.isAlive).sort((a, b) => sortByPosition(a, b));

	for (let car of carsPositions) {
		let originCharacter = roadMapNoCars[car.coordX][car.coordY];
		roadMap[car.coordX][car.coordY] = originCharacter;
		let nextCoord = car.getNextCoordinate();
		car.coordX = nextCoord[0];
		car.coordY = nextCoord[1];
		let nextOriginCharacter = roadMapNoCars[nextCoord[0]][nextCoord[1]];
		car.setOrientation(nextOriginCharacter);
		if (nextOriginCharacter == "+") {
			car.numIntersections += 1;
		}
		let stateNextCoord = roadMap[nextCoord[0]][nextCoord[1]];
		if (stateNextCoord == "<" || stateNextCoord == ">" || stateNextCoord == "^" || stateNextCoord == "v") {
			let crashedCar = carsPositions.find(_c => _c.coordX == nextCoord[0] && _c.coordY == nextCoord[1] && _c.cardId != car.cardId);
			if (crashedCar != undefined) {
				roadMap[crashedCar.coordX][crashedCar.coordY] = roadMapNoCars[crashedCar.coordX][crashedCar.coordY];
				if(!crashed) {
					coordCrashed = [car.coordX, car.coordY];
					crashed = true;
				}
				car.isAlive = false;
				crashedCar.isAlive = false;
				carsPositions.splice(carsPositions.indexOf(car), 1);
				carsPositions.splice(carsPositions.indexOf(crashedCar), 1);
			}
		} else {
			roadMap[car.coordX][car.coordY] = car.state;
		}
	}
	// displayRoadMap(roadMap);
	carsLeft = carsPositions.filter(_c => _c.isAlive).length;
} while (carsLeft > 1);

let carSurvivor = carsPositions.find(_c => _c.isAlive);

if (carSurvivor != undefined) {
	console.log(`Part 1 crashed: ${coordCrashed.toString()}!`);
	console.log(`Part 2 survivor: ${carSurvivor.coordX},${carSurvivor.coordY}!`);
}

2

u/aoc-fan Dec 13 '18

1

u/blfuentes Dec 14 '18

really clean. I am still learning so I have to check this kind of solutions to see the all available features. Thanks for sharing!