From 7f4517b9e6c01491f7021b4dc11f3a4ef0ced8f2 Mon Sep 17 00:00:00 2001 From: Joshua Smallwood <31932412+smallwoj@users.noreply.github.com> Date: Tue, 2 Dec 2025 20:07:05 -0600 Subject: [PATCH] feat: solve day 2 --- Cargo.lock | 40 +++++++++++++++++--------- Cargo.toml | 2 +- data/examples/02.txt | 1 + src/bin/02.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 data/examples/02.txt create mode 100644 src/bin/02.rs diff --git a/Cargo.lock b/Cargo.lock index 90137cc..25c8c88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,9 +23,9 @@ version = "0.12.0" dependencies = [ "chrono", "dhat", + "fancy-regex", "itertools", "pico-args", - "regex", "tinyjson", ] @@ -74,6 +74,21 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + [[package]] name = "bitflags" version = "1.3.2" @@ -143,6 +158,17 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "fancy-regex" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "998b056554fbe42e03ae0e152895cd1a7e1002aec800fdc6635d20270260c46f" +dependencies = [ + "bit-set", + "regex-automata", + "regex-syntax", +] + [[package]] name = "gimli" version = "0.28.1" @@ -329,18 +355,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "regex" -version = "1.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - [[package]] name = "regex-automata" version = "0.4.13" diff --git a/Cargo.toml b/Cargo.toml index af508f9..efd1698 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,5 +29,5 @@ tinyjson = "2.5.1" # Solution dependencies itertools = "0.14.0" -regex = "1.12.2" +fancy-regex = "0.16.2" diff --git a/data/examples/02.txt b/data/examples/02.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/data/examples/02.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/src/bin/02.rs b/src/bin/02.rs new file mode 100644 index 0000000..0c5be43 --- /dev/null +++ b/src/bin/02.rs @@ -0,0 +1,68 @@ +use fancy_regex::Regex; +use itertools::Itertools; +use std::str::FromStr; + +advent_of_code::solution!(2); + +pub fn part_one(input: &str) -> Option { + input + .split(",") + .map(|range| { + let [start, end] = range.split("-").collect::>().try_into().unwrap(); + let start_num = start.parse().unwrap_or(0); + let end_num = end.parse().unwrap_or(0); + (start_num..(end_num + 1)) + .map(|id| { + let mut chars = id.to_string().chars().collect::>(); + let second_chars = chars.split_off(chars.len() / 2); + if chars == second_chars { id } else { 0 } + }) + .sum1() + .unwrap_or(0) + }) + .sum1() +} + +pub fn part_two(input: &str) -> Option { + input + .split(",") + .map(|range| { + let [start, end] = range + .split("-") + .map(|s| s.trim()) + .collect::>() + .try_into() + .unwrap(); + let start_num: u128 = u128::from_str(start).unwrap(); + let end_num: u128 = u128::from_str(end).unwrap(); + (start_num..(end_num + 1)) + .map(|id| { + let re = Regex::new(r"^(\d+)\1+$").unwrap(); + if re.is_match(&id.to_string()).unwrap_or(false) { + id + } else { + 0 + } + }) + .sum1() + .unwrap_or(0) + }) + .sum1() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(1227775554)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(4174379265)); + } +}