Compare commits
No commits in common. "b08290c70c6449fe454c1a5863dd6ccf299b19b8" and "eebfbe67e536f855f22746d35566e9e2ae8e934d" have entirely different histories.
b08290c70c
...
eebfbe67e5
45
.vscode/launch.json
vendored
45
.vscode/launch.json
vendored
@ -1,45 +0,0 @@
|
|||||||
{
|
|
||||||
// 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}"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
@ -3,7 +3,7 @@ name = "advent_of_code_2023"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fancy-regex = "0.12.0"
|
regex = "1.10.2"
|
||||||
strum = "0.25.0"
|
|
||||||
strum_macros = "0.25.3"
|
|
||||||
|
@ -16,8 +16,5 @@ fn main() -> Result<(), Box<dyn Error>>{
|
|||||||
println!("Day01 Part1 Test: {}", day01.part1(get_input(day01.get_day(), utils::InputType::Test)?.as_mut())?);
|
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 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,7 @@
|
|||||||
use super::dayxx::Solution;
|
use super::dayxx::Solution;
|
||||||
use std::str::FromStr;
|
use regex::Regex;
|
||||||
use fancy_regex::Regex;
|
|
||||||
use strum_macros::EnumString;
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
const REG: &str = r#"[0-9]"#;
|
||||||
#[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 {}
|
pub struct Day01 {}
|
||||||
|
|
||||||
@ -25,10 +10,9 @@ 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#"[0-9]"#;
|
|
||||||
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(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Box::new(total))
|
Ok(Box::new(total))
|
||||||
@ -38,13 +22,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#"[0-9]|one|two|three|four|five|six|seven|eight|nine"#;
|
Ok(Box::new(0))
|
||||||
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 {
|
fn get_day(&self) -> u8 {
|
||||||
@ -53,42 +31,18 @@ impl Solution for Day01 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Day01 {
|
impl Day01 {
|
||||||
fn regex_get_num(&self, regex: &str, input: &mut String) -> u32 {
|
fn regex_get_num(&self, input: &mut String) -> u32 {
|
||||||
let re = Regex::new(regex).unwrap();
|
let re = Regex::new(REG).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<u32> = re
|
||||||
.find_iter(&input)
|
.find_iter(input)
|
||||||
.map(|m| m.unwrap().as_str())
|
.map(|m| m.as_str().parse::<u32>().unwrap_or_default())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Convert everything to a number
|
let mut num: u32 = matches.first().unwrap_or(&0) * 10;
|
||||||
let mut nums: Vec<u32> = vec![];
|
num += matches.last().unwrap_or(&0);
|
||||||
for m in matches.clone() {
|
|
||||||
match Day01::convert_written_number(m) {
|
|
||||||
Some(n) => nums.push(n),
|
|
||||||
None => nums.push(m.parse::<u32>().unwrap_or(0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut num: u32 = nums.first().unwrap_or(&0) * 10;
|
|
||||||
num += nums.last().unwrap_or(&0);
|
|
||||||
|
|
||||||
num
|
num
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_written_number(number_str: &str) -> Option<u32> {
|
|
||||||
match WrittenNumbers::from_str(number_str) {
|
|
||||||
Ok(number) => Some(number as u32),
|
|
||||||
Err(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user