From ca342cef94696829c374a09008fab7ee298a8d1c Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Sat, 14 Dec 2024 12:42:56 -0500 Subject: [PATCH] Fixing bugs with day 8 code --- .vscode/launch.json | 18 ++++++ Cargo.lock | 1 + crates/day_7/src/equation.rs | 2 +- crates/day_8/Cargo.toml | 1 + crates/day_8/src/day_8.rs | 8 +++ crates/day_8/src/map.rs | 105 ++++++++++++++++++++++++++++------- 6 files changed, 115 insertions(+), 20 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9569962..687b924 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -154,6 +154,24 @@ }, "args": [], "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug Day 8 in 'advent_of_code_2024'", + "cargo": { + "args": [ + "build", + "--bin=advent_of_code_2024", + "--package=advent_of_code_2024" + ], + "filter": { + "name": "advent_of_code_2024", + "kind": "bin" + } + }, + "args": ["--", "-s", "6", "7", "-d", "8"], + "cwd": "${workspaceFolder}" } ] } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 05891f7..c0aa38b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,6 +101,7 @@ dependencies = [ name = "day_8" version = "0.1.0" dependencies = [ + "nalgebra-glm", "solver_base", "utils", ] diff --git a/crates/day_7/src/equation.rs b/crates/day_7/src/equation.rs index 660e882..b873a0b 100644 --- a/crates/day_7/src/equation.rs +++ b/crates/day_7/src/equation.rs @@ -206,7 +206,7 @@ impl Equation pub fn oper_sequence_is_valid(self: &Equation, opers: &Vec) -> bool { - let mut values_clone = self.values.clone(); + let values_clone = self.values.clone(); let mut actual_result = self.values[0]; let mut print_test_cat = self.enable_debug_prints; diff --git a/crates/day_8/Cargo.toml b/crates/day_8/Cargo.toml index ce43e96..7c7569c 100644 --- a/crates/day_8/Cargo.toml +++ b/crates/day_8/Cargo.toml @@ -5,5 +5,6 @@ version = "0.1.0" edition = "2021" [dependencies] +nalgebra-glm = "0.19.0" solver_base = { workspace = true } utils = { workspace = true } \ No newline at end of file diff --git a/crates/day_8/src/day_8.rs b/crates/day_8/src/day_8.rs index 7670e39..7614b6d 100644 --- a/crates/day_8/src/day_8.rs +++ b/crates/day_8/src/day_8.rs @@ -45,6 +45,14 @@ impl Day8 fn solve_first_case(self: &mut Self) -> String { + let antinodes = self.map.find_antinodes(); + + if self.do_debug_prints + { + self.map.print(true); + } + + self.final_result = antinodes.len() as i32; self.final_result.to_string() } diff --git a/crates/day_8/src/map.rs b/crates/day_8/src/map.rs index c9b7738..cc67f24 100644 --- a/crates/day_8/src/map.rs +++ b/crates/day_8/src/map.rs @@ -10,25 +10,11 @@ // Each antenna is tuned to a specific frequency indicated by a // single lowercase letter, uppercase letter, or digit. -// an antinode occurs at any point that is perfectly in line with two antennas of the same -// frequency - but only when one of the antennas is twice as far away as the other -// This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them. - -// So, for these two antennas with frequency `a`, they create the two antinodes marked with `#`: - -// .......... -// ...#...... -// .......... -// ....a..... -// .......... -// .....a.... -// .......... -// ......#... -// .......... -// .......... use std::{collections::HashMap, mem}; +use nalgebra_glm::Vec2; + #[derive(Copy, Clone, PartialEq, Debug)] pub enum MapCell @@ -51,6 +37,11 @@ impl PointIdx { PointIdx { i, j } } + + // pub fn as_glm_vec(self: & PointIdx) -> I32Vec2 + // { + // I32Vec2::new(self.i as i32, self.j as i32) + // } } #[derive(Clone, Debug)] @@ -142,7 +133,14 @@ impl Map pub fn print(self: &Map, include_antinodes: bool) { - print!("MAP:"); + if include_antinodes + { + print!("MAP (w/ antinodes):") + } + else + { + print!("MAP:"); + } for (i, row) in self.map.iter().enumerate() { print!("\n\t"); @@ -170,13 +168,82 @@ impl Map println!("{:#?}", self.antenna_locations); } + // an antinode occurs at any point that is perfectly in line with two antennas of the same + // frequency - but only when one of the antennas is twice as far away as the other + // This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them. + + // So, for these two antennas with frequency `a`, they create the two antinodes marked with `#`: + + // .......... + // ...#...... + // .......... + // ....a..... + // .......... + // .....a.... + // .......... + // ......#... + // .......... + // .......... + pub fn find_antinodes(self: &mut Map) -> Vec { - for ant in &self.antenna_locations + let mut antinodes: Vec = vec![]; + for (_freq, locations) in &self.antenna_locations { + let mut i = 0; + while i < locations.len() + { + let mut j = 0; + while j < locations.len() + { + if i == j + { + j += 1; + continue; + } + + let x1 = locations[i].i as f32; + let y1 = locations[i].j as f32; + let x2 = locations[j].i as f32; + let y2 = locations[j].j as f32; + let dist = ((x2 - x1).powf(2.0) + (y2 - y1).powf(2.0)).sqrt() * 2.0; + + let first_glm = Vec2::new(locations[i].i as f32, locations[i].j as f32); + let second_glm = Vec2::new(locations[j].i as f32, locations[j].j as f32); + let direction = (second_glm - first_glm).normalize(); + + + let new_i = (locations[i].i as f32 + direction.x * dist); + let new_j = (locations[i].j as f32 + direction.y * dist); + + if new_i < 0.0 || new_j < 0.0 + { + j += 1; + continue; + } + if new_i >= self.map.len() as f32 || + new_j >= self.map[0].len() as f32 + { + j += 1; + continue; + } + + let antinode_location = PointIdx::new(new_i as usize, new_j as usize); + + if !antinodes.contains(&antinode_location) + { + antinodes.push(antinode_location); + } + + j += 1; + } + + i += 1; + } } - vec![] + self.antinodes = antinodes; + self.antinodes.clone() } } \ No newline at end of file