Commit c4ea8667 authored by Marcel Behrmann's avatar Marcel Behrmann

day(10): part 1 solved

parent 5260fdb2
......@@ -6,19 +6,213 @@ const INPUT_FILE: &str = "input.txt";
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
let input =
".#....#####...#..
##...##.#####..##
##...#...#.#####.
..#.....X...###..
..#.#.....#....##";
let input =
".#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##";
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
part_01(input.clone());
//println!("Result part 1: {}", part_01(input.clone()));
part_02(input.clone());
}
#[derive(Debug, PartialEq, PartialOrd, Clone)]
struct Asteroid(f64, f64);
#[allow(unused_variables)]
fn part_01(input: Vec<&str>) {
println!("Result of Part 1");
fn part_01(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;
for xx in yy.chars() {
if xx == '#' {
asteroids.push(Asteroid(x, y));
}
x += 1.0;
}
y += 1.0;
}
let mut max = 0;
for asteroid in &asteroids {
let count = get_asteroids_in_los(&asteroid, &asteroids);
if count > max {
max = count;
}
}
max
}
fn get_asteroids_in_los(a: &Asteroid, list: &Vec<Asteroid>) -> i64 {
let mut something_between;
let mut count = 0;
for b in list {
something_between = false;
for c in list {
if a == c || b == c {
continue;
}
let cross: f64 = ((c.1 - a.1) * (b.0 - a.0)) - ((c.0 - a.0) * (b.1 - a.1));
if cross.abs() > core::f64::EPSILON {
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;
}
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 {
count += 1;
}
}
count
}
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) {
println!("Result of Part 2");
let mut y = 0.0;
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));
}
x += 1.0;
}
y += 1.0;
}
let mut max = 0;
let mut base: Asteroid = Asteroid(0.0, 0.0);
for asteroid in &asteroids {
let count = get_asteroids_in_los(&asteroid, &asteroids);
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;
}
}
}
}
fn find_closest_asteroid_in_direction(a: &Asteroid, b: &Asteroid, list: &Vec<Asteroid>) -> Option<Asteroid> {
//println!("Checking for direction: {:?} -> {:?}", a, b);
let mut closest_distance = 10000000.0;
let mut closest: Option<Asteroid> = None;
for c in list {
if a == c || b == c {
continue;
}
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;
}
let square = (b.0 - a.0)*(b.0 - a.0) + (b.1 - a.1)*(b.1 - a.1);
if dot > square {
continue;
}
let distance = ((a.0 - c.0).powi(2) + (a.1 - c.1).powi(2)).sqrt();
if distance < closest_distance {
closest_distance = distance;
closest = Some(c.clone());
}
}
closest
}
\ 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