Compare commits

..
3 Commits
Author SHA1 Message Date
Jan-Bulthuis 0ff3b02db9 feat: Day 4 2025-12-04 09:20:37 +01:00
Jan-Bulthuis e3affe980a chore: Switched to WinnowDay 2025-12-03 23:16:00 +01:00
Jan-Bulthuis d08eabf619 fix: Allow dead code again but disallow unused 2025-12-03 23:10:28 +01:00
5 changed files with 166 additions and 13 deletions

No files matched your search

+4 -5
View File
@@ -1,4 +1,4 @@
use crate::{AoC, SimpleDay}; use crate::{AoC, WinnowDay};
use winnow::{ use winnow::{
Parser, Result, Parser, Result,
ascii::{digit1, multispace0}, ascii::{digit1, multispace0},
@@ -11,13 +11,12 @@ enum Side {
Right(i64), Right(i64),
} }
fn day() -> impl SimpleDay { fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _) AoC::new(parse as _, part1 as _, part2 as _)
} }
fn parse(input: String) -> Parsed { fn parse(input: &mut &str) -> Result<Parsed> {
let mut input: &str = input.as_str(); repeat(1.., parse_line).parse_next(input)
repeat(1.., parse_line).parse_next(&mut input).unwrap()
} }
fn parse_line(input: &mut &str) -> Result<Side> { fn parse_line(input: &mut &str) -> Result<Side> {
+4 -7
View File
@@ -1,4 +1,4 @@
use crate::{AoC, SimpleDay}; use crate::{AoC, WinnowDay};
use winnow::{ use winnow::{
Parser, Result, Parser, Result,
ascii::digit1, ascii::digit1,
@@ -7,15 +7,12 @@ use winnow::{
type Parsed = Vec<(u64, u64)>; type Parsed = Vec<(u64, u64)>;
fn day() -> impl SimpleDay { fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _) AoC::new(parse as _, part1 as _, part2 as _)
} }
fn parse(input: String) -> Parsed { fn parse(input: &mut &str) -> Result<Parsed> {
let mut input: &str = input.as_str(); separated(1.., parse_range, ",").parse_next(input)
separated(1.., parse_range, ",")
.parse_next(&mut input)
.unwrap()
} }
fn parse_range(input: &mut &str) -> Result<(u64, u64)> { fn parse_range(input: &mut &str) -> Result<(u64, u64)> {
+155
View File
@@ -0,0 +1,155 @@
use winnow::{
Parser, Result,
ascii::{multispace0, newline},
combinator::{alt, preceded, repeat, terminated},
};
use crate::{AoC, SimpleDay, WinnowDay};
fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _)
}
type Parsed = Vec<Vec<Cell>>;
#[derive(Debug, PartialEq, Eq)]
enum Cell {
Empty,
Paper,
}
impl Cell {
fn is_empty(&self) -> bool {
self == &Cell::Empty
}
fn is_paper(&self) -> bool {
self == &Cell::Paper
}
}
fn parse(input: &mut &str) -> Result<Parsed> {
repeat(1.., preceded(multispace0, parse_line)).parse_next(input)
}
fn parse_line(input: &mut &str) -> Result<Vec<Cell>> {
repeat(1.., parse_cell).parse_next(input)
}
fn parse_cell(input: &mut &str) -> Result<Cell> {
alt((".".map(|_| Cell::Empty), "@".map(|_| Cell::Paper))).parse_next(input)
}
fn part1(input: Parsed) -> usize {
let mut input = input;
remove_paper(&mut input)
}
fn remove_paper(input: &mut [Vec<Cell>]) -> usize {
let mut score = 0;
let mut access = Vec::new();
let h = input.len();
let w = input[0].len();
let directions = [
(-1, -1),
(0, -1),
(1, -1),
(1, 0),
(1, 1),
(0, 1),
(-1, 1),
(-1, 0),
];
for y in 0..h {
for x in 0..w {
match input[y][x] {
Cell::Empty => {}
Cell::Paper => {
let count = directions
.iter()
.filter(|dir| {
let nx = x as isize + dir.0;
let ny = y as isize + dir.1;
nx >= 0
&& nx < w as isize
&& ny >= 0
&& ny < h as isize
&& input[ny as usize][nx as usize].is_paper()
})
.count();
if count < 4 {
score += 1;
access.push((x, y));
}
}
}
}
}
access.iter().for_each(|(x, y)| {
input[*y][*x] = Cell::Empty;
});
score
}
fn part2(input: Parsed) -> usize {
let mut input = input;
let mut score = 0;
loop {
let removed = remove_paper(&mut input);
score += removed;
if removed == 0 {
return score;
}
}
}
#[cfg(test)]
mod tests {
use crate::Day;
use super::*;
#[test]
fn part1_test() {
day().assert1(
"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.",
"13",
);
}
#[test]
fn part1_solve() {
day().assert1(include_str!("../input/day4.txt"), "1356");
}
#[test]
fn part2_test() {
day().assert2(
"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.",
"43",
);
}
#[test]
fn part2_solve() {
day().assert2(include_str!("../input/day4.txt"), "8713");
}
}
+2
View File
@@ -1,4 +1,5 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused)]
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
@@ -7,6 +8,7 @@ use winnow::Parser;
mod day1; mod day1;
mod day2; mod day2;
mod day3; mod day3;
mod day4;
trait Day { trait Day {
fn assert1(&self, input: &str, output: &str); fn assert1(&self, input: &str, output: &str);
+1 -1
View File
@@ -1,6 +1,6 @@
use winnow::{Parser, Result}; use winnow::{Parser, Result};
use crate::{AoC, Day, SimpleDay, WinnowDay}; use crate::{AoC, SimpleDay, WinnowDay};
fn day() -> impl WinnowDay { fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _) AoC::new(parse as _, part1 as _, part2 as _)