summaryrefslogtreecommitdiff
path: root/2018/src/bin/day_10.rs
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/bin/day_10.rs')
-rw-r--r--2018/src/bin/day_10.rs82
1 files changed, 82 insertions, 0 deletions
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<Error>;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let split = s
+ .split(|c: char| c != '-' && !c.is_numeric())
+ .filter(|s| !s.is_empty())
+ .map(|s| s.parse())
+ .collect::<Result<Vec<i32>, _>>()?;
+
+ Ok(Star {
+ x: split[0],
+ y: split[1],
+ dx: split[2],
+ dy: split[3]
+ })
+ }
+}
+
+fn main() -> Result<(), Box<Error>> {
+ let input = read_file(&PathBuf::from("inputs/10.txt"))?;
+ let mut stars = input.iter().map(|line| line.parse::<Star>().unwrap()).collect::<Vec<_>>();
+
+ //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(())
+}