feat: day 10

This commit is contained in:
Lucy 2024-12-12 12:59:51 +01:00
parent a7bc0e746a
commit ebae29e209
5 changed files with 235 additions and 1 deletions

View file

@ -4,7 +4,18 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[features] [features]
full = ["day1", "day2", "day3", "day4", "day5", "day6", "day7", "day8", "day9"] full = [
"day1",
"day2",
"day3",
"day4",
"day5",
"day6",
"day7",
"day8",
"day9",
"day10",
]
default = ["full"] default = ["full"]
day1 = [] day1 = []
day2 = [] day2 = []
@ -15,6 +26,7 @@ day6 = []
day7 = [] day7 = []
day8 = [] day8 = []
day9 = [] day9 = []
day10 = []
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -69,6 +69,7 @@
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
shellHook = '' shellHook = ''
export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc}
export RUST_LOG=info
''; '';
buildInputs = runtimeDeps; buildInputs = runtimeDeps;
nativeBuildInputs = nativeBuildInputs =

8
inputs/day10/example.txt Normal file
View file

@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

209
src/day10.rs Normal file
View file

@ -0,0 +1,209 @@
#![allow(
clippy::cast_sign_loss,
clippy::cast_possible_wrap,
clippy::too_many_lines
)]
use std::collections::HashSet;
use tracing::debug;
pub fn pt1(input: &str) -> usize {
let grid = input
.lines()
.enumerate()
.flat_map(|(y, line)| {
line.chars()
.enumerate()
.map(move |(x, c)| (c, x as isize, y as isize))
})
.collect::<Vec<_>>();
let zeroes = grid
.iter()
.filter(|(c, _, _)| *c == '0')
.map(|(_, x, y)| (*x, *y))
.collect::<HashSet<_>>();
let neighbours = |x: isize, y: isize, c: char| -> Vec<(isize, isize)> {
[(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
.iter()
.filter(|(ox, oy)| {
grid.iter()
.any(|(c2, x2, y2)| *c2 == c && (ox == x2 && oy == y2))
})
.map(|(ox, oy)| (*ox, *oy))
.collect::<Vec<_>>()
};
let ones = zeroes
.clone()
.into_iter()
.map(|(x, y)| neighbours(x, y, '1'))
.collect::<Vec<_>>();
let twos = ones
.clone()
.into_iter()
.map(|list| {
list.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '2'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let threes = twos
.clone()
.into_iter()
.map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '3'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let fours = threes
.clone()
.into_iter()
.map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '4'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let fives = fours
.clone()
.into_iter()
.map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '5'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let sixes = fives
.clone()
.into_iter()
.map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '6'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let sevens = sixes
.clone()
.into_iter()
.map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '7'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let eights = sevens
.clone()
.into_iter()
.map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '8'))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let nines = eights
.clone()
.into_iter()
.flat_map(|line| {
line.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '9'))
.collect::<HashSet<_>>()
.into_iter()
})
.collect::<Vec<_>>();
debug!("zeroes: {zeroes:?}");
debug!("ones: {ones:?}");
debug!("twos: {twos:?}");
debug!("threes: {threes:?}");
debug!("fours: {fours:?}");
debug!("fives: {fives:?}");
debug!("sixes: {sixes:?}");
debug!("sevens: {sevens:?}");
debug!("eights: {eights:?}");
debug!("nines: {nines:?}");
nines.len()
}
pub fn pt2(input: &str) -> usize {
let grid = input
.lines()
.enumerate()
.flat_map(|(y, line)| {
line.chars()
.enumerate()
.map(move |(x, c)| (c, x as isize, y as isize))
})
.collect::<Vec<_>>();
let zeroes = grid
.iter()
.filter(|(c, _, _)| *c == '0')
.map(|(_, x, y)| (*x, *y))
.collect::<HashSet<_>>();
let neighbours = |x: isize, y: isize, c: char| -> Vec<(isize, isize)> {
[(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
.iter()
.filter(|(ox, oy)| {
grid.iter()
.any(|(c2, x2, y2)| *c2 == c && (ox == x2 && oy == y2))
})
.map(|(ox, oy)| (*ox, *oy))
.collect::<Vec<_>>()
};
zeroes
.clone()
.into_iter()
.flat_map(|(x, y)| neighbours(x, y, '1'))
.flat_map(|(x, y)| neighbours(x, y, '2'))
.flat_map(|(x, y)| neighbours(x, y, '3'))
.flat_map(|(x, y)| neighbours(x, y, '4'))
.flat_map(|(x, y)| neighbours(x, y, '5'))
.flat_map(|(x, y)| neighbours(x, y, '6'))
.flat_map(|(x, y)| neighbours(x, y, '7'))
.flat_map(|(x, y)| neighbours(x, y, '8'))
.flat_map(|(x, y)| neighbours(x, y, '9'))
.count()
}
#[cfg(test)]
mod tests {
use tracing_test::traced_test;
use super::{pt1, pt2};
use std::fs::read_to_string;
#[traced_test]
#[test]
pub fn test_pt1() {
let input =
read_to_string("./inputs/day10/example.txt").expect("Missing example.txt for day10");
assert_eq!(pt1(&input), 36)
}
#[traced_test]
#[test]
pub fn test_pt1_2() {
let input = "...0...\n...1...\n...2...\n6543456\n7.....7\n8.....8\n9.....9";
assert_eq!(pt1(&input), 2);
}
#[traced_test]
#[test]
pub fn test_pt1_3() {
let input = "..90..9\n...1.98\n...2..7\n6543456\n765.987\n876....\n987....";
assert_eq!(pt1(&input), 4);
}
#[traced_test]
#[test]
pub fn test_pt1_4() {
let input = "0123\n1234\n8765\n9876";
assert_eq!(pt1(&input), 1);
}
#[traced_test]
#[test]
pub fn test_pt2() {
let input =
read_to_string("./inputs/day10/example.txt").expect("Missing example.txt for day10");
assert_eq!(pt2(&input), 81)
}
}

View file

@ -21,6 +21,8 @@ mod day07;
mod day08; mod day08;
#[cfg(feature = "day9")] #[cfg(feature = "day9")]
mod day09; mod day09;
#[cfg(feature = "day10")]
mod day10;
fn main() -> eyre::Result<()> { fn main() -> eyre::Result<()> {
color_eyre::install()?; color_eyre::install()?;
@ -45,6 +47,8 @@ fn main() -> eyre::Result<()> {
solve_day(8, day08::pt1, day08::pt2); solve_day(8, day08::pt1, day08::pt2);
#[cfg(feature = "day9")] #[cfg(feature = "day9")]
solve_day(9, day09::pt1, day09::pt2); solve_day(9, day09::pt1, day09::pt2);
#[cfg(feature = "day10")]
solve_day(10, day10::pt1, day10::pt2);
Ok(()) Ok(())
} }