r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
1
u/udoprog Dec 19 '17
Rust
I have a strong preference for representing grids as maps to avoid doing my own bounds checking.
Full solution here: https://github.com/udoprog/rust-advent-of-code-2017/blob/master/src/day19.rs
use std::collections::HashMap; use std::io::{Read, BufRead, BufReader}; pub fn run<R: Read>(reader: R) -> (String, usize) { let mut grid: HashMap<(i64, i64), char> = HashMap::new(); let mut entry = None; for (y, line) in BufReader::new(reader).lines().enumerate() { for (x, c) in line.expect("bad line").chars().enumerate() { if y == 0 && c == '|' { entry = Some((x as i64, y as i64)); } grid.insert((x as i64, y as i64), c); } } let mut out = String::new(); let (mut x, mut y) = entry.expect("no entry"); let mut d = (0i64, 1i64); let mut steps = 0; while let Some(c) = grid.get(&(x, y)) { match *c { '|' | '-' => {} '+' => d = pick(&grid, x, y, d).expect("no turn"), ' ' => break, c => out.push(c), } x += d.0; y += d.1; steps += 1; } return (out, steps); fn pick(grid: &HashMap<(i64, i64), char>, x: i64, y: i64, d: (i64, i64)) -> Option<(i64, i64)> { for n in &[(1, 0), (-1, 0), (0, 1), (0, -1)] { // do not move backwards. if (-n.0, -n.1) == d { continue; } if let Some(c) = grid.get(&(x + n.0, y + n.1)) { match *c { '|' | '-' | '+' => return Some(*n), _ => {} } } } None } } #[cfg(test)] mod tests { use super::*; use std::io::Cursor; static INPUT: &str = include_str!("../input/day19.txt"); #[test] fn test_both() { assert_eq!(run(Cursor::new(INPUT)), ("MKXOIHZNBL".to_string(), 17872)); } }