86 lines
2.2 KiB
Rust
86 lines
2.2 KiB
Rust
#[macro_use]
|
|
extern crate lazy_static;
|
|
extern crate regex;
|
|
|
|
use regex::Regex;
|
|
|
|
struct Star {
|
|
x: i32,
|
|
y: i32,
|
|
x_vel: i32,
|
|
y_vel: i32,
|
|
}
|
|
|
|
impl Star {
|
|
pub fn new(x: i32, y: i32, x_vel: i32, y_vel: i32) -> Self {
|
|
Star {
|
|
x, y, x_vel, y_vel
|
|
}
|
|
}
|
|
}
|
|
|
|
lazy_static! {
|
|
static ref STAR_REGEX: Regex = Regex::new(r"position=<\s?(-?\d+),\s{1,2}(-?\d+)>\svelocity=<\s?(-?\d+),\s{1,2}(-?\d+)>").unwrap();
|
|
}
|
|
|
|
fn main() {
|
|
let lines: Vec<&str> = include_str!("../input").lines().collect();
|
|
|
|
let mut stars: Vec<Star> = lines.iter().map(|line| parse_star(line)).collect();
|
|
|
|
let mut min_y: i32 = 0;
|
|
let mut max_y: i32 = 11;
|
|
|
|
let mut step = 0;
|
|
|
|
while (max_y - min_y).abs() > 10 {
|
|
stars.iter_mut().for_each(|star| {
|
|
star.x += star.x_vel;
|
|
star.y += star.y_vel;
|
|
});
|
|
|
|
min_y = stars.iter().min_by_key(|a| a.y).unwrap().y;
|
|
max_y = stars.iter().max_by_key(|a| a.y).unwrap().y;
|
|
|
|
step += 1;
|
|
}
|
|
|
|
// Part 1
|
|
print_stars(&stars);
|
|
|
|
// Part 2
|
|
println!("{}", step);
|
|
}
|
|
|
|
fn print_stars(stars: &Vec<Star>) {
|
|
let min_x = stars.iter().min_by_key(|a| a.x).unwrap().x;
|
|
let max_x = stars.iter().max_by_key(|a| a.x).unwrap().x;
|
|
let min_y= stars.iter().min_by_key(|a| a.y).unwrap().y;
|
|
let max_y = stars.iter().max_by_key(|a| a.y).unwrap().y;
|
|
|
|
let width: i32 = max_x - min_x;
|
|
let height: i32 = max_y - min_y;
|
|
|
|
let line = vec!('.'; (width + 1) as usize);
|
|
let mut field = vec!(line.clone(); (height + 1) as usize);
|
|
|
|
for star in stars {
|
|
field[(star.y - min_y) as usize][(star.x - min_x) as usize] = '#';
|
|
}
|
|
|
|
for l in field {
|
|
let s: String = l.into_iter().collect();
|
|
println!("{}", s);
|
|
}
|
|
}
|
|
|
|
fn parse_star(input: &str) -> Star {
|
|
let captures = STAR_REGEX.captures(input).expect("unexpected input line");
|
|
let (x, y, x_vel, y_vel): (i32, i32, i32, i32);
|
|
x = captures.get(1).unwrap().as_str().parse::<i32>().unwrap();
|
|
y = captures.get(2).unwrap().as_str().parse::<i32>().unwrap();
|
|
x_vel = captures.get(3).unwrap().as_str().parse::<i32>().unwrap();
|
|
y_vel = captures.get(4).unwrap().as_str().parse::<i32>().unwrap();
|
|
return Star::new(x, y, x_vel, y_vel);
|
|
}
|