day_8: part 1 done
This commit is contained in:
@@ -4,10 +4,39 @@ use utils::read_antena_map_input;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn resonant_collinearity(input: &str) -> AntinodeCount {
|
||||
pub fn resonant_collinearity(input: &str) -> (AntinodeCount, AntinodeCount) {
|
||||
let (roof_map, antena_list) = read_antena_map_input(input);
|
||||
|
||||
get_antinode_set_of_aligned_position(&antena_list, &roof_map).len()
|
||||
(
|
||||
get_antinode_set_of_aligned_position(&antena_list, &roof_map).len(),
|
||||
get_antinode_set_of_any_aligned_position(&antena_list, &roof_map).len(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_antinode_set_of_any_aligned_position(
|
||||
antena_list: &AntenaList,
|
||||
map: &RoofMap,
|
||||
) -> AntinodeList {
|
||||
let mut antinode_list: AntinodeList = HashSet::new();
|
||||
|
||||
for antena in antena_list {
|
||||
for complementary_antena in antena_list {
|
||||
if antena != complementary_antena && antena.2 == complementary_antena.2 {
|
||||
let distance_x = (antena.1 as i32 - complementary_antena.1 as i32).abs() as usize;
|
||||
let distance_y = (antena.0 as i32 - complementary_antena.0 as i32).abs() as usize;
|
||||
|
||||
let mut count: usize = 0;
|
||||
|
||||
// loop {
|
||||
// let test_x = distance_x * count;
|
||||
// }
|
||||
|
||||
// antinode_list.insert(pair);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
antinode_list
|
||||
}
|
||||
|
||||
pub fn get_antinode_set_of_aligned_position(
|
||||
@@ -18,15 +47,10 @@ pub fn get_antinode_set_of_aligned_position(
|
||||
|
||||
for antena in antena_list {
|
||||
for complementary_antena in antena_list {
|
||||
if antena != complementary_antena {
|
||||
if antena != complementary_antena && antena.2 == complementary_antena.2 {
|
||||
let distance_x = (antena.1 as i32 - complementary_antena.1 as i32).abs() as usize;
|
||||
let distance_y = (antena.0 as i32 - complementary_antena.0 as i32).abs() as usize;
|
||||
|
||||
println!(
|
||||
"For antenas: {:?} and {:?} the distances are x: {}, y: {}",
|
||||
antena, complementary_antena, distance_x, distance_y
|
||||
);
|
||||
|
||||
let (a, b) = get_extremes_with_variance(
|
||||
(antena.0, antena.1),
|
||||
(complementary_antena.0, complementary_antena.1),
|
||||
@@ -34,6 +58,7 @@ pub fn get_antinode_set_of_aligned_position(
|
||||
distance_x,
|
||||
map.len(),
|
||||
map[0].len(),
|
||||
map,
|
||||
);
|
||||
|
||||
if let Some(pair) = a {
|
||||
@@ -46,8 +71,6 @@ pub fn get_antinode_set_of_aligned_position(
|
||||
}
|
||||
}
|
||||
|
||||
println!("Aninode list: {:?}", antinode_list);
|
||||
|
||||
antinode_list
|
||||
}
|
||||
|
||||
@@ -59,6 +82,7 @@ pub fn get_extremes_with_variance(
|
||||
variance_x: usize,
|
||||
max_y: usize,
|
||||
max_x: usize,
|
||||
map: &RoofMap,
|
||||
) -> (Option<(usize, usize)>, Option<(usize, usize)>) {
|
||||
if a.1 > b.1 {
|
||||
if a.0 > b.0 {
|
||||
@@ -66,12 +90,22 @@ pub fn get_extremes_with_variance(
|
||||
// |_a|
|
||||
let first =
|
||||
if b.0 as i32 - variance_y as i32 >= 0 && b.1 as i32 - variance_x as i32 >= 0 {
|
||||
Some((b.0 - variance_y, b.1 - variance_x))
|
||||
let new = (b.0 - variance_y, b.1 - variance_x);
|
||||
if !is_station_between(new, b, map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let second = if a.0 + variance_y < max_y && a.1 + variance_x < max_x {
|
||||
Some((a.0 + variance_y, a.1 + variance_x))
|
||||
let new = (a.0 + variance_y, a.1 + variance_x);
|
||||
if !is_station_between(new, a, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -81,12 +115,22 @@ pub fn get_extremes_with_variance(
|
||||
// |_a|
|
||||
// |b_|
|
||||
let first = if a.0 as i32 - variance_y as i32 >= 0 && a.1 + variance_x < max_x {
|
||||
Some((a.0 - variance_y, a.1 + variance_x))
|
||||
let new = (a.0 - variance_y, a.1 + variance_x);
|
||||
if !is_station_between(new, a, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let second = if b.0 + variance_y < max_y && b.1 as i32 - variance_x as i32 >= 0 {
|
||||
Some((b.0 + variance_y, b.1 - variance_x))
|
||||
let new = (b.0 + variance_y, b.1 - variance_x);
|
||||
if !is_station_between(new, b, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -98,12 +142,22 @@ pub fn get_extremes_with_variance(
|
||||
// |_b|
|
||||
// |a_|
|
||||
let first = if b.0 as i32 - variance_y as i32 >= 0 && b.1 + variance_x < max_x {
|
||||
Some((b.0 - variance_y, b.1 + variance_x))
|
||||
let new = (b.0 - variance_y, b.1 + variance_x);
|
||||
if !is_station_between(new, b, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let second = if a.0 + variance_y < max_y && a.1 as i32 - variance_x as i32 >= 0 {
|
||||
Some((a.0 + variance_y, a.1 - variance_x))
|
||||
let new = (a.0 + variance_y, a.1 - variance_x);
|
||||
if !is_station_between(new, a, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -114,12 +168,22 @@ pub fn get_extremes_with_variance(
|
||||
// |_b|
|
||||
let first =
|
||||
if a.0 as i32 - variance_y as i32 >= 0 && a.1 as i32 - variance_x as i32 >= 0 {
|
||||
Some((a.0 - variance_y, a.1 - variance_x))
|
||||
let new = (a.0 - variance_y, a.1 - variance_x);
|
||||
if !is_station_between(new, a, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let second = if b.0 + variance_y < max_y && b.1 + variance_x < max_x {
|
||||
Some((b.0 + variance_y, b.1 + variance_x))
|
||||
let new = (b.0 + variance_y, b.1 + variance_x);
|
||||
if !is_station_between(new, b, &map) {
|
||||
Some(new)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -129,6 +193,63 @@ pub fn get_extremes_with_variance(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_station_between(a: (usize, usize), b: (usize, usize), map: &RoofMap) -> bool {
|
||||
let mut count: usize = 1;
|
||||
|
||||
let y_diff = (a.0 as i32 - b.0 as i32).abs() as usize;
|
||||
let x_diff = (a.1 as i32 - b.1 as i32).abs() as usize;
|
||||
|
||||
'check_diagonal: loop {
|
||||
let mut next_x: i32 = 0;
|
||||
let mut next_y: i32 = 0;
|
||||
|
||||
if a.1 > b.1 {
|
||||
if a.0 > b.0 {
|
||||
// |b_|
|
||||
// |_a|
|
||||
next_y = b.0 as i32 + (y_diff * count) as i32;
|
||||
next_x = b.1 as i32 + (x_diff * count) as i32;
|
||||
if next_x >= a.1 as i32 || next_y >= a.0 as i32 {
|
||||
break 'check_diagonal;
|
||||
}
|
||||
} else {
|
||||
// |_a|
|
||||
// |b_|
|
||||
next_y = a.0 as i32 + (y_diff * count) as i32;
|
||||
next_x = a.1 as i32 - (x_diff * count) as i32;
|
||||
if next_x <= b.1 as i32 || next_y >= b.0 as i32 {
|
||||
break 'check_diagonal;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if a.0 > b.0 {
|
||||
// |_b|
|
||||
// |a_|
|
||||
next_y = b.0 as i32 + (x_diff * count) as i32;
|
||||
next_x = b.1 as i32 - (y_diff * count) as i32;
|
||||
if next_x <= a.1 as i32 || next_y >= b.0 as i32 {
|
||||
break 'check_diagonal;
|
||||
}
|
||||
} else {
|
||||
// |a_|
|
||||
// |_b|
|
||||
next_y = a.0 as i32 - (x_diff * count) as i32;
|
||||
next_x = a.1 as i32 + (y_diff * count) as i32;
|
||||
if next_x >= b.1 as i32 || next_y <= b.0 as i32 {
|
||||
break 'check_diagonal;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(_) = map[next_y as usize][next_x as usize] {
|
||||
return true;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
// NOTE: this might be the ugliest piece of code ever
|
||||
// pub fn get_antinode_set_of_position(
|
||||
// x: usize,
|
||||
|
||||
Reference in New Issue
Block a user