day10 task 1 beginning

This commit is contained in:
Johannes
2019-12-10 22:12:10 +01:00
parent 3917746851
commit d3ef4235a0
5 changed files with 79 additions and 1 deletions

5
input/day10.txt Normal file
View File

@@ -0,0 +1,5 @@
.#..#
.....
#####
....#
...##

View File

@@ -1,5 +1,5 @@
mod tasks;
fn main() {
tasks::day09::run();
tasks::day10::run();
}

View File

@@ -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

71
src/tasks/day10.rs Normal file
View File

@@ -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)));
}
}

View File

@@ -6,3 +6,4 @@ pub mod day06;
pub mod day07;
pub mod day08;
pub mod day09;
pub mod day10;