finished day04

This commit is contained in:
2025-12-09 21:13:37 +01:00
parent b67163521e
commit 161ba78efe
5 changed files with 87 additions and 58 deletions

View File

@@ -1,23 +1,23 @@
# Advent of Code 2025
```bash
day01:A => 1150 ( 52us)
day01:B => 1150 ( 50us)
day02:A => 31839939622 ( 9337us)
day02:B => 41662374059 ( 68756us)
day01:A => 1150 ( 50us)
day01:B => 1150 ( 48us)
day02:A => 31839939622 ( 7730us)
day02:B => 41662374059 ( 64909us)
day03:A => 16812 ( 83us)
day03:B => 166345822896410 ( 292us)
day04:A => 1518 ( 190us)
day04:B => 0 ( 0us)
day03:B => 166345822896410 ( 285us)
day04:A => 1518 ( 194us)
day04:B => 8665 ( 3753us)
day05:A => 0 ( 0us)
day05:B => 0 ( 0us)
day06:A => 6503327062445 ( 201us)
day06:B => 9640641878593 ( 135us)
day06:A => 6503327062445 ( 196us)
day06:B => 9640641878593 ( 134us)
day07:A => 0 ( 0us)
day07:B => 0 ( 0us)
day08:A => 0 ( 0us)
day08:B => 0 ( 0us)
day09:A => 4725826296 ( 160us)
day09:A => 4725826296 ( 165us)
day09:B => 0 ( 0us)
----------
81842us
78131us
```

64
src/day04/common.rs Normal file
View File

@@ -0,0 +1,64 @@
pub fn parse_to_floor(input: String) -> Vec<Vec<u8>> {
let mut rows: Vec<Vec<u8>> = Vec::with_capacity(input.len() + 2);
rows.push(vec![0u8; input.len()]);
rows.append(
&mut input
.lines()
.map(|line| {
let mut col: Vec<u8> = Vec::with_capacity(line.len() + 2);
col.push(0u8);
col.append(
&mut line
.chars()
.map(|c| match c {
'@' => 1u8,
_ => 0u8,
})
.collect(),
);
col.push(0u8);
col
})
.collect(),
);
rows.push(vec![0u8; input.len()]);
rows
}
pub fn remove_if_possible(floor: &mut Vec<Vec<u8>>) -> Option<u64> {
let mut removed: Vec<(usize, usize)> = Vec::new();
floor
.windows(3)
.enumerate()
.for_each(|(r, rows)| {
rows[0]
.iter()
.zip(rows[1].iter())
.zip(rows[2].iter())
.map(|((a, b), c)| (a, b, c))
.collect::<Vec<(&u8, &u8, &u8)>>()
.windows(3)
.enumerate()
.for_each(|(c, cols)| {
if cols[1].1 == &0u8 {
return;
}
if cols.iter().map(|(a, b, c)| *a + *b + *c).sum::<u8>() - 1 < 4 {
removed.push((r+1, c+1));
}
});
});
removed.iter().for_each(|(r, c)| {
floor[*r][*c] = 0;
});
if removed.len() == 0 {
return None;
}
Some(removed.len() as u64)
}

View File

@@ -1,5 +1,6 @@
pub mod part04a;
pub mod part04b;
mod common;
#[cfg(test)]
mod tests {
@@ -22,7 +23,6 @@ mod tests {
}
#[test]
#[ignore]
fn part04b() {
assert_eq!(part04b::solve(PUZZLE.to_string()), 43);
}

View File

@@ -1,48 +1,5 @@
use crate::day04::common::{parse_to_floor, remove_if_possible};
pub fn solve(puzzle: String) -> u64 {
let mut solution: u64 = 0;
let mut rows: Vec<Vec<u8>> = Vec::with_capacity(puzzle.len() + 2);
rows.push(vec![0u8; puzzle.len()]);
rows.append(
&mut puzzle
.lines()
.map(|line| {
let mut col: Vec<u8> = Vec::with_capacity(line.len() + 2);
col.push(0u8);
col.append(
&mut line
.chars()
.map(|c| match c {
'@' => 1u8,
_ => 0u8,
})
.collect(),
);
col.push(0u8);
col
})
.collect(),
);
rows.push(vec![0u8; puzzle.len()]);
rows.windows(3).for_each(|rows| {
rows[0]
.iter()
.zip(rows[1].iter())
.zip(rows[2].iter())
.map(|((a, b), c)| (*a, *b, *c))
.collect::<Vec<(u8, u8, u8)>>()
.windows(3)
.for_each(|cols| {
if cols[1].1 != 1u8 {
return;
}
if cols.iter().map(|(a, b, c)| *a + *b + *c).sum::<u8>() - 1 < 4 {
solution += 1;
}
});
});
solution
remove_if_possible(&mut parse_to_floor(puzzle)).unwrap_or_else(|| 0)
}

View File

@@ -1,6 +1,14 @@
use crate::day04::common::{parse_to_floor, remove_if_possible};
pub fn solve(puzzle: String) -> u64 {
0 // TODO
let mut solution: u64 = 0;
let mut floor = parse_to_floor(puzzle);
while let Some(removed) = remove_if_possible(&mut floor) {
solution += removed;
}
solution
}