Add day 04 part 1
This commit is contained in:
parent
879272687b
commit
182cea15a7
3 changed files with 68 additions and 0 deletions
66
AoC_2025/src/days/day04.rs
Normal file
66
AoC_2025/src/days/day04.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
use crate::days::Solution;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
|
pub struct Day04;
|
||||||
|
|
||||||
|
impl Solution for Day04 {
|
||||||
|
type Input = Vec<Vec<bool>>;
|
||||||
|
|
||||||
|
fn parse(&self, data: &str) -> Self::Input {
|
||||||
|
data.split("\n")
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.chars().map(|c| c == '@').collect())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(&self, input: &Self::Input) -> usize {
|
||||||
|
input
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(x, l)| {
|
||||||
|
l.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(move |(y, v)| {
|
||||||
|
**v && neighbors((x, *y), input.len())
|
||||||
|
.iter()
|
||||||
|
.filter(|(nx, ny)| input[*nx][*ny])
|
||||||
|
.count()
|
||||||
|
< 4
|
||||||
|
})
|
||||||
|
.count()
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(&self, input: &Self::Input) -> usize {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn coord_in_range((x, y): (i32, i32), size: usize) -> (usize, usize) {
|
||||||
|
(
|
||||||
|
min(max(x, 0) as usize, size - 1),
|
||||||
|
min(max(y, 0) as usize, size - 1),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbors((x, y): (usize, usize), size: usize) -> Vec<(usize, usize)> {
|
||||||
|
(0..=2)
|
||||||
|
.map(|i| i as i32 - 1)
|
||||||
|
.flat_map(|dx| {
|
||||||
|
(0..=2)
|
||||||
|
.map(|i| i as i32 - 1)
|
||||||
|
.filter_map(|dy| {
|
||||||
|
if dx != 0 || dy != 0 {
|
||||||
|
Some(coord_in_range((x as i32 + dx, y as i32 + dy), size))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(|n| *n != (x, y))
|
||||||
|
.collect::<Vec<(usize, usize)>>()
|
||||||
|
})
|
||||||
|
.collect::<std::collections::HashSet<(usize, usize)>>()
|
||||||
|
.into_iter()
|
||||||
|
.collect::<Vec<(usize, usize)>>()
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
pub mod day01;
|
pub mod day01;
|
||||||
pub mod day02;
|
pub mod day02;
|
||||||
pub mod day03;
|
pub mod day03;
|
||||||
|
pub mod day04;
|
||||||
|
|
||||||
pub trait Solution {
|
pub trait Solution {
|
||||||
type Input;
|
type Input;
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ fn main() {
|
||||||
1 => run(cli.day, days::day01::Day01),
|
1 => run(cli.day, days::day01::Day01),
|
||||||
2 => run(cli.day, days::day02::Day02),
|
2 => run(cli.day, days::day02::Day02),
|
||||||
3 => run(cli.day, days::day03::Day03),
|
3 => run(cli.day, days::day03::Day03),
|
||||||
|
4 => run(cli.day, days::day04::Day04),
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Day {:02} is not implemented yet!", cli.day);
|
eprintln!("Day {:02} is not implemented yet!", cli.day);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue