From 703c429bcfd40b98fd85fb7fc5696fbab75d4a3c Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Tue, 3 Dec 2024 07:22:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Day=203:=20Significant=20p?= =?UTF-8?q?erformance=20improvement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/days/d03.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/days/d03.rs b/src/days/d03.rs index 3a06482..a7db967 100644 --- a/src/days/d03.rs +++ b/src/days/d03.rs @@ -11,7 +11,7 @@ pub struct Command { impl Command { pub fn run(&self) -> u64 { - if !self.command.ends_with("mul") || self.parameters.len() < 2 { + if self.command != "mul" || self.parameters.len() < 2 { return 0; } self.parameters[0] * self.parameters[1] @@ -32,9 +32,9 @@ impl Program { self.0 .iter() .map(|v| { - if v.command.ends_with("don't") { + if v.command == "don't" { enabled = false; - } else if v.command.ends_with("do") { + } else if v.command == "do" { enabled = true; } else if enabled { return v.run(); @@ -47,7 +47,7 @@ impl Program { impl From<&str> for Program { fn from(value: &str) -> Self { - let pattern: Regex = Regex::new(r"([a-z_']+)\(((?:\d+,?)*)\)").unwrap(); + let pattern: Regex = Regex::new(r"(do|don't|mul)\(((?:\d+,?)*)\)").unwrap(); let mut commands = vec![]; for (_, [command, parameters]) in pattern.captures_iter(value).map(|c| c.extract()) {