2025: day04: part 2

This commit is contained in:
Antoine Martin 2025-12-08 16:24:56 +01:00
parent 8f2d42bb73
commit 6cf10a0eb5
2 changed files with 41 additions and 8 deletions

View file

@ -3,11 +3,13 @@ use criterion::{criterion_group, criterion_main, Criterion};
use aoc2025::day01; use aoc2025::day01;
use aoc2025::day02; use aoc2025::day02;
use aoc2025::day03; use aoc2025::day03;
use aoc2025::day04;
fn aoc2025_all(c: &mut Criterion) { fn aoc2025_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
c.bench_function("day02", |b| b.iter(|| day02::run().unwrap())); c.bench_function("day02", |b| b.iter(|| day02::run().unwrap()));
c.bench_function("day03", |b| b.iter(|| day03::run().unwrap())); c.bench_function("day03", |b| b.iter(|| day03::run().unwrap()));
c.bench_function("day04", |b| b.iter(|| day04::run().unwrap()));
} }
criterion_group! { criterion_group! {

View file

@ -7,6 +7,7 @@ const INPUT: &str = include_str!("../input/day04.txt");
pub fn run() -> Result<String> { pub fn run() -> Result<String> {
let mut res = String::with_capacity(128); let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?; writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res) Ok(res)
} }
@ -84,6 +85,30 @@ impl PaperRollsMap {
} }
count <= 4 count <= 4
} }
fn count_removable_rolls(&mut self) -> usize {
let mut count = 0;
loop {
let mut cur_loop_count = 0;
let mut copy = PaperRollsMap(self.0.clone());
for y in 0..self.len() {
for x in 0..self.width() {
if self.0[y][x] && self.roll_is_accessible(x, y) {
copy.0[y][x] = false;
cur_loop_count += 1;
}
}
}
if cur_loop_count == 0 {
break;
}
*self = copy;
count += cur_loop_count;
}
count
}
} }
fn part1(input: &str) -> Result<usize> { fn part1(input: &str) -> Result<usize> {
@ -92,6 +117,12 @@ fn part1(input: &str) -> Result<usize> {
Ok(grid.count_accessible_rolls()) Ok(grid.count_accessible_rolls())
} }
fn part2(input: &str) -> Result<usize> {
let mut grid: PaperRollsMap = input.parse()?;
Ok(grid.count_removable_rolls())
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -108,13 +139,13 @@ mod tests {
assert_eq!(part1(INPUT).unwrap(), 1393); assert_eq!(part1(INPUT).unwrap(), 1393);
} }
//#[test] #[test]
//fn part2_provided() { fn part2_provided() {
// assert_eq!(part2(PROVIDED).unwrap(), 3121910778619); assert_eq!(part2(PROVIDED).unwrap(), 43);
//} }
//#[test] #[test]
//fn part2_real() { fn part2_real() {
// assert_eq!(part2(INPUT).unwrap(), 168798209663590); assert_eq!(part2(INPUT).unwrap(), 8643);
//} }
} }