refactor day 1 solutions

day_5_part_2_broken
Joeyrp 2 years ago
parent e17c9b7b1e
commit f122a4763c

Binary file not shown.

@ -3,60 +3,45 @@ 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<usize> = (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 +60,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
{

Loading…
Cancel
Save