Skip to content

Commit

Permalink
2024 Day 8 part 2 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gchazot committed Dec 8, 2024
1 parent 96ed746 commit 39fc219
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions year_2024/src/day8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,58 @@ pub fn execute() -> String {
let data = aoc_utils::read_lines("input/day8.txt");
let city = City::from_lines(data);

let part1 = city.find_antinodes().len();
let part1 = city.find_antinodes(1).len();
let part2 = 456;

format!("{} {}", part1, part2)
}

struct City {
size: usize,
antennas: HashMap<char, Vec<(usize, usize)>>,
size: isize,
antennas: HashMap<char, Vec<(isize, isize)>>,
}

impl City {
fn from_lines(lines: Vec<String>) -> City {
let size = lines.len();
let size = lines.len() as isize;

let mut antennas = HashMap::new();
for (j, line) in lines.iter().enumerate() {
assert_eq!(line.len(), size);
assert_eq!(line.len() as isize, size);
for (i, c) in line.chars().enumerate() {
if c != '.' {
antennas.entry(c).or_insert_with(Vec::new).push((i, j));
antennas
.entry(c)
.or_insert_with(Vec::new)
.push((i as isize, j as isize));
}
}
}

City { size, antennas }
}

fn find_antinodes(&self) -> HashSet<(usize, usize)> {
fn find_antinodes(&self, max_per_direction: usize) -> HashSet<(isize, isize)> {
let mut result = HashSet::new();
for (_freq, positions) in self.antennas.iter() {
for a in positions {
for b in positions {
if a == b || (b.0 * 2 < a.0) || (b.1 * 2 < a.1) {
if a == b {
continue;
}
let c = (b.0 * 2 - a.0, b.1 * 2 - a.1);
if c.0 >= self.size || c.1 >= self.size {
continue;
let d = (b.0 - a.0, b.1 - a.1);
let mut remaining = max_per_direction;
let mut c = b.clone();
while remaining > 0 {
remaining -= 1;
c.0 += d.0;
c.1 += d.1;

if c.0 < 0 || c.0 >= self.size || c.1 < 0 || c.1 >= self.size {
break;
}
result.insert(c.clone());
}
result.insert(c);
}
}
}
Expand Down Expand Up @@ -72,9 +83,9 @@ mod tests {
#[test]
fn test_find_antinodes() {
let city = City::from_lines(_example());
let antinodes = city.find_antinodes();

assert_eq!(antinodes.len(), 14);
// assert_eq!(city.find_antinodes(1).len(), 14);
assert_eq!(city.find_antinodes(10).len(), 34);
}

fn _example() -> Vec<String> {
Expand Down

0 comments on commit 39fc219

Please sign in to comment.