diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a63da34 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'advent_of_code_2023'", + "cargo": { + "args": [ + "build", + "--bin=advent_of_code_2023", + "--package=advent_of_code_2023" + ], + "filter": { + "name": "advent_of_code_2023", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'advent_of_code_2023'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=advent_of_code_2023", + "--package=advent_of_code_2023" + ], + "filter": { + "name": "advent_of_code_2023", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f2eab74..164caa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "advent_of_code_2023" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -regex = "1.10.2" +fancy-regex = "0.12.0" +strum = "0.25.0" +strum_macros = "0.25.3" diff --git a/src/main.rs b/src/main.rs index 4b5edde..9d8cb66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,5 +16,8 @@ fn main() -> Result<(), Box>{ println!("Day01 Part1 Test: {}", day01.part1(get_input(day01.get_day(), utils::InputType::Test)?.as_mut())?); println!("Day01 Part1 Result: {}", day01.part1(get_input(day01.get_day(), utils::InputType::Actual)?.as_mut())?); + println!("Day01 Part2 Test: {}", day01.part2(get_input(day01.get_day(), utils::InputType::Test)?.as_mut())?); + println!("Day01 Part2 Result: {}", day01.part2(get_input(day01.get_day(), utils::InputType::Actual)?.as_mut())?); + Ok(()) } diff --git a/src/solutions/day01.rs b/src/solutions/day01.rs index 281bca0..eda372a 100644 --- a/src/solutions/day01.rs +++ b/src/solutions/day01.rs @@ -1,7 +1,22 @@ use super::dayxx::Solution; -use regex::Regex; +use std::str::FromStr; +use fancy_regex::Regex; +use strum_macros::EnumString; -const REG: &str = r#"[0-9]"#; +#[allow(non_camel_case_types)] +#[derive(EnumString)] +enum WrittenNumbers { + zero = 0, + one = 1, + two = 2, + three = 3, + four = 4, + five = 5, + six = 6, + seven = 7, + eight = 8, + nine = 9 +} pub struct Day01 {} @@ -10,9 +25,10 @@ impl Solution for Day01 { &self, input: &mut Vec, ) -> Result, Box> { + const REG: &str = r#"[0-9]"#; let mut total: u32 = 0; for line in input { - total += self.regex_get_num(line); + total += self.regex_get_num(REG, line); } Ok(Box::new(total)) @@ -22,7 +38,13 @@ impl Solution for Day01 { &self, input: &mut Vec, ) -> Result, Box> { - Ok(Box::new(0)) + const REG: &str = r#"(?=(\d|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); + } + + Ok(Box::new(total)) } fn get_day(&self) -> u8 { @@ -31,18 +53,34 @@ impl Solution for Day01 { } impl Day01 { - fn regex_get_num(&self, input: &mut String) -> u32 { - let re = Regex::new(REG).unwrap(); + fn regex_get_num(&self, regex: &str, input: &mut String) -> u32 { + let re = Regex::new(regex).unwrap(); // Get all single digits out of string - let matches: Vec = re + let matches: Vec<&str> = re .find_iter(input) - .map(|m| m.as_str().parse::().unwrap_or_default()) + .map(|m| m.unwrap().as_str()) .collect(); - let mut num: u32 = matches.first().unwrap_or(&0) * 10; - num += matches.last().unwrap_or(&0); + // Convert everything to a number + let mut nums: Vec = vec![]; + for m in matches.clone() { + match Day01::convert_written_number(m) { + Some(n) => nums.push(n), + None => nums.push(m.parse::().unwrap_or(0)) + } + } + + let mut num: u32 = nums.first().unwrap_or(&0) * 10; + num += nums.last().unwrap_or(&0); num } + + fn convert_written_number(number_str: &str) -> Option { + match WrittenNumbers::from_str(number_str) { + Ok(number) => Some(number as u32), + Err(_) => None, + } + } }