Commit 73c2b51d authored by Marcel Behrmann's avatar Marcel Behrmann

day(10): part 2 solved

parent c4ea8667
......@@ -6,7 +6,7 @@ const INPUT_FILE: &str = "input.txt";
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
let input =
/*let input =
".#....#####...#..
##...##.#####..##
##...#...#.#####.
......@@ -33,13 +33,13 @@ fn main() {
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##";
###.##.####.##.#..##";*/
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
//println!("Result part 1: {}", part_01(input.clone()));
part_02(input.clone());
println!("Result part 1: {}", part_01(input.clone()));
println!("Result part 2: {}", part_02(input.clone()));
}
#[derive(Debug, PartialEq, PartialOrd, Clone)]
......@@ -66,7 +66,7 @@ fn part_01(input: Vec<&str>) -> i64 {
let mut max = 0;
for asteroid in &asteroids {
let count = get_asteroids_in_los(&asteroid, &asteroids);
let count = get_count_in_los(&asteroid, &asteroids);
if count > max {
max = count;
......@@ -76,7 +76,7 @@ fn part_01(input: Vec<&str>) -> i64 {
max
}
fn get_asteroids_in_los(a: &Asteroid, list: &Vec<Asteroid>) -> i64 {
fn get_count_in_los(a: &Asteroid, list: &Vec<Asteroid>) -> i64 {
let mut something_between;
let mut count = 0;
......@@ -118,101 +118,72 @@ fn get_asteroids_in_los(a: &Asteroid, list: &Vec<Asteroid>) -> i64 {
count
}
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) {
let mut y = 0.0;
fn get_asteroids_in_los(a: &Asteroid, list: &Vec<Asteroid>) -> Vec<Asteroid> {
let mut asteroids: Vec<Asteroid> = Vec::new();
for yy in input {
let mut x = 0.0;
for xx in yy.chars() {
if xx == '#' {
asteroids.push(Asteroid(x, y));
let mut something_between;
for b in list {
something_between = false;
for c in list {
if a == c || b == c {
continue;
}
x += 1.0;
}
y += 1.0;
}
let cross: f64 = ((c.1 - a.1) * (b.0 - a.0)) - ((c.0 - a.0) * (b.1 - a.1));
let mut max = 0;
let mut base: Asteroid = Asteroid(0.0, 0.0);
if cross.abs() > core::f64::EPSILON {
continue;
}
for asteroid in &asteroids {
let count = get_asteroids_in_los(&asteroid, &asteroids);
let dot: f64 = (c.0 - a.0) * (b.0 - a.0) + ((c.1 - a.1) * (b.1 - a.1));
if dot < 0.0 {
continue;
}
if count > max {
max = count;
base = asteroid.clone();
}
}
let mut kill_count = 0;
let base_vec: Asteroid = Asteroid(0.0, -1.0);
const DEG2RAD : f64 = 0.01745329252;
println!("Using base: {:?}", base);
while asteroids.len() > 5 {
for degree in 0..360 {
let theta = DEG2RAD * (degree as f64);
println!("theta: {}", theta);
let cs = theta.cos();
let sn = theta.sin();
let target = Asteroid(
base.0 + (base_vec.0 * cs - base_vec.1 * sn) * 500.0,
base.1 + (base_vec.0 * sn + base_vec.1 * cs) * 500.0,
);
let asteroid = find_closest_asteroid_in_direction(&base, &target, &asteroids);
if let Some(a) = asteroid {
let index = asteroids.iter().position(|x| *x == a).unwrap();
println!("#{} Removed asteroid: {:?}", kill_count, a);
asteroids.remove(index);
kill_count += 1;
let square = (b.0 - a.0)*(b.0 - a.0) + (b.1 - a.1)*(b.1 - a.1);
if dot > square {
continue;
}
something_between = true;
break;
}
if !something_between {
asteroids.push(b.clone());
}
}
asteroids
}
fn find_closest_asteroid_in_direction(a: &Asteroid, b: &Asteroid, list: &Vec<Asteroid>) -> Option<Asteroid> {
//println!("Checking for direction: {:?} -> {:?}", a, b);
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) -> i64 {
let mut y = 0.0;
let mut asteroids: Vec<Asteroid> = Vec::new();
for yy in input {
let mut x = 0.0;
let mut closest_distance = 10000000.0;
let mut closest: Option<Asteroid> = None;
for xx in yy.chars() {
if xx == '#' {
asteroids.push(Asteroid(x, y));
}
for c in list {
if a == c || b == c {
continue;
x += 1.0;
}
let cross: f64 = ((c.1 - a.1) * (b.0 - a.0)) - ((c.0 - a.0) * (b.1 - a.1));
if cross.abs() > 0.0000000001 {
continue;
}
let dot: f64 = (c.0 - a.0) * (b.0 - a.0) + ((c.1 - a.1) * (b.1 - a.1));
if dot < 0.0 {
continue;
}
y += 1.0;
}
let square = (b.0 - a.0)*(b.0 - a.0) + (b.1 - a.1)*(b.1 - a.1);
let base = Asteroid(28.0, 29.0);
if dot > square {
continue;
}
let distance = ((a.0 - c.0).powi(2) + (a.1 - c.1).powi(2)).sqrt();
let mut list = get_asteroids_in_los(&base, &asteroids);
if distance < closest_distance {
closest_distance = distance;
closest = Some(c.clone());
}
}
list.sort_by(|e, b| ((b.0 - base.0).atan2(b.1 - base.1)).partial_cmp(&((e.0 - base.0).atan2(e.1 - base.1))).unwrap());
closest
(list[199].0 as i64) * 100 + (list[199].1 as i64)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment