(Rust) day 10 optimizations
This commit is contained in:
parent
23e23de5fe
commit
33a4152f91
|
@ -6,4 +6,3 @@ authors = ["kageru <kageru@encode.moe>"]
|
|||
[dependencies]
|
||||
regex = "1.1"
|
||||
lazy_static = "1.2.0"
|
||||
rayon = "1.0.3"
|
||||
|
|
|
@ -3,7 +3,6 @@ extern crate rayon;
|
|||
extern crate regex;
|
||||
|
||||
use regex::Regex;
|
||||
use rayon::prelude::*;
|
||||
|
||||
struct Star {
|
||||
x: i32,
|
||||
|
@ -30,10 +29,10 @@ struct Bounds {
|
|||
impl Bounds {
|
||||
pub fn from_stars(stars: &Vec<Star>) -> Self {
|
||||
Bounds {
|
||||
min_x: stars.par_iter().min_by_key(|a| a.x).unwrap().x,
|
||||
max_x: stars.par_iter().max_by_key(|a| a.x).unwrap().x,
|
||||
min_y: stars.par_iter().min_by_key(|a| a.y).unwrap().y,
|
||||
max_y: stars.par_iter().max_by_key(|a| a.y).unwrap().y
|
||||
min_x: stars.iter().min_by_key(|a| a.x).unwrap().x,
|
||||
max_x: stars.iter().max_by_key(|a| a.x).unwrap().x,
|
||||
min_y: stars.iter().min_by_key(|a| a.y).unwrap().y,
|
||||
max_y: stars.iter().max_by_key(|a| a.y).unwrap().y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,10 +51,6 @@ fn parse_star(input: &str) -> Star {
|
|||
return Star::new(x, y, x_vel, y_vel);
|
||||
}
|
||||
|
||||
fn move_star(star: &Star) -> Star {
|
||||
return Star::new(star.x + star.x_vel, star.y + star.y_vel, star.x_vel, star.y_vel);
|
||||
}
|
||||
|
||||
fn print_stars(stars: &Vec<Star>) {
|
||||
let bounds = Bounds::from_stars(&stars);
|
||||
let width: i32 = bounds.max_x - bounds.min_x;
|
||||
|
@ -74,12 +69,15 @@ fn print_stars(stars: &Vec<Star>) {
|
|||
fn main() {
|
||||
let lines: Vec<&str> = include_str!("../input").lines().collect();
|
||||
|
||||
let mut stars: Vec<Star> = lines.par_iter().map(|line| parse_star(line)).collect();
|
||||
let mut stars: Vec<Star> = lines.iter().map(|line| parse_star(line)).collect();
|
||||
|
||||
let mut bounds = Bounds::from_stars(&stars);
|
||||
let mut step = 0;
|
||||
while (bounds.max_y - bounds.min_y).abs() > 15 {
|
||||
stars = stars.par_iter().map(|star| move_star(&star)).collect();
|
||||
stars.iter_mut().for_each(|star| {
|
||||
star.x += star.x_vel;
|
||||
star.y += star.y_vel;
|
||||
});
|
||||
bounds = Bounds::from_stars(&stars);
|
||||
step+=1;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user