Day 1 Part 2 Super janky solution. Just could not get to grips with the regex engine

This commit is contained in:
Luke Else 2023-12-01 10:51:24 +00:00
parent 9c26f1ce02
commit b08290c70c

View File

@ -38,7 +38,7 @@ impl Solution for Day01 {
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
const REG: &str = r#"(?=(\d|one|two|three|four|five|six|seven|eight|nine))"#;
const REG: &str = r#"[0-9]|one|two|three|four|five|six|seven|eight|nine"#;
let mut total: u32 = 0;
for line in input {
total += self.regex_get_num(REG, line);
@ -56,9 +56,17 @@ impl Day01 {
fn regex_get_num(&self, regex: &str, input: &mut String) -> u32 {
let re = Regex::new(regex).unwrap();
let input = input.replace("oneight", "oneeight");
let input = input.replace("threeight", "threeeight");
let input = input.replace("fiveight", "fiveeight");
let input = input.replace("nineight", "nineeight");
let input = input.replace("twone", "twoone");
let input = input.replace("sevenine", "sevennine");
let input = input.replace("eightwo", "eighttwo");
// Get all single digits out of string
let matches: Vec<&str> = re
.find_iter(input)
.find_iter(&input)
.map(|m| m.unwrap().as_str())
.collect();