day4: part1
This commit is contained in:
BIN
.aoc_tiles/tiles/2025/04.png
Normal file
BIN
.aoc_tiles/tiles/2025/04.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.1 KiB |
@@ -1,6 +1,6 @@
|
|||||||
<!-- AOC TILES BEGIN -->
|
<!-- AOC TILES BEGIN -->
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
2025 - 6 ⭐ - Rust
|
2025 - 7 ⭐ - Rust
|
||||||
</h1>
|
</h1>
|
||||||
<a href="src/day1.rs">
|
<a href="src/day1.rs">
|
||||||
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
|
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
|
||||||
@@ -11,4 +11,7 @@
|
|||||||
<a href="src/day3.rs">
|
<a href="src/day3.rs">
|
||||||
<img src=".aoc_tiles/tiles/2025/03.png" width="161px">
|
<img src=".aoc_tiles/tiles/2025/03.png" width="161px">
|
||||||
</a>
|
</a>
|
||||||
|
<a href="src/day4.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2025/04.png" width="161px">
|
||||||
|
</a>
|
||||||
<!-- AOC TILES END -->
|
<!-- AOC TILES END -->
|
||||||
|
|||||||
47
src/day4.rs
Normal file
47
src/day4.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
use grid::Grid;
|
||||||
|
|
||||||
|
#[aoc_generator(day4)]
|
||||||
|
fn parse(input: &str) -> Grid<u8> {
|
||||||
|
input.parse().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day4, part1)]
|
||||||
|
fn part1(input: &Grid<u8>) -> u64 {
|
||||||
|
(0..input.height() * input.width())
|
||||||
|
.filter(|i| *input.get(&input.coord(*i as i64).unwrap()).unwrap() == b'@')
|
||||||
|
.map(|i| input.neighbours_count(input.coord(i as i64).unwrap(), |c| *c == b'@'))
|
||||||
|
.filter(|n| *n < 4)
|
||||||
|
.count() as u64
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day4, part2)]
|
||||||
|
fn part2(input: &Grid<u8>) -> u64 {
|
||||||
|
input.width() as u64
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const EXAMPLE: &str = "..@@.@@@@.
|
||||||
|
@@@.@.@.@@
|
||||||
|
@@@@@.@.@@
|
||||||
|
@.@@@@..@.
|
||||||
|
@@.@@@@.@@
|
||||||
|
.@@@@@@@.@
|
||||||
|
.@.@.@.@@@
|
||||||
|
@.@@@.@@@@
|
||||||
|
.@@@@@@@@.
|
||||||
|
@.@.@@@.@.";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_example() {
|
||||||
|
assert_eq!(part1(&parse(EXAMPLE)), 13);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_example() {
|
||||||
|
assert_eq!(part2(&parse(EXAMPLE)), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
mod day1;
|
mod day1;
|
||||||
mod day2;
|
mod day2;
|
||||||
mod day3;
|
mod day3;
|
||||||
|
mod day4;
|
||||||
|
|
||||||
use aoc_runner_derive::aoc_lib;
|
use aoc_runner_derive::aoc_lib;
|
||||||
|
|
||||||
|
|||||||
@@ -352,6 +352,28 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the count of neighbours (8 directions) matching predicate p
|
||||||
|
pub fn neighbours_count<C: AsCoord2d, P>(&self, c: C, mut p: P) -> usize
|
||||||
|
where
|
||||||
|
P: FnMut(&T) -> bool,
|
||||||
|
{
|
||||||
|
const DIRECTIONS: [(i64, i64); 8] = [
|
||||||
|
(-1, -1),
|
||||||
|
(0, -1),
|
||||||
|
(1, -1),
|
||||||
|
(-1, 0),
|
||||||
|
(1, 0),
|
||||||
|
(1, 1),
|
||||||
|
(0, 1),
|
||||||
|
(-1, 1),
|
||||||
|
];
|
||||||
|
DIRECTIONS
|
||||||
|
.iter()
|
||||||
|
.map(|d| (c.x() + d.0, c.y() + d.1))
|
||||||
|
.filter(|c| self.get(c).is_some_and(|x| p(x)))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
// fn window_compare_impl<const REV: bool>(&self, needle: &[T]) -> Vec<(i64, i64)> {
|
// fn window_compare_impl<const REV: bool>(&self, needle: &[T]) -> Vec<(i64, i64)> {
|
||||||
// if (self.width as usize) < needle.len() {
|
// if (self.width as usize) < needle.len() {
|
||||||
// return Vec::new();
|
// return Vec::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user