Skip to content

Commit

Permalink
feat: day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefKuchar committed Dec 8, 2024
1 parent e652abf commit c6539e0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 5](./src/bin/05.rs) | `1.8ms` | `48.4ms` |
| [Day 6](./src/bin/06.rs) | `1.1ms` | `642.9ms` |
| [Day 7](./src/bin/07.rs) | `4.8ms` | `254.2ms` |
| [Day 8](./src/bin/08.rs) | `89.8µs` | `253.6µs` |

**Total: 965.96ms**
**Total: 966.31ms**
<!--- benchmarking table --->

---
Expand Down
12 changes: 12 additions & 0 deletions data/examples/08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
74 changes: 74 additions & 0 deletions src/bin/08.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::{collections::HashSet, ops::RangeBounds};

advent_of_code::solution!(8);

fn in_bounds(map: &Vec<Vec<char>>, pos: (i32, i32)) -> bool {
pos.0 >= 0 && pos.1 >= 0 && pos.0 < map.len() as i32 && pos.1 < map[0].len() as i32
}

pub fn solve<R: RangeBounds<i32> + IntoIterator<Item = i32> + Clone>(
input: &str,
range: R,
) -> Option<u32> {
let map: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect();
let mut antennas: Vec<((usize, usize), char)> = Vec::new();
for i in 0..map.len() {
for j in 0..map[i].len() {
if map[i][j] != '.' {
antennas.push(((i, j), map[i][j]));
}
}
}
let mut nodes: HashSet<(usize, usize)> = HashSet::new();
antennas.iter().for_each(|a| {
antennas.iter().filter(|b| a.1 == b.1).for_each(|b| {
if a.0 != b.0 {
let (y1, x1) = a.0;
let (y2, x2) = b.0;
let (dx, dy) = (x2 as i32 - x1 as i32, y2 as i32 - y1 as i32);
for k in range.clone() {
let (ny1, nx1) = (y1 as i32 - dy * k, x1 as i32 - dx * k);
if in_bounds(&map, (ny1, nx1)) {
nodes.insert((ny1 as usize, nx1 as usize));
} else {
break;
}
}
for k in range.clone() {
let (ny2, nx2) = (y2 as i32 + dy * k, x2 as i32 + dx * k);
if in_bounds(&map, (ny2, nx2)) {
nodes.insert((ny2 as usize, nx2 as usize));
} else {
break;
}
}
}
});
});
Some(nodes.len() as u32)
}

pub fn part_one(input: &str) -> Option<u32> {
solve(input, 1..=1)
}

pub fn part_two(input: &str) -> Option<u32> {
solve(input, 0..)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(14));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(34));
}
}

0 comments on commit c6539e0

Please sign in to comment.