diff --git a/day_1/new_result b/day_1/new_result new file mode 100644 index 0000000..e4ff976 Binary files /dev/null and b/day_1/new_result differ diff --git a/day_1/src/main.rs b/day_1/src/main.rs index 08cc8f7..ff2db75 100644 --- a/day_1/src/main.rs +++ b/day_1/src/main.rs @@ -3,60 +3,46 @@ use std::{io::prelude::*, fs::File, path::Path, io }; fn main() { - do_part_1(); + run_solution(false); println!("\nPress enter to continue"); let mut buffer = String::new(); io::stdin().read_line(&mut buffer).unwrap(); - do_part_2(); + run_solution(true); } -fn do_part_2() +fn run_solution(check_for_spelled: bool) { - let data = load_data("data/part2_input"); + let data = load_data("data/input"); println!("\nProcessing Data..."); let mut sum: u64 = 0; for line in data.lines() { - let first = get_first_digit(line.to_ascii_lowercase().as_bytes(), true); - let last = get_last_digit(line.to_ascii_lowercase().as_bytes(), true); + let first = get_digit(line.to_ascii_lowercase().as_bytes(), false, check_for_spelled); + let last = get_digit(line.to_ascii_lowercase().as_bytes(), true, check_for_spelled); + let num = (first * 10) + last; - println!("\nLine: {} -- First: {}, Second: {}, Num: {}", line, first, last, num); + // println!("\nLine: {} -- First: {}, Second: {}, Num: {}", line, first, last, num); sum += num as u64; } println!("\nFinal Sum: {}", sum); } -fn do_part_1() +fn get_digit(line: &[u8], from_back: bool, check_for_spelled: bool) -> u8 { - let data = load_data("data/input"); - - println!("\nProcessing Data..."); - - let mut sum: u64 = 0; - for line in data.lines() + let mut range: Vec = (0..line.len()).collect(); + if from_back { - let first = get_first_digit(line.to_ascii_lowercase().as_bytes(), false); - let last = get_last_digit(line.to_ascii_lowercase().as_bytes(), false); - - let num = (first * 10) + last; - - println!("\nLine: {} -- First: {}, Second: {}, Num: {}", line, first, last, num); - sum += num as u64; + range.reverse(); } - println!("\nFinal Sum: {}", sum); -} - -fn get_first_digit(line: &[u8], check_for_spelled: bool) -> u8 -{ - for i in 0..line.len() + for i in range { if is_num(line[i]) { @@ -75,28 +61,28 @@ fn get_first_digit(line: &[u8], check_for_spelled: bool) -> u8 return 0; } -fn get_last_digit(line: &[u8], check_for_spelled: bool) -> u8 -{ - //let mut last_digit = 0 as u8; - - for i in (0..line.len()).rev() - { - if is_num(line[i]) - { - return line[i] - 48; - } - - if check_for_spelled - { - if let Some(num) = is_spelled_num(line, i) - { - return num; - } - } - } - - 0 -} +// fn get_last_digit(line: &[u8], check_for_spelled: bool) -> u8 +// { +// //let mut last_digit = 0 as u8; + +// for i in (0..line.len()).rev() +// { +// if is_num(line[i]) +// { +// return line[i] - 48; +// } + +// if check_for_spelled +// { +// if let Some(num) = is_spelled_num(line, i) +// { +// return num; +// } +// } +// } + +// 0 +// } fn is_num(c: u8) -> bool {