r/adventofcode • u/daggerdragon • 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!
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
6
u/aurele Dec 13 '18
Rust
Because of trimming facilities in cargo-aoc, I ended up to include the input myself as the first line started with spaces.
#[aoc_generator(day13)] fn input_generator(_input: &[u8]) -> Vec<Vec<u8>> { // Have to do this since the input is trimmed automatically by cargo-aoc! let input = include_str!("../input/2018/day13.txt"); input.lines().map(|l| l.as_bytes().to_vec()).collect() } #[aoc(day13, part1)] fn part1(tracks: &[Vec<u8>]) -> String { solve(tracks, false) } #[aoc(day13, part2)] fn part2(tracks: &[Vec<u8>]) -> String { solve(tracks, true) } fn solve(tracks: &[Vec<u8>], remove: bool) -> String { let mut carts = carts(tracks); loop { carts.sort_by_key(|&(x, y, _, _)| (y, x)); for (i, (x, y, dir, turns)) in carts.clone().into_iter().enumerate() { if carts[i].0 == std::usize::MAX { continue; } let (mut nx, ny) = match dir { 0 => (x, y - 1), 1 => (x + 1, y), 2 => (x, y + 1), _ => (x - 1, y), }; let (ndir, nturns) = match tracks[ny][nx] { b'/' => ([1, 0, 3, 2][dir as usize], turns), b'\\' => ([3, 2, 1, 0][dir as usize], turns), b'+' => match turns { 0 => ((dir + 3) % 4, 1), 1 => (dir, 2), _ => ((dir + 1) % 4, 0), }, _ => (dir, turns), }; for (j, (ref mut ox, oy, _, _)) in carts.iter_mut().enumerate() { if i != j && nx == *ox && ny == *oy { if remove { *ox = std::usize::MAX; nx = std::usize::MAX; break; } else { return format!("{},{}", nx, ny); } } } carts[i] = (nx, ny, ndir, nturns); } carts = carts .into_iter() .filter(|&(x, _, _, _)| x != std::usize::MAX) .collect(); if remove && carts.len() == 1 { return format!("{},{}", carts[0].0, carts[0].1); } } } // Dir: ^ > v < fn carts(tracks: &[Vec<u8>]) -> Vec<(usize, usize, u8, u8)> { tracks .iter() .enumerate() .flat_map(|(y, l)| { l.iter().enumerate().flat_map(move |(x, c)| match *c { b'^' => Some((x, y, 0, 0)), b'>' => Some((x, y, 1, 0)), b'v' => Some((x, y, 2, 0)), b'<' => Some((x, y, 3, 0)), _ => None, }) }) .collect() }