Compare commits

..
1 Commits
Author SHA1 Message Date
Jan-Bulthuis 9410fae22e fix: Allow dead code again but disallow unused 2025-12-03 23:08:08 +01:00
5 changed files with 13 additions and 166 deletions

No files matched your search

+5 -4
View File
@@ -1,4 +1,4 @@
use crate::{AoC, WinnowDay}; use crate::{AoC, SimpleDay};
use winnow::{ use winnow::{
Parser, Result, Parser, Result,
ascii::{digit1, multispace0}, ascii::{digit1, multispace0},
@@ -11,12 +11,13 @@ enum Side {
Right(i64), Right(i64),
} }
fn day() -> impl WinnowDay { fn day() -> impl SimpleDay {
AoC::new(parse as _, part1 as _, part2 as _) AoC::new(parse as _, part1 as _, part2 as _)
} }
fn parse(input: &mut &str) -> Result<Parsed> { fn parse(input: String) -> Parsed {
repeat(1.., parse_line).parse_next(input) let mut input: &str = input.as_str();
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> {
+7 -4
View File
@@ -1,4 +1,4 @@
use crate::{AoC, WinnowDay}; use crate::{AoC, SimpleDay};
use winnow::{ use winnow::{
Parser, Result, Parser, Result,
ascii::digit1, ascii::digit1,
@@ -7,12 +7,15 @@ use winnow::{
type Parsed = Vec<(u64, u64)>; type Parsed = Vec<(u64, u64)>;
fn day() -> impl WinnowDay { fn day() -> impl SimpleDay {
AoC::new(parse as _, part1 as _, part2 as _) AoC::new(parse as _, part1 as _, part2 as _)
} }
fn parse(input: &mut &str) -> Result<Parsed> { fn parse(input: String) -> Parsed {
separated(1.., parse_range, ",").parse_next(input) let mut input: &str = input.as_str();
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
@@ -1,155 +0,0 @@
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,5 +1,4 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused)]
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
@@ -8,7 +7,6 @@ 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, SimpleDay, WinnowDay}; use crate::{AoC, Day, 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 _)