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
6 changed files with 180 additions and 20 deletions

No files matched your search

+8 -7
View File
@@ -1,4 +1,4 @@
use crate::{AoC, Day, SimpleDay, WinnowDay};
use crate::{AoC, WinnowDay};
use winnow::{
Parser, Result,
ascii::{digit1, multispace0},
@@ -11,13 +11,12 @@ enum Side {
Right(i64),
}
fn day() -> impl SimpleDay {
fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _)
}
fn parse(input: String) -> Parsed {
let mut input: &str = input.as_str();
repeat(1.., parse_line).parse_next(&mut input).unwrap()
fn parse(input: &mut &str) -> Result<Parsed> {
repeat(1.., parse_line).parse_next(input)
}
fn parse_line(input: &mut &str) -> Result<Side> {
@@ -55,8 +54,8 @@ fn part2(input: Parsed) -> String {
let input = input
.iter()
.flat_map(|side| match side {
Side::Left(dx) => (0..*dx).map(|i| Side::Left(1)).collect::<Vec<Side>>(),
Side::Right(dx) => (0..*dx).map(|i| Side::Right(1)).collect::<Vec<Side>>(),
Side::Left(dx) => (0..*dx).map(|_| Side::Left(1)).collect::<Vec<Side>>(),
Side::Right(dx) => (0..*dx).map(|_| Side::Right(1)).collect::<Vec<Side>>(),
})
.collect::<Vec<Side>>();
let mut res = 0;
@@ -75,6 +74,8 @@ fn part2(input: Parsed) -> String {
#[cfg(test)]
mod tests {
use crate::Day;
use super::*;
#[test]
+7 -8
View File
@@ -1,4 +1,4 @@
use crate::{AoC, Day, SimpleDay, WinnowDay};
use crate::{AoC, WinnowDay};
use winnow::{
Parser, Result,
ascii::digit1,
@@ -7,15 +7,12 @@ use winnow::{
type Parsed = Vec<(u64, u64)>;
fn day() -> impl SimpleDay {
fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _)
}
fn parse(input: String) -> Parsed {
let mut input: &str = input.as_str();
separated(1.., parse_range, ",")
.parse_next(&mut input)
.unwrap()
fn parse(input: &mut &str) -> Result<Parsed> {
separated(1.., parse_range, ",").parse_next(input)
}
fn parse_range(input: &mut &str) -> Result<(u64, u64)> {
@@ -44,7 +41,7 @@ fn part2(input: Parsed) -> String {
.map(|(from, to)| {
(*from..=*to)
.filter(|i| {
let n = (i.ilog10() + 1);
let n = i.ilog10() + 1;
(1..=(n >> 1)).filter(|p| n % p == 0).any(|p| {
(1..(n / p))
.all(|s| i % 10u64.pow(p) == (i / 10u64.pow(p * s)) % 10u64.pow(p))
@@ -58,6 +55,8 @@ fn part2(input: Parsed) -> String {
#[cfg(test)]
mod tests {
use crate::Day;
use super::*;
#[test]
+4 -2
View File
@@ -1,6 +1,6 @@
use std::iter::Sum;
use crate::{AoC, Day, SimpleDay, WinnowDay};
use crate::{AoC, WinnowDay};
use winnow::{
Parser, Result,
ascii::newline,
@@ -13,7 +13,7 @@ fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _)
}
fn parse<'a, 'b>(input: &'a mut &'b str) -> Result<Vec<Vec<usize>>> {
fn parse(input: &mut &str) -> Result<Vec<Vec<usize>>> {
repeat(1.., parse_line).parse_next(input)
}
@@ -62,6 +62,8 @@ fn joltage<
#[cfg(test)]
mod tests {
use crate::Day;
use super::*;
#[test]
+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");
}
}
+3 -2
View File
@@ -1,5 +1,5 @@
// #![allow(dead_code)]
// #![allow(unused)]
#![allow(dead_code)]
#![allow(unused)]
use std::fmt::{Debug, Display};
@@ -8,6 +8,7 @@ use winnow::Parser;
mod day1;
mod day2;
mod day3;
mod day4;
trait Day {
fn assert1(&self, input: &str, output: &str);
+3 -1
View File
@@ -1,6 +1,6 @@
use winnow::{Parser, Result};
use crate::{AoC, Day, SimpleDay, WinnowDay};
use crate::{AoC, SimpleDay, WinnowDay};
fn day() -> impl WinnowDay {
AoC::new(parse as _, part1 as _, part2 as _)
@@ -22,6 +22,8 @@ fn part2(input: Parsed) -> usize {
#[cfg(test)]
mod tests {
use crate::Day;
use super::*;
#[test]