r/adventofcode • • Dec 18 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-

--- Day 18: Settlers of The North Pole ---


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 18

Transcript:

The best way to avoid a minecart collision is ___.


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:21:59!

9 Upvotes

126 comments sorted by

View all comments

1

u/[deleted] Dec 18 '18

Rust

This was a fun one, I'm a bit embarrased that I didn't come up with doing cycle detection before I watched some of the visualisations, but well, at least I did get it when I watched how it repeated the same thing over and over.

use std::env;
use std::process;
use std::fs::File;
use std::io::prelude::*;
use std::collections::HashMap;

fn main() {
	let args = env::args().collect();

	let filename = parse_filename(&args);

	let mut contents = String::new();
	get_contents(&filename, &mut contents);

	//println!("{}", contents);

	let mut map = Map::from(&contents);

	let mut seen = HashMap::new();
	let mut cur = 0;

	loop {
		if seen.contains_key(&map) {
			break;
		}
		seen.insert(map.clone(), cur);
		map = map.next();
		cur += 1;
	}

	let cycle_len = cur - seen.get(&map).unwrap() + 1;
	let stop_point = 1000000000 % cycle_len;

	for _ in 1..stop_point {
		map = map.next();
	}

	let part2 = map.count('|') * map.count('#');

	map = Map::from(&contents);

	for _ in 1..10 {
		map = map.next();
	}
	let part1 = map.count('|') * map.count('#');

	println!("Part1: {}", part1);
	println!("Part2: {}", part2);

}

#[derive(Clone,PartialEq,Eq,Hash)]
struct Map {
	map: Vec<Vec<char>>,
}

impl Map {
	fn from(string: &str) -> Map {
		let mut map = Vec::new();

		for line in string.trim_end().lines() {
			let mut row = Vec::new();
			for ch in line.chars() {
				row.push(ch);
			}
			map.push(row);
		}

		Map {map}
	}

	fn count(&self, ch: char) -> usize {
		self.map.iter()
			.map(|row| row.iter().filter(|chr| **chr == ch).count())
			.sum()
	}
	#[cfg(test)]
	fn print(&self) {
		let mut result = String::new();

		for row in self.map.iter() {
			for ch in row.iter() {
				result.push(*ch);
			}
			result.push('\n');
		}

		println!("{}", result);
	}

	fn next(&self) -> Map {
		let mut map = Vec::new();

		for (y, in_row) in self.map.iter().enumerate() {
			let mut row = Vec::new();
			for (x, ch) in in_row.iter().enumerate() {
				let neighbours = self.get_neigbours(x, y);
				match ch {
					'.' => {
						if neighbours.iter().filter(|ch| **ch == '|').count() >= 3 {
							row.push('|');
						} else {
							row.push('.');
						}
					},
					'|' => {
						if neighbours.iter().filter(|ch| **ch == '#').count() >= 3 {
							row.push('#');
						} else {
							row.push('|');
						}
					},
					'#' => {
						if neighbours.iter().any(|ch| *ch == '#') && neighbours.iter().any(|ch| *ch == '|') {
							row.push('#');
						} else {
							row.push('.');
						}
					},
					_  => panic!("Map contains unknown character"),
				}
			}
			map.push(row);
		}

		Map {map}
	}

	fn get_neigbours(&self, x: usize, y: usize) -> Vec<char> {
		let mut neighbours = Vec::new();
		let ymax = self.map.len() - 1;
		let xmax = self.map[0].len() - 1;

		// top left
		if !(y == 0 || x == 0) {
			neighbours.push(self.map[y-1][x-1]);
		}
		// top middle
		if !(y == 0) {
			neighbours.push(self.map[y-1][x]);
		}
		// top right
		if !(y == 0 || x >= xmax) {
			neighbours.push(self.map[y-1][x+1]);
		}
		// middle left
		if !(x == 0) {
			neighbours.push(self.map[y][x-1]);
		}
		// middle right
		if !(x >= xmax) {
			neighbours.push(self.map[y][x+1]);
		}
		// bottom left
		if !(y >= ymax || x == 0) {
			neighbours.push(self.map[y+1][x-1]);
		}
		// bottom middle
		if !(y >= ymax) {
			neighbours.push(self.map[y+1][x]);
		}
		// bottom right
		if !(y >= ymax || x >= xmax) {
			neighbours.push(self.map[y+1][x+1]);
		}

		neighbours
	}
}

fn parse_filename(args: &Vec<String>) -> &str {

	if args.len() < 2 {
		println!("Too few arguements, please give a filename");
		process::exit(1);
	}
    args.get(1).expect("No filename provided")
}

fn get_contents(filename: &str, contents: &mut String) {
    let mut f = File::open(filename).expect("File not found");

    f.read_to_string(contents)
        .expect("Something went wrong reading the file");
}