Finish Day 1
This commit is contained in:
parent
40b1277b55
commit
d3d1c25299
6 changed files with 142 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
**/target/
|
7
day01/Cargo.lock
generated
Normal file
7
day01/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day01"
|
||||||
|
version = "0.1.0"
|
8
day01/Cargo.toml
Normal file
8
day01/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day01"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
8
day01/data/testDataTwo.txt
Normal file
8
day01/data/testDataTwo.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
||||||
|
|
17
day01/src/bin/first.rs
Normal file
17
day01/src/bin/first.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
const FILE_NAME:&str = "./data/data.txt";
|
||||||
|
fn main() {
|
||||||
|
let file_contents = fs::read_to_string(FILE_NAME).unwrap();
|
||||||
|
let lines:Vec<&str> = file_contents.split('\n').collect();
|
||||||
|
let mut final_output = 0;
|
||||||
|
for line in lines{
|
||||||
|
let line_string = line.to_string();
|
||||||
|
if line_string.is_empty() { break; }
|
||||||
|
let front_iter = line_string.clone().chars().find(|c:&char| c.is_ascii_digit()).unwrap();
|
||||||
|
let back_iter = line_string.clone().chars().rfind(|c:&char| c.is_ascii_digit()).unwrap();
|
||||||
|
let temp = (front_iter.to_digit(10).unwrap() * 10) + back_iter.to_digit(10).unwrap();
|
||||||
|
final_output += temp;
|
||||||
|
}
|
||||||
|
println!("{}",final_output);
|
||||||
|
}
|
101
day01/src/bin/second.rs
Normal file
101
day01/src/bin/second.rs
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
use std::{fs, collections::HashMap};
|
||||||
|
|
||||||
|
const ONE:&str = "one";
|
||||||
|
const TWO:&str = "two";
|
||||||
|
const THREE:&str = "three";
|
||||||
|
const FOUR:&str = "four";
|
||||||
|
const FIVE:&str = "five";
|
||||||
|
const SIX:&str = "six";
|
||||||
|
const SEVEN:&str = "seven";
|
||||||
|
const EIGHT:&str = "eight";
|
||||||
|
const NINE:&str = "nine";
|
||||||
|
|
||||||
|
const NUMBERS:[&str;9] = [ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE];
|
||||||
|
const FILE_NAME:&str = "./data/data.txt";
|
||||||
|
fn main() {
|
||||||
|
let mut MAP:HashMap<&str,u8> = HashMap::new();
|
||||||
|
MAP.insert(ONE,1);
|
||||||
|
MAP.insert(TWO,2);
|
||||||
|
MAP.insert(THREE,3);
|
||||||
|
MAP.insert(FOUR,4);
|
||||||
|
MAP.insert(FIVE,5);
|
||||||
|
MAP.insert(SIX,6);
|
||||||
|
MAP.insert(SEVEN,7);
|
||||||
|
MAP.insert(EIGHT,8);
|
||||||
|
MAP.insert(NINE,9);
|
||||||
|
let file_contents = fs::read_to_string(FILE_NAME).unwrap();
|
||||||
|
let lines:Vec<&str> = file_contents.split('\n').collect();
|
||||||
|
let mut final_output:u64 = 0;
|
||||||
|
for line in lines{
|
||||||
|
let line_string = line.to_string();
|
||||||
|
if line_string.is_empty() { break; }
|
||||||
|
let mut front_iter = line_string.chars();
|
||||||
|
let mut front_digit:u8 = 0;
|
||||||
|
let mut back_digit:u8 = 0;
|
||||||
|
let mut potential_word = String::new();
|
||||||
|
while let Some(next_char) = front_iter.next(){
|
||||||
|
if next_char.is_ascii_digit(){
|
||||||
|
front_digit = next_char.to_digit(10).unwrap() as u8;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
potential_word.push(next_char);
|
||||||
|
let digit_convert = is_text_digit(MAP.clone(), potential_word.clone());
|
||||||
|
println!("current:{:?}\t detect:{:?}",potential_word,digit_convert);
|
||||||
|
match digit_convert{
|
||||||
|
None => {
|
||||||
|
potential_word.remove(0);
|
||||||
|
},
|
||||||
|
Some(0) => {
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
Some(value) => {
|
||||||
|
front_digit = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut back_iter = line_string.chars();
|
||||||
|
potential_word.clear();
|
||||||
|
while let Some(next_char) = back_iter.next_back(){
|
||||||
|
if next_char.is_ascii_digit(){
|
||||||
|
back_digit = next_char.to_digit(10).unwrap() as u8;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
let mut char_string = next_char.to_string();
|
||||||
|
char_string.push_str(&potential_word);
|
||||||
|
let digit_convert = is_text_digit(MAP.clone(), char_string.clone());
|
||||||
|
println!("last:{:?}\t current:{:?}\t detect:{:?}",potential_word,char_string,digit_convert);
|
||||||
|
match digit_convert{
|
||||||
|
None => {
|
||||||
|
potential_word = char_string;
|
||||||
|
potential_word.pop();
|
||||||
|
},
|
||||||
|
Some(0) => {
|
||||||
|
potential_word = char_string;
|
||||||
|
},
|
||||||
|
Some(value) => {
|
||||||
|
back_digit = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let temp = (front_digit * 10) + back_digit;
|
||||||
|
println!("{:?} + {:?}",final_output,temp);
|
||||||
|
final_output += temp as u64;
|
||||||
|
}
|
||||||
|
println!("{}",final_output);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_text_digit(map:HashMap<&str,u8>,string:String) -> Option<u8>{
|
||||||
|
for digit in NUMBERS{
|
||||||
|
if string.contains(digit){
|
||||||
|
return Some(*map.get(digit).unwrap());
|
||||||
|
}
|
||||||
|
if digit.contains(&string){
|
||||||
|
return Some(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
None
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue