From ebae29e209f8907539af674eb015a93d46379114 Mon Sep 17 00:00:00 2001 From: Lucy Date: Thu, 12 Dec 2024 12:59:51 +0100 Subject: [PATCH] feat: day 10 --- Cargo.toml | 14 ++- flake.nix | 1 + inputs/day10/example.txt | 8 ++ src/day10.rs | 209 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 + 5 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 inputs/day10/example.txt create mode 100644 src/day10.rs diff --git a/Cargo.toml b/Cargo.toml index 020ed76..0dd9a1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,18 @@ version = "0.1.0" edition = "2021" [features] -full = ["day1", "day2", "day3", "day4", "day5", "day6", "day7", "day8", "day9"] +full = [ + "day1", + "day2", + "day3", + "day4", + "day5", + "day6", + "day7", + "day8", + "day9", + "day10", +] default = ["full"] day1 = [] day2 = [] @@ -15,6 +26,7 @@ day6 = [] day7 = [] day8 = [] day9 = [] +day10 = [] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/flake.nix b/flake.nix index 611363a..ebd2764 100644 --- a/flake.nix +++ b/flake.nix @@ -69,6 +69,7 @@ devShells.default = pkgs.mkShell { shellHook = '' export RUST_SRC_PATH=${pkgs.rustPlatform.rustLibSrc} + export RUST_LOG=info ''; buildInputs = runtimeDeps; nativeBuildInputs = diff --git a/inputs/day10/example.txt b/inputs/day10/example.txt new file mode 100644 index 0000000..7bb1248 --- /dev/null +++ b/inputs/day10/example.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 \ No newline at end of file diff --git a/src/day10.rs b/src/day10.rs new file mode 100644 index 0000000..f7473a9 --- /dev/null +++ b/src/day10.rs @@ -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::>(); + let zeroes = grid + .iter() + .filter(|(c, _, _)| *c == '0') + .map(|(_, x, y)| (*x, *y)) + .collect::>(); + 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::>() + }; + let ones = zeroes + .clone() + .into_iter() + .map(|(x, y)| neighbours(x, y, '1')) + .collect::>(); + let twos = ones + .clone() + .into_iter() + .map(|list| { + list.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '2')) + .collect::>() + }) + .collect::>(); + let threes = twos + .clone() + .into_iter() + .map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '3')) + .collect::>() + }) + .collect::>(); + let fours = threes + .clone() + .into_iter() + .map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '4')) + .collect::>() + }) + .collect::>(); + let fives = fours + .clone() + .into_iter() + .map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '5')) + .collect::>() + }) + .collect::>(); + let sixes = fives + .clone() + .into_iter() + .map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '6')) + .collect::>() + }) + .collect::>(); + let sevens = sixes + .clone() + .into_iter() + .map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '7')) + .collect::>() + }) + .collect::>(); + let eights = sevens + .clone() + .into_iter() + .map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '8')) + .collect::>() + }) + .collect::>(); + let nines = eights + .clone() + .into_iter() + .flat_map(|line| { + line.into_iter() + .flat_map(|(x, y)| neighbours(x, y, '9')) + .collect::>() + .into_iter() + }) + .collect::>(); + 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::>(); + let zeroes = grid + .iter() + .filter(|(c, _, _)| *c == '0') + .map(|(_, x, y)| (*x, *y)) + .collect::>(); + 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::>() + }; + 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) + } +} diff --git a/src/main.rs b/src/main.rs index bfa0cc5..7311ed5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,8 @@ mod day07; mod day08; #[cfg(feature = "day9")] mod day09; +#[cfg(feature = "day10")] +mod day10; fn main() -> eyre::Result<()> { color_eyre::install()?; @@ -45,6 +47,8 @@ fn main() -> eyre::Result<()> { solve_day(8, day08::pt1, day08::pt2); #[cfg(feature = "day9")] solve_day(9, day09::pt1, day09::pt2); + #[cfg(feature = "day10")] + solve_day(10, day10::pt1, day10::pt2); Ok(()) }