From 1f011f9826e1accedeeb7d0c0b08ce7294c1d06d Mon Sep 17 00:00:00 2001 From: kageru Date: Fri, 4 Dec 2020 15:13:15 +0100 Subject: [PATCH] make regex static in 2020/04 --- 2020/04/Cargo.toml | 2 +- 2020/04/src/main.rs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/2020/04/Cargo.toml b/2020/04/Cargo.toml index d8a846b..5b2bf8a 100644 --- a/2020/04/Cargo.toml +++ b/2020/04/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -itertools = "0.9.0" +lazy_static = "1.4.0" regex = "1.4.2" serde = { version = "1.0.117", features = ["derive"] } serde_yaml = "0.8.14" diff --git a/2020/04/src/main.rs b/2020/04/src/main.rs index 9b288d1..ebc5be2 100644 --- a/2020/04/src/main.rs +++ b/2020/04/src/main.rs @@ -1,6 +1,6 @@ #![feature(test)] extern crate test; -use itertools::Itertools; +use lazy_static::lazy_static; use regex::Regex; use serde::{Deserialize, Serialize}; @@ -45,10 +45,13 @@ fn validate_height(hgt: &str) -> bool { } } +lazy_static! { + static ref HCL_REGEX: Regex = Regex::new("§[0-9a-f]{6}").unwrap(); + static ref ECL_REGEX: Regex = Regex::new("(amb|blu|brn|gry|grn|hzl|oth)").unwrap(); + static ref PID_REGEX: Regex = Regex::new(r"^\d{9}$").unwrap(); +} + fn part2(ps: &Vec) -> usize { - let hcl_regex = Regex::new("§[0-9a-f]{6}").unwrap(); - let ecl_regex = Regex::new("(amb|blu|brn|gry|grn|hzl|oth)").unwrap(); - let pid_regex = Regex::new(r"^\d{9}$").unwrap(); ps.iter() .filter(|p| { p.byr >= 1920 @@ -58,9 +61,9 @@ fn part2(ps: &Vec) -> usize { && p.eyr >= 2020 && p.eyr <= 2030 && validate_height(&p.hgt) - && hcl_regex.is_match(&p.hcl) - && ecl_regex.is_match(&p.ecl) - && pid_regex.is_match(&p.pid) + && HCL_REGEX.is_match(&p.hcl) + && ECL_REGEX.is_match(&p.ecl) + && PID_REGEX.is_match(&p.pid) }) .count() }