day_8: part 1 done

This commit is contained in:
2024-12-11 23:27:26 +01:00
parent 074f4ea870
commit 3a2adcaac5
4 changed files with 195 additions and 31 deletions

View File

@@ -1,12 +1,50 @@
............ .....................................O..V.........
........0... ..................................................
.....0...... ................................O.........Z.......
.......0.... ....W....................................V....v...
....0....... ........................m................8........
......A..... .....................................n........Z..v
............ .............F.....3...n....5m....................
............ ................................................V.
........A... ................3............iv....Z.............V
.........A.. ...........................O..n..i........p......H
............ ......W..6..............................i.........
............ ......................................b...........
..................................n........p......
........M.......c...........m..5......1...........
...M............................L..5..A...........
...w...........9.............F5..................q
.W.....................................q....p.....
.......W........r.......H.....LA......q...........
................4.F....................A..........
........3.......a.....F...................A..L....
....ME...............................Q..........q.
.E..................ih...................Z........
................E...H...........h.................
.........m.........X..............................
..................0......C.................h......
.M......l.................Q.h.....................
..........C..............0........................
.............lX............3.c....................
......8.X.........c....r..a......H.....9..........
.................QE.....C.........................
..R................a........Q...................7.
...........................a......................
l..........X.R............1..I..........9.........
.................0R..............b.....z......x...
.......l.....w....r..........................b....
.8..........0...................P1z...............
.............c.........................L..........
.................C..N............o............9...
...........e..f..N................................
8.............................B...................
...........4...............................x......
....w....RY..........4.......................P....
.........yw.....Y.............o2...............7..
..6y........4..............fo..............7......
.........Y..6............o......................x.
.....Y....e.....y..I.r...........2................
....e.............................P.......z.bB....
.............6.................B........7......x..
..y.N........f...........1....I....z....B.........
.....e....f.............I.................2.......

View File

@@ -62,6 +62,11 @@ pub fn historian_hysteria() {
// println!("The total calibration result is {}", calibration_result); // println!("The total calibration result is {}", calibration_result);
// Sunday 08 // Sunday 08
let antinode_count = sun_08::resonant_collinearity("./assets/day_8_antena_map_input"); let (antinode_count, antinode_count_any) =
sun_08::resonant_collinearity("./assets/day_8_antena_map_input");
println!("The total antinode positions is {}", antinode_count); println!("The total antinode positions is {}", antinode_count);
println!(
"The total antinode positions in any grid is {}",
antinode_count_any
);
} }

View File

View File

@@ -4,10 +4,39 @@ use utils::read_antena_map_input;
use super::*; 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); 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( 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 antena in antena_list {
for complementary_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_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 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( let (a, b) = get_extremes_with_variance(
(antena.0, antena.1), (antena.0, antena.1),
(complementary_antena.0, complementary_antena.1), (complementary_antena.0, complementary_antena.1),
@@ -34,6 +58,7 @@ pub fn get_antinode_set_of_aligned_position(
distance_x, distance_x,
map.len(), map.len(),
map[0].len(), map[0].len(),
map,
); );
if let Some(pair) = a { if let Some(pair) = a {
@@ -46,8 +71,6 @@ pub fn get_antinode_set_of_aligned_position(
} }
} }
println!("Aninode list: {:?}", antinode_list);
antinode_list antinode_list
} }
@@ -59,6 +82,7 @@ pub fn get_extremes_with_variance(
variance_x: usize, variance_x: usize,
max_y: usize, max_y: usize,
max_x: usize, max_x: usize,
map: &RoofMap,
) -> (Option<(usize, usize)>, Option<(usize, usize)>) { ) -> (Option<(usize, usize)>, Option<(usize, usize)>) {
if a.1 > b.1 { if a.1 > b.1 {
if a.0 > b.0 { if a.0 > b.0 {
@@ -66,12 +90,22 @@ pub fn get_extremes_with_variance(
// |_a| // |_a|
let first = let first =
if b.0 as i32 - variance_y as i32 >= 0 && b.1 as i32 - variance_x as i32 >= 0 { 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 { } else {
None None
}; };
let second = if a.0 + variance_y < max_y && a.1 + variance_x < max_x { 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 { } else {
None None
}; };
@@ -81,12 +115,22 @@ pub fn get_extremes_with_variance(
// |_a| // |_a|
// |b_| // |b_|
let first = if a.0 as i32 - variance_y as i32 >= 0 && a.1 + variance_x < max_x { 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 { } else {
None None
}; };
let second = if b.0 + variance_y < max_y && b.1 as i32 - variance_x as i32 >= 0 { 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 { } else {
None None
}; };
@@ -98,12 +142,22 @@ pub fn get_extremes_with_variance(
// |_b| // |_b|
// |a_| // |a_|
let first = if b.0 as i32 - variance_y as i32 >= 0 && b.1 + variance_x < max_x { 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 { } else {
None None
}; };
let second = if a.0 + variance_y < max_y && a.1 as i32 - variance_x as i32 >= 0 { 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 { } else {
None None
}; };
@@ -114,12 +168,22 @@ pub fn get_extremes_with_variance(
// |_b| // |_b|
let first = let first =
if a.0 as i32 - variance_y as i32 >= 0 && a.1 as i32 - variance_x as i32 >= 0 { 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 { } else {
None None
}; };
let second = if b.0 + variance_y < max_y && b.1 + variance_x < max_x { 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 { } else {
None 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 // NOTE: this might be the ugliest piece of code ever
// pub fn get_antinode_set_of_position( // pub fn get_antinode_set_of_position(
// x: usize, // x: usize,