Day 01 Part 1
This commit is contained in:
parent
af0b387339
commit
eebfbe67e5
@ -6,3 +6,4 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1.10.2"
|
||||
|
1000
src/input/day01
Normal file
1000
src/input/day01
Normal file
File diff suppressed because it is too large
Load Diff
4
src/input/day01_test
Normal file
4
src/input/day01_test
Normal file
@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
19
src/main.rs
19
src/main.rs
@ -1,5 +1,20 @@
|
||||
mod utils;
|
||||
|
||||
mod solutions;
|
||||
|
||||
fn main() {}
|
||||
use std::error::Error;
|
||||
|
||||
use solutions::{
|
||||
dayxx::Solution,
|
||||
day01
|
||||
};
|
||||
|
||||
use crate::utils::get_input;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>>{
|
||||
// Day 1
|
||||
let day01 = day01::Day01{};
|
||||
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())?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
48
src/solutions/day01.rs
Normal file
48
src/solutions/day01.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use super::dayxx::Solution;
|
||||
use regex::Regex;
|
||||
|
||||
const REG: &str = r#"[0-9]"#;
|
||||
|
||||
pub struct Day01 {}
|
||||
|
||||
impl Solution for Day01 {
|
||||
fn part1(
|
||||
&self,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
let mut total: u32 = 0;
|
||||
for line in input {
|
||||
total += self.regex_get_num(line);
|
||||
}
|
||||
|
||||
Ok(Box::new(total))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
&self,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new(0))
|
||||
}
|
||||
|
||||
fn get_day(&self) -> u8 {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
impl Day01 {
|
||||
fn regex_get_num(&self, input: &mut String) -> u32 {
|
||||
let re = Regex::new(REG).unwrap();
|
||||
|
||||
// Get all single digits out of string
|
||||
let matches: Vec<u32> = re
|
||||
.find_iter(input)
|
||||
.map(|m| m.as_str().parse::<u32>().unwrap_or_default())
|
||||
.collect();
|
||||
|
||||
let mut num: u32 = matches.first().unwrap_or(&0) * 10;
|
||||
num += matches.last().unwrap_or(&0);
|
||||
|
||||
num
|
||||
}
|
||||
}
|
@ -14,8 +14,8 @@ pub enum InputType {
|
||||
pub fn get_input(day: u8, input: InputType) -> Result<Vec<String>, Box<dyn Error>> {
|
||||
// Find Input File Name
|
||||
let file_name = match input {
|
||||
InputType::Test => format!("src/input/day{:02}_test.txt", day),
|
||||
InputType::Actual => format!("src/input/day{:02}.txt", day),
|
||||
InputType::Test => format!("src/input/day{:02}_test", day),
|
||||
InputType::Actual => format!("src/input/day{:02}", day),
|
||||
};
|
||||
|
||||
// Read file into buffer
|
||||
|
Loading…
Reference in New Issue
Block a user