Day 1 Part 2 Super janky solution. Just could not get to grips with the regex engine
This commit is contained in:
parent
9c26f1ce02
commit
b08290c70c
@ -38,7 +38,7 @@ impl Solution for Day01 {
|
|||||||
&self,
|
&self,
|
||||||
input: &mut Vec<String>,
|
input: &mut Vec<String>,
|
||||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
) -> 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;
|
let mut total: u32 = 0;
|
||||||
for line in input {
|
for line in input {
|
||||||
total += self.regex_get_num(REG, line);
|
total += self.regex_get_num(REG, line);
|
||||||
@ -56,9 +56,17 @@ impl Day01 {
|
|||||||
fn regex_get_num(&self, regex: &str, input: &mut String) -> u32 {
|
fn regex_get_num(&self, regex: &str, input: &mut String) -> u32 {
|
||||||
let re = Regex::new(regex).unwrap();
|
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
|
// Get all single digits out of string
|
||||||
let matches: Vec<&str> = re
|
let matches: Vec<&str> = re
|
||||||
.find_iter(input)
|
.find_iter(&input)
|
||||||
.map(|m| m.unwrap().as_str())
|
.map(|m| m.unwrap().as_str())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user