diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-12-10 07:28:04 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-12-10 07:28:04 +0200 |
commit | 324e2bf31fec7dfb2de0a328b41a7e7e5f6075a9 (patch) | |
tree | be31f8065f4c04681ccc6cf692064298567a385a /src/bin | |
parent | bae410fb9c8d36481c9049f9892b3d65e277baea (diff) |
Day 10: Finding patterns in the stars
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/day_10.rs | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/src/bin/day_10.rs b/src/bin/day_10.rs index 12c29cd..082be7c 100644 --- a/src/bin/day_10.rs +++ b/src/bin/day_10.rs @@ -1,18 +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); - println!("Input: {:?}", input); + 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(()) } |