Compare commits
8
Commits
2a340c2e04
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ff3b02db9 | ||
|
|
e3affe980a | ||
|
|
d08eabf619 | ||
|
|
ff3fe411ae | ||
|
|
22fc3511b7 | ||
|
|
3740d93f81 | ||
|
|
91d9d7b1dc | ||
|
|
f5788b2825 |
No files matched your search
+2
-1
@@ -1,3 +1,4 @@
|
|||||||
*/target/
|
*/target/
|
||||||
*/input/
|
*/input/
|
||||||
*.out
|
*.out
|
||||||
|
.direnv
|
||||||
Generated
+149
@@ -0,0 +1,149 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "allocator-api2"
|
||||||
|
version = "0.2.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc_2025"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"hashbrown",
|
||||||
|
"num",
|
||||||
|
"num-modular",
|
||||||
|
"topological-sort",
|
||||||
|
"winnow",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "equivalent"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foldhash"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.16.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
|
||||||
|
dependencies = [
|
||||||
|
"allocator-api2",
|
||||||
|
"equivalent",
|
||||||
|
"foldhash",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
|
||||||
|
dependencies = [
|
||||||
|
"num-bigint",
|
||||||
|
"num-complex",
|
||||||
|
"num-integer",
|
||||||
|
"num-iter",
|
||||||
|
"num-rational",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-bigint"
|
||||||
|
version = "0.4.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||||
|
dependencies = [
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-complex"
|
||||||
|
version = "0.4.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-integer"
|
||||||
|
version = "0.1.46"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-iter"
|
||||||
|
version = "0.1.45"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-modular"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "17bb261bf36fa7d83f4c294f834e91256769097b3cb505d44831e0a179ac647f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-rational"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
|
||||||
|
dependencies = [
|
||||||
|
"num-bigint",
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "topological-sort"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ea68304e134ecd095ac6c3574494fc62b909f416c4fca77e440530221e549d3d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "0.7.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc_2025"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
hashbrown = "0.16.1"
|
||||||
|
num = "0.4.3"
|
||||||
|
num-modular = "0.6.1"
|
||||||
|
topological-sort = "0.2.2"
|
||||||
|
winnow = "0.7.14"
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
```bash
|
||||||
|
bacon test -- dayN
|
||||||
|
```
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
use crate::{AoC, WinnowDay};
|
||||||
|
use winnow::{
|
||||||
|
Parser, Result,
|
||||||
|
ascii::{digit1, multispace0},
|
||||||
|
combinator::{alt, preceded, repeat},
|
||||||
|
};
|
||||||
|
|
||||||
|
type Parsed = Vec<Side>;
|
||||||
|
enum Side {
|
||||||
|
Left(i64),
|
||||||
|
Right(i64),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn day() -> impl WinnowDay {
|
||||||
|
AoC::new(parse as _, part1 as _, part2 as _)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &mut &str) -> Result<Parsed> {
|
||||||
|
repeat(1.., parse_line).parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_line(input: &mut &str) -> Result<Side> {
|
||||||
|
preceded(multispace0, alt((parse_left, parse_right))).parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_left(input: &mut &str) -> Result<Side> {
|
||||||
|
preceded("L", digit1.parse_to())
|
||||||
|
.map(Side::Left)
|
||||||
|
.parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_right(input: &mut &str) -> Result<Side> {
|
||||||
|
preceded("R", digit1.parse_to())
|
||||||
|
.map(Side::Right)
|
||||||
|
.parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: Parsed) -> String {
|
||||||
|
let mut res = 0;
|
||||||
|
let mut pos = 50i64;
|
||||||
|
for change in input {
|
||||||
|
match change {
|
||||||
|
Side::Left(dx) => pos = (pos + 100 - dx) % 100,
|
||||||
|
Side::Right(dx) => pos = (pos + dx) % 100,
|
||||||
|
}
|
||||||
|
if pos == 0 {
|
||||||
|
res += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
format!("{res}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: Parsed) -> String {
|
||||||
|
let input = input
|
||||||
|
.iter()
|
||||||
|
.flat_map(|side| match 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;
|
||||||
|
let mut pos = 50i64;
|
||||||
|
for change in input {
|
||||||
|
match change {
|
||||||
|
Side::Left(dx) => pos = (pos + 100 - dx) % 100,
|
||||||
|
Side::Right(dx) => pos = (pos + dx) % 100,
|
||||||
|
}
|
||||||
|
if pos == 0 {
|
||||||
|
res += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
format!("{res}")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::Day;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_test() {
|
||||||
|
day().assert1(
|
||||||
|
"L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82",
|
||||||
|
"3",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_solve() {
|
||||||
|
day().assert1(include_str!("../input/day1.txt"), "982");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_test() {
|
||||||
|
day().assert2(
|
||||||
|
"L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82",
|
||||||
|
"6",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_solve() {
|
||||||
|
day().assert2(include_str!("../input/day1.txt"), "6106");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
use crate::{AoC, WinnowDay};
|
||||||
|
use winnow::{
|
||||||
|
Parser, Result,
|
||||||
|
ascii::digit1,
|
||||||
|
combinator::{separated, separated_pair},
|
||||||
|
};
|
||||||
|
|
||||||
|
type Parsed = Vec<(u64, u64)>;
|
||||||
|
|
||||||
|
fn day() -> impl WinnowDay {
|
||||||
|
AoC::new(parse as _, part1 as _, part2 as _)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &mut &str) -> Result<Parsed> {
|
||||||
|
separated(1.., parse_range, ",").parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_range(input: &mut &str) -> Result<(u64, u64)> {
|
||||||
|
separated_pair(digit1.parse_to(), "-", digit1.parse_to()).parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: Parsed) -> String {
|
||||||
|
let res = input
|
||||||
|
.iter()
|
||||||
|
.map(|(from, to)| {
|
||||||
|
(*from..=*to)
|
||||||
|
.filter(|i| {
|
||||||
|
let n = (i.ilog10() + 1) >> 1;
|
||||||
|
let p = 10u64.pow(n);
|
||||||
|
i / p == i % p
|
||||||
|
})
|
||||||
|
.sum::<u64>()
|
||||||
|
})
|
||||||
|
.sum::<u64>();
|
||||||
|
format!("{res}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: Parsed) -> String {
|
||||||
|
let res = input
|
||||||
|
.iter()
|
||||||
|
.map(|(from, to)| {
|
||||||
|
(*from..=*to)
|
||||||
|
.filter(|i| {
|
||||||
|
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))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.sum::<u64>()
|
||||||
|
})
|
||||||
|
.sum::<u64>();
|
||||||
|
format!("{res}")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::Day;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_test() {
|
||||||
|
day().assert1(
|
||||||
|
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124",
|
||||||
|
"1227775554",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_solve() {
|
||||||
|
day().assert1(include_str!("../input/day2.txt"), "12599655151");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_test() {
|
||||||
|
day().assert2(
|
||||||
|
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124",
|
||||||
|
"4174379265",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_solve() {
|
||||||
|
day().assert2(include_str!("../input/day2.txt"), "20942028255");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
use std::iter::Sum;
|
||||||
|
|
||||||
|
use crate::{AoC, WinnowDay};
|
||||||
|
use winnow::{
|
||||||
|
Parser, Result,
|
||||||
|
ascii::newline,
|
||||||
|
combinator::{repeat, terminated},
|
||||||
|
error::ContextError,
|
||||||
|
token::one_of,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn day() -> impl WinnowDay {
|
||||||
|
AoC::new(parse as _, part1 as _, part2 as _)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &mut &str) -> Result<Vec<Vec<usize>>> {
|
||||||
|
repeat(1.., parse_line).parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_line(input: &mut &str) -> Result<Vec<usize>, ContextError> {
|
||||||
|
terminated(
|
||||||
|
repeat(
|
||||||
|
1..,
|
||||||
|
one_of(('0'..='9',)).map(|c: char| c.to_string().parse::<usize>().unwrap()),
|
||||||
|
),
|
||||||
|
newline,
|
||||||
|
)
|
||||||
|
.parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: Vec<Vec<usize>>) -> usize {
|
||||||
|
input.iter().map(joltage(2)).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: Vec<Vec<usize>>) -> usize {
|
||||||
|
input.iter().map(joltage(12)).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn joltage<
|
||||||
|
'a,
|
||||||
|
B: IntoIterator<IntoIter = I> + Copy,
|
||||||
|
I: ExactSizeIterator<Item = &'a N>,
|
||||||
|
N: Sum<&'a N> + Into<usize> + From<usize> + Copy + Default + 'a,
|
||||||
|
>(
|
||||||
|
size: usize,
|
||||||
|
) -> impl Fn(B) -> N {
|
||||||
|
move |line| {
|
||||||
|
(line.into_iter().len() - size..line.into_iter().len())
|
||||||
|
.fold((0, 0), |(start, val), end| {
|
||||||
|
line.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.skip(start)
|
||||||
|
.take(end - start + 1)
|
||||||
|
.map(|(i, v)| (i + 1, (*v).into() + val * 10))
|
||||||
|
.reduce(|a, b| if a.1 >= b.1 { a } else { b })
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.1
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::Day;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_test() {
|
||||||
|
day().assert1(
|
||||||
|
"987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
|
",
|
||||||
|
"357",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_solve() {
|
||||||
|
day().assert1(include_str!("../input/day3.txt"), "17343");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_test() {
|
||||||
|
day().assert2(
|
||||||
|
"987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111
|
||||||
|
",
|
||||||
|
"3121910778619",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_solve() {
|
||||||
|
day().assert2(include_str!("../input/day3.txt"), "172664333119298");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use std::fmt::{Debug, Display};
|
||||||
|
|
||||||
|
use winnow::Parser;
|
||||||
|
|
||||||
|
mod day1;
|
||||||
|
mod day2;
|
||||||
|
mod day3;
|
||||||
|
mod day4;
|
||||||
|
|
||||||
|
trait Day {
|
||||||
|
fn assert1(&self, input: &str, output: &str);
|
||||||
|
fn assert2(&self, input: &str, output: &str);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AoC<P, O1, O2> {
|
||||||
|
parse: P,
|
||||||
|
part1: O1,
|
||||||
|
part2: O2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P, O1, O2> AoC<P, O1, O2> {
|
||||||
|
fn new(parse: P, part1: O1, part2: O2) -> Self {
|
||||||
|
Self {
|
||||||
|
parse,
|
||||||
|
part1,
|
||||||
|
part2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait SimpleDay: Day {}
|
||||||
|
|
||||||
|
impl<P, O1, O2> SimpleDay for AoC<fn(String) -> P, fn(P) -> O1, fn(P) -> O2>
|
||||||
|
where
|
||||||
|
O1: Display,
|
||||||
|
O2: Display,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P, O1, O2> Day for AoC<fn(String) -> P, fn(P) -> O1, fn(P) -> O2>
|
||||||
|
where
|
||||||
|
O1: Display,
|
||||||
|
O2: Display,
|
||||||
|
{
|
||||||
|
fn assert1(&self, input: &str, output: &str) {
|
||||||
|
let parsed = (self.parse)(input.to_string());
|
||||||
|
assert_eq!(format!("{}", (self.part1)(parsed)), output);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert2(&self, input: &str, output: &str) {
|
||||||
|
let parsed = (self.parse)(input.to_string());
|
||||||
|
assert_eq!(format!("{}", (self.part2)(parsed)), output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait WinnowDay: Day {}
|
||||||
|
|
||||||
|
impl<O, E, O1, O2> WinnowDay
|
||||||
|
for AoC<for<'a, 'b> fn(&'a mut &'b str) -> Result<O, E>, fn(O) -> O1, fn(O) -> O2>
|
||||||
|
where
|
||||||
|
E: Debug,
|
||||||
|
O1: Display,
|
||||||
|
O2: Display,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_parser<O, E>(
|
||||||
|
func: for<'a, 'b> fn(&'a mut &'b str) -> Result<O, E>,
|
||||||
|
) -> impl for<'a> Parser<&'a str, O, E>
|
||||||
|
where
|
||||||
|
E: Debug,
|
||||||
|
{
|
||||||
|
func
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<O, E, O1, O2> Day
|
||||||
|
for AoC<for<'a, 'b> fn(&'a mut &'b str) -> Result<O, E>, fn(O) -> O1, fn(O) -> O2>
|
||||||
|
where
|
||||||
|
E: Debug,
|
||||||
|
O1: Display,
|
||||||
|
O2: Display,
|
||||||
|
{
|
||||||
|
fn assert1(&self, input: &str, output: &str) {
|
||||||
|
let mut input = input;
|
||||||
|
let mut parser = as_parser(self.parse);
|
||||||
|
let parsed = parser.parse_next(&mut input).unwrap();
|
||||||
|
assert_eq!(format!("{}", (self.part1)(parsed)), output);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert2(&self, input: &str, output: &str) {
|
||||||
|
let mut input = input;
|
||||||
|
let mut parser = as_parser(self.parse);
|
||||||
|
let parsed = parser.parse_next(&mut input).unwrap();
|
||||||
|
assert_eq!(format!("{}", (self.part2)(parsed)), output);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
use winnow::{Parser, Result};
|
||||||
|
|
||||||
|
use crate::{AoC, SimpleDay, WinnowDay};
|
||||||
|
|
||||||
|
fn day() -> impl WinnowDay {
|
||||||
|
AoC::new(parse as _, part1 as _, part2 as _)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Parsed = ();
|
||||||
|
|
||||||
|
fn parse(input: &mut &str) -> Result<Parsed> {
|
||||||
|
"".map(|_| ()).parse_next(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: Parsed) -> usize {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: Parsed) -> usize {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::Day;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_test() {
|
||||||
|
day().assert1("", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_solve() {
|
||||||
|
day().assert1(include_str!("../input/day.txt"), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn part2_test() {
|
||||||
|
// day().assert2("", "");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
// fn part2_solve() {
|
||||||
|
// day().assert2(include_str!("../input/day.txt"), "");
|
||||||
|
// }
|
||||||
|
}
|
||||||
Generated
+61
@@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1731533236,
|
||||||
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1764242076,
|
||||||
|
"narHash": "sha256-sKoIWfnijJ0+9e4wRvIgm/HgE27bzwQxcEmo2J/gNpI=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "2fad6eac6077f03fe109c4d4eb171cf96791faa4",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs =
|
||||||
|
{
|
||||||
|
nixpkgs,
|
||||||
|
flake-utils,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
flake-utils.lib.eachDefaultSystem (
|
||||||
|
system:
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs {
|
||||||
|
inherit system;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
cargo
|
||||||
|
rustc
|
||||||
|
rustfmt
|
||||||
|
clippy
|
||||||
|
bacon
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user