From 61c576afd41bf0844de8661af749c424fe196dd9 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 25 Jan 2024 17:38:56 +0100 Subject: [PATCH] match by regex instead of splitting --- src/common/vectorize.rs | 16 ++++++++-------- src/day09/mod.rs | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/common/vectorize.rs b/src/common/vectorize.rs index ca7177a..0e460f5 100644 --- a/src/common/vectorize.rs +++ b/src/common/vectorize.rs @@ -8,23 +8,23 @@ pub fn split_lines(input: &str) -> Vec { .collect() } -pub fn split_input(entry: &str, re: &str) -> Vec { +pub fn match_input(entry: &str, re: &str) -> Vec { let regex = Regex::new(re) - .expect("Invalid regex"); + .expect("Invalid regex provided!"); - let split: Vec = regex - .split(entry) - .map(|s| s.to_string()) + let matches: Vec = regex + .find_iter(entry) + .map(|m| m.as_str().to_string()) .collect(); - split + matches } -pub fn split_inputs(input: Vec, re: &str) -> Vec> { +pub fn match_inputs(input: Vec, re: &str) -> Vec> { let mut result = Vec::new(); for entry in input { - let split = split_input(&entry, re); + let split = match_input(&entry, re); result.push(split); } diff --git a/src/day09/mod.rs b/src/day09/mod.rs index ab82656..2b90ccf 100644 --- a/src/day09/mod.rs +++ b/src/day09/mod.rs @@ -1,4 +1,4 @@ -use crate::common::{convert_num, read_file, split_inputs, split_lines}; +use crate::common::{convert_num, read_file, match_inputs, split_lines}; #[allow(dead_code)] pub fn part_two() { @@ -122,6 +122,6 @@ fn setup() -> Vec> { let content = read_file(name); let lines = split_lines(&content); - let input = split_inputs(lines, &r"\s+"); + let input = match_inputs(lines, &r"\s*-?\d+\s*"); convert_num(input) }