From dc67b38420bfe37c0136448e4180a17a17ffe513 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Wed, 4 Dec 2024 16:39:57 -0800 Subject: [PATCH] day3: disable unicode matching, don't recompile RE for every iteration --- 3/Cargo.lock | 17 +++++++++++++++++ 3/Cargo.toml | 3 ++- 3/src/main.rs | 18 ++++++++++-------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/3/Cargo.lock b/3/Cargo.lock index 4c00902..918bc6f 100644 --- a/3/Cargo.lock +++ b/3/Cargo.lock @@ -15,6 +15,7 @@ dependencies = [ name = "day3" version = "0.1.0" dependencies = [ + "nom", "regex", ] @@ -24,6 +25,22 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "regex" version = "1.11.1" diff --git a/3/Cargo.toml b/3/Cargo.toml index 0275d9d..2b7e6ab 100644 --- a/3/Cargo.toml +++ b/3/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -regex = "1.11.1" +nom = "7.1.3" +regex = { version = "1.11.1", default-features = false, features = ["perf", "std"] } diff --git a/3/src/main.rs b/3/src/main.rs index 3347003..3b5af1c 100644 --- a/3/src/main.rs +++ b/3/src/main.rs @@ -1,4 +1,4 @@ -use regex::Regex; +use regex::bytes::Regex; use std::fs::File; use std::io::{BufRead, BufReader, Lines}; use std::time::{Duration, Instant}; @@ -39,10 +39,11 @@ fn main() { fn problem1(input: Lines) -> u64 { let mut sum = 0u64; + let re = Regex::new(r"(?-u)mul\((\d+),(\d+)\)").unwrap(); for line in input.map(|i| i.unwrap()) { - let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap(); - for m in re.captures_iter(&line) { - sum += m[1].parse::().unwrap() * m[2].parse::().unwrap(); + let line = line.as_bytes(); + for m in re.captures_iter(line) { + sum += std::str::from_utf8(&m[1]).unwrap().parse::().unwrap() * std::str::from_utf8(&m[2]).unwrap().parse::().unwrap(); } } sum @@ -52,13 +53,14 @@ fn problem1(input: Lines) -> u64 { fn problem2(input: Lines) -> u64 { let mut sum = 0u64; let mut do_mul = true; + let re = Regex::new(r"(?-u)(do\(\)|don't\(\)|mul\((\d+),(\d+)\))").unwrap(); for line in input.map(|i| i.unwrap()) { - let re = Regex::new(r"(do\(\)|don't\(\)|mul\((\d+),(\d+)\))").unwrap(); - for m in re.captures_iter(&line) { - match &m[1] { + let line = line.as_bytes(); + for m in re.captures_iter(line) { + match std::str::from_utf8(&m[1]).unwrap() { "do()" => do_mul = true, "don't()" => do_mul = false, - _ if do_mul => sum += m[2].parse::().unwrap() * m[3].parse::().unwrap(), + _ if do_mul => sum += std::str::from_utf8(&m[2]).unwrap().parse::().unwrap() * std::str::from_utf8(&m[3]).unwrap().parse::().unwrap(), _ => {} } }