From cfd9b4f2ad1a09bedf7f764f84448a61faab54a3 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 20:26:02 +0200 Subject: Refile for merging repos --- 2018/src/bin/day_10.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 2018/src/bin/day_10.rs (limited to '2018/src/bin/day_10.rs') diff --git a/2018/src/bin/day_10.rs b/2018/src/bin/day_10.rs new file mode 100644 index 0000000..082be7c --- /dev/null +++ b/2018/src/bin/day_10.rs @@ -0,0 +1,82 @@ +extern crate advent_of_code_2018; +use advent_of_code_2018::*; + +use std::str::FromStr; + +use std::error::Error; +use std::path::PathBuf; + +use std::i32; + +// cargo watch -cs "cargo run --release --bin day_10" + + + +#[derive(Debug, Clone)] +struct Star { + x: i32, + y: i32, + dx: i32, + dy: i32 +} + +impl FromStr for Star { + type Err = Box; + + fn from_str(s: &str) -> Result { + let split = s + .split(|c: char| c != '-' && !c.is_numeric()) + .filter(|s| !s.is_empty()) + .map(|s| s.parse()) + .collect::, _>>()?; + + Ok(Star { + x: split[0], + y: split[1], + dx: split[2], + dy: split[3] + }) + } +} + +fn main() -> Result<(), Box> { + let input = read_file(&PathBuf::from("inputs/10.txt"))?; + let mut stars = input.iter().map(|line| line.parse::().unwrap()).collect::>(); + + //debug!(input); + //debug!(stars); + + let screen_width = 80; + let screen_height = 80; + + for seconds in 0..1000000 { + let min_x = stars.iter().map(|s| s.x).min().unwrap(); + let max_x = stars.iter().map(|s| s.x).max().unwrap(); + let min_y = stars.iter().map(|s| s.y).min().unwrap(); + let max_y = stars.iter().map(|s| s.y).max().unwrap(); + + if max_x - min_x < screen_width && max_y - min_y < screen_height { + debug!(seconds); + for y in min_y..max_y+1 { + for x in min_x..max_x+1 { + let star_at_point = stars.iter().any(|s| s.x == x && s.y == y); + if star_at_point { + print!("#"); + } else { + print!(" "); + } + } + println!(); + } + println!(); + println!(); + } + + for star in &mut stars { + star.x += star.dx; + star.y += star.dy; + } + } + + Ok(()) +} -- cgit v1.2.3