day10 part 1
This commit is contained in:
95
src/tasks/day10.rs
Normal file
95
src/tasks/day10.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use crate::utils;
|
||||
use regex::Regex;
|
||||
|
||||
pub fn task1() {
|
||||
let input = utils::read_file("input/day10.txt");
|
||||
// position=<-4, 3> velocity=< 2, 0>
|
||||
let regex = Regex::new(
|
||||
r"position=<\s*(?P<x>-?\d+),\s*(?P<y>-?\d+)> velocity=<\s*(?P<vx>-?\d+),\s*(?P<vy>-?\d+)>",
|
||||
)
|
||||
.unwrap();
|
||||
let mut lights: Vec<Light> = input
|
||||
.lines()
|
||||
//.inspect(|line| println!("{}", line))
|
||||
.map(|line| {
|
||||
let cap = regex.captures(line).unwrap();
|
||||
Light {
|
||||
x: cap["x"].parse::<i32>().unwrap(),
|
||||
y: cap["y"].parse::<i32>().unwrap(),
|
||||
vx: cap["vx"].parse::<i32>().unwrap(),
|
||||
vy: cap["vy"].parse::<i32>().unwrap(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// for light in lights.iter() {
|
||||
// println!("{:?}", light);
|
||||
// }
|
||||
|
||||
let mut old_area = get_area(&lights);
|
||||
loop {
|
||||
let new_lights = lights.iter().map(|light| light.move_copy()).collect();
|
||||
// print_lights(&new_lights);
|
||||
let new_area = get_area(&new_lights);
|
||||
// println!("Area: {} ", new_area);
|
||||
if new_area > old_area {
|
||||
println!("Found a minimum..");
|
||||
print_lights(&lights);
|
||||
break;
|
||||
} else {
|
||||
lights = new_lights;
|
||||
old_area = new_area;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_area(lights: &Vec<Light>) -> usize {
|
||||
let xmin = lights.iter().map(|it| it.x).min().unwrap();
|
||||
let xmax = lights.iter().map(|it| it.x).max().unwrap();
|
||||
let ymin = lights.iter().map(|it| it.y).min().unwrap();
|
||||
let ymax = lights.iter().map(|it| it.y).max().unwrap();
|
||||
(xmax - xmin) as usize * (ymax - ymin) as usize
|
||||
}
|
||||
|
||||
fn print_lights(lights: &Vec<Light>) {
|
||||
let xmin = lights.iter().map(|it| it.x).min().unwrap();
|
||||
let xmax = lights.iter().map(|it| it.x).max().unwrap();
|
||||
let ymin = lights.iter().map(|it| it.y).min().unwrap();
|
||||
let ymax = lights.iter().map(|it| it.y).max().unwrap();
|
||||
let width = xmax - xmin + 1;
|
||||
let height = ymax - ymin + 1;
|
||||
|
||||
let mut screen: Vec<Vec<u8>> = Vec::with_capacity(height as usize);
|
||||
for _ in 0..=height {
|
||||
let mut chars = Vec::with_capacity(width as usize);
|
||||
for _ in 0..=width {
|
||||
chars.push('.' as u8);
|
||||
}
|
||||
screen.push(chars);
|
||||
}
|
||||
lights
|
||||
.iter()
|
||||
.for_each(|light| screen[(light.y - ymin) as usize][(light.x - xmin) as usize] = '#' as u8);
|
||||
for line in screen.iter() {
|
||||
println!("{}", String::from_utf8(line.clone()).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Light {
|
||||
x: i32,
|
||||
y: i32,
|
||||
vx: i32,
|
||||
vy: i32,
|
||||
}
|
||||
|
||||
impl Light {
|
||||
fn move_copy(&self) -> Self {
|
||||
Light {
|
||||
x: self.x + self.vx,
|
||||
y: self.y + self.vy,
|
||||
vx: self.vx,
|
||||
vy: self.vy,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,4 @@ pub mod day06;
|
||||
pub mod day07;
|
||||
pub mod day08;
|
||||
pub mod day09;
|
||||
pub mod day10;
|
||||
|
||||
Reference in New Issue
Block a user