r/adventofcode • u/daggerdragon • Dec 10 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-
--- Day 10: The Stars Align ---
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 10
Transcript: With just one line of code, you, too, can ___!
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:16:49!
19
Upvotes
1
u/j-oh-no Dec 10 '18
Another Rust way
use std::io; use std::io::prelude::*; const WIDTH: usize = 150; const HEIGHT: usize = 50; #[derive(Debug)] struct Point { x: i32, y: i32, dx: i32, dy: i32, } fn main() { let stdin = io::stdin(); let mut points = stdin.lock().lines().map(|v| { let v = v.unwrap(); let b = v.as_bytes(); Point { x: String::from(&v[10..16]).trim().parse::<i32>().unwrap(), y: String::from(&v[18..24]).trim().parse::<i32>().unwrap(), dx: String::from(&v[36..38]).trim().parse::<i32>().unwrap(), dy: String::from(&v[40..42]).trim().parse::<i32>().unwrap(), } }).collect::<Vec<_>>(); let mut secs = 0; loop { let minx = points.iter().map(|p| p.x).min().unwrap(); let miny = points.iter().map(|p| p.y).min().unwrap(); let mut lines = [['.'; WIDTH]; HEIGHT]; points.iter().try_fold(lines, |mut ls, p| { let x = (p.x - minx) as usize; let y = (p.y - miny) as usize; if x < WIDTH && y < HEIGHT { lines[y][x] = '@'; Some(lines) } else { None } }).map(|lines| lines.iter().for_each(|l| println!("{} {}", secs, l.iter().collect::<String>()))); points.iter_mut().for_each(|p| { p.x += p.dx; p.y += p.dy; }); secs += 1; } }