diff --git a/input/day10.txt b/input/day10.txt new file mode 100644 index 0000000..6a9f2d1 --- /dev/null +++ b/input/day10.txt @@ -0,0 +1,5 @@ +.#..# +..... +##### +....# +...## \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 33ea898..eb31e43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod tasks; fn main() { - tasks::day09::run(); + tasks::day10::run(); } diff --git a/src/tasks/day09.rs b/src/tasks/day09.rs index 269703c..eddc162 100644 --- a/src/tasks/day09.rs +++ b/src/tasks/day09.rs @@ -1,5 +1,6 @@ use super::day05::{IntCodeComputer, RAM}; +#[allow(dead_code)] pub fn run() { let input = std::fs::read_to_string("input/day09.txt").unwrap(); let ram: RAM = input diff --git a/src/tasks/day10.rs b/src/tasks/day10.rs new file mode 100644 index 0000000..dec8ed3 --- /dev/null +++ b/src/tasks/day10.rs @@ -0,0 +1,71 @@ +use std::collections::HashSet; + +#[allow(dead_code)] +pub fn run() { + let asteroids: HashSet<(i32, i32)> = std::fs::read_to_string("input/day10.txt") + .unwrap() + .lines() + .enumerate() + .map(|(y, line)| { + line.chars() + .enumerate() + .filter(|(_, it)| *it == '#') + .map(move |(x, _)| (x as i32, y as i32)) + }) + .flatten() + .collect(); + task1(&asteroids); +} + +fn task1(asteroids: &HashSet<(i32, i32)>) { + let max = asteroids + .iter() + .map(|asteroid| { + //println!("inspecting {:?}", asteroid); + // map to number of other asteroids it sees + let mut others = asteroids.clone(); + others.remove(&asteroid); + // count those asteroids that are not blocked by another + let count = others + .iter() + .filter(|target| { + !others + .iter() + .any(|occluder| blocks_view(*asteroid, **target, *occluder)) + }) + //.inspect(|target| println!("sees {:?}", target)) + .count(); + println!("{:?}: {}", asteroid, count); + count + }) + .max() + .unwrap(); + println!( + "Task 1: maximum visible asteroids from another asteroid are {}", + max + ); //283 too high, 257 too low +} + +fn blocks_view(pov: (i32, i32), target: (i32, i32), occluder: (i32, i32)) -> bool { + let ux = occluder.0 - pov.0; + let uy = occluder.1 - pov.1; + + let sx = (target.0 - pov.0) as f32 / ux as f32; + let sy = (target.1 - pov.1) as f32 / uy as f32; + //dbg!((ux, uy, sx, sy)); + match (ux, uy) { + (0, _) => sy.abs() > 1.0 && target.0 == pov.0, + (_, 0) => sx.abs() > 1.0 && target.1 == pov.1, + (_, _) => sx.abs() > 1.0 && sx == sy, + } +} + +mod test { + #[test] + fn block_test() { + assert_eq!(true, super::blocks_view((3, 4), (1, 0), (2, 2))); + assert_ne!(true, super::blocks_view((3, 4), (2, 2), (1, 0))); + assert_ne!(true, super::blocks_view((3, 4), (1, 0), (1, 2))); + assert_eq!(true, super::blocks_view((4, 4), (4, 0), (4, 3))); + } +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 30a8d06..5c5d3bb 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -6,3 +6,4 @@ pub mod day06; pub mod day07; pub mod day08; pub mod day09; +pub mod day10;