day 1 part 2 finished

day_5_part_2_broken
Joey Pollack 2 years ago
parent 409120c113
commit e17c9b7b1e

@ -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 'day_1'",
"cargo": {
"args": [
"build",
"--bin=day_1",
"--package=day_1"
],
"filter": {
"name": "day_1",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'day_1'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=day_1",
"--package=day_1"
],
"filter": {
"name": "day_1",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

@ -1,17 +1,36 @@
use std::{io::prelude::*, fs::File, path::Path };
use std::{io::prelude::*, fs::File, path::Path, io };
fn main()
{
do_part_1();
do_part_2();
println!("\nPress enter to continue");
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
do_part_2();
}
fn do_part_2()
{
let data = load_data("data/part2_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 num = (first * 10) + last;
println!("\nLine: {} -- First: {}, Second: {}, Num: {}", line, first, last, num);
sum += num as u64;
}
println!("\nFinal Sum: {}", sum);
}
fn do_part_1()
@ -23,8 +42,8 @@ fn do_part_1()
let mut sum: u64 = 0;
for line in data.lines()
{
let first = get_first_digit(line);
let last = get_last_digit(line);
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;
@ -35,28 +54,44 @@ fn do_part_1()
println!("\nFinal Sum: {}", sum);
}
fn get_first_digit(line: &str) -> u8
fn get_first_digit(line: &[u8], check_for_spelled: bool) -> u8
{
for c in line.as_bytes()
for i in 0..line.len()
{
if is_num(*c)
if is_num(line[i])
{
return (*c - 48) as u8;
return (line[i] - 48) as u8;
}
if check_for_spelled
{
if let Some(num) = is_spelled_num(line, i)
{
return num;
}
}
}
return 0;
}
fn get_last_digit(line: &str) -> u8
fn get_last_digit(line: &[u8], check_for_spelled: bool) -> u8
{
//let mut last_digit = 0 as u8;
for c in line.chars().rev()
for i in (0..line.len()).rev()
{
if is_num(c as u8)
if is_num(line[i])
{
return line[i] - 48;
}
if check_for_spelled
{
return (c as u8 - 48) as u8;
if let Some(num) = is_spelled_num(line, i)
{
return num;
}
}
}
@ -68,9 +103,32 @@ fn is_num(c: u8) -> bool
c >= 48 && c <= 57
}
fn is_spelled_num(start: &str) -> Option<u8>
fn is_spelled_num(line: &[u8], start: usize) -> Option<u8>
{
let words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
for word_idx in 0..words.len()
{
let mut i = start;
let mut found = true;
for c in words[word_idx].as_bytes()
{
if i < line.len() && *c != line[i]
{
found = false;
break;
}
i += 1;
}
if found && i <= line.len()
{
return Some(word_idx as u8 + 1);
}
}
None
}
fn load_data(file_name: &str) -> String

Loading…
Cancel
Save