From 53977edeadd2df1074d472bcb7022f77b5011ce0 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Wed, 4 Dec 2024 10:43:11 -0500 Subject: [PATCH] Extract constants --- rs/Cargo.lock | 16 ++++++++++++++++ rs/Cargo.toml | 1 + rs/src/day4.rs | 23 ++++++++++++----------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/rs/Cargo.lock b/rs/Cargo.lock index 57b6ac25..b16a1937 100644 --- a/rs/Cargo.lock +++ b/rs/Cargo.lock @@ -27,12 +27,28 @@ checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" name = "aoc2024" version = "0.1.0" dependencies = [ + "array-util", "criterion", "indoc", "itertools 0.13.0", "pretty_assertions", ] +[[package]] +name = "array-util" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e509844de8f09b90a2c3444684a2b6695f4071360e13d2fda0af9f749cc2ed6" +dependencies = [ + "arrayvec", +] + +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "autocfg" version = "1.4.0" diff --git a/rs/Cargo.toml b/rs/Cargo.toml index d2bc545c..e105df58 100644 --- a/rs/Cargo.toml +++ b/rs/Cargo.toml @@ -17,6 +17,7 @@ name = "aoc2024" path = "src/main.rs" [dependencies] +array-util = "1.0.2" itertools = "0.13" [dev-dependencies] diff --git a/rs/src/day4.rs b/rs/src/day4.rs index ce595f2f..a7585e09 100644 --- a/rs/src/day4.rs +++ b/rs/src/day4.rs @@ -1,29 +1,34 @@ +use array_util::try_from_fn; use itertools::izip; pub fn part1(data: &str) -> usize { + const XMAS: [[u8; 4]; 2] = [*b"XMAS", *b"SAMX"]; + let lines = &data.lines().collect::>()[..]; lines .iter() .enumerate() .flat_map(|(y, line)| { (0..line.len()).flat_map(move |x| { - (-1..=1).flat_map(move |dy| { - (-1..=1).filter(move |dx| { - "XMAS".bytes().enumerate().all(|(i, b)| { + [(1, 0), (1, 1), (0, 1), (-1, 1)] + .iter() + .filter_map(move |(dx, dy)| { + try_from_fn(|i| { y.checked_add_signed(i as isize * dy) .and_then(|y| lines.get(y)) .zip(x.checked_add_signed(i as isize * dx)) - .and_then(|(line, x)| line.as_bytes().get(x)) - == Some(&b) + .and_then(|(line, x)| line.as_bytes().get(x).copied()) }) }) - }) }) }) + .filter(|s| XMAS.contains(s)) .count() } pub fn part2(data: &str) -> usize { + const XMAS: [[u8; 5]; 4] = [*b"MMASS", *b"MSAMS", *b"SMASM", *b"SSAMM"]; + let lines = data.lines().collect::>(); izip!(&lines[..], &lines[1..], &lines[2..]) .flat_map(|(above, line, below)| { @@ -34,12 +39,8 @@ pub fn part2(data: &str) -> usize { below.bytes(), below.bytes().skip(2) ) - .filter(|&(nw, ne, b, sw, se)| { - b == b'A' - && (nw == b'M' && se == b'S' || se == b'M' && nw == b'S') - && (ne == b'M' && sw == b'S' || sw == b'M' && ne == b'S') - }) }) + .filter(|&(nw, ne, b, sw, se)| XMAS.contains(&[nw, ne, b, sw, se])) .count() }