work in progress
This commit is contained in:
parent
3fe753bab6
commit
83b752a517
@ -1,23 +1,17 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead};
|
use std::io::{self, BufRead};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub fn process() -> u32 {
|
pub fn process() -> u32 {
|
||||||
process_day_1_part_2()
|
process_day_1_part_2()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[derive(Eq, Hash, PartialEq, Debug)]
|
||||||
fn process_day_1() -> u32 {
|
struct NumWord {
|
||||||
let file_path = "./input_sources/day1.txt";
|
word: String,
|
||||||
let mut to_sum: Vec<u32> = vec![];
|
value: u32,
|
||||||
if let Ok(lines) = read_lines(file_path) {
|
location: u32
|
||||||
for line in lines.flatten() {
|
|
||||||
let numbers = extract_numbers_from_string(line.clone());
|
|
||||||
to_sum.push(numbers);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let sum: u32 = to_sum.iter().sum();
|
|
||||||
sum
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
@ -53,30 +47,72 @@ fn process_day_1_part_2() -> u32 {
|
|||||||
}
|
}
|
||||||
// Need to search for strings within the line and return their index.
|
// Need to search for strings within the line and return their index.
|
||||||
// Build a struct ( )
|
// Build a struct ( )
|
||||||
let num_words: Vec<String> = vec![
|
let num_words = HashMap::from([
|
||||||
String::from("one"),
|
("one".to_string(), 1),
|
||||||
String::from("two"),
|
("two".to_string(), 2),
|
||||||
String::from("three"),
|
("three".to_string(), 3),
|
||||||
String::from("four"),
|
("four".to_string(), 4),
|
||||||
String::from("five"),
|
("five".to_string(), 5),
|
||||||
String::from("six"),
|
("six".to_string(), 6),
|
||||||
String::from("seven"),
|
("seven".to_string(), 7),
|
||||||
String::from("eight"),
|
("eight".to_string(), 8),
|
||||||
String::from("nine"),
|
("nine".to_string(), 9)
|
||||||
];
|
]);
|
||||||
|
|
||||||
for word in num_words {
|
let mut words_info: HashMap<u32, u32> = HashMap::new();
|
||||||
|
for (word, val) in num_words {
|
||||||
let found_word: Option<usize> = line.find(&word);
|
let found_word: Option<usize> = line.find(&word);
|
||||||
match found_word {
|
match found_word {
|
||||||
None => {
|
None => {
|
||||||
},
|
},
|
||||||
Some(index) => {
|
Some(index) => {
|
||||||
|
words_info.insert(val, index as u32);
|
||||||
println!("Found word: {} at location {}", word, index)
|
println!("Found word: {} at location {}", word, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let mut count_vec: Vec<_> = words_info.iter().collect();
|
||||||
|
count_vec.sort_by(|a, b| a.1.cmp(b.1));
|
||||||
|
|
||||||
|
// If we have a first char, get the numeric value for the last item
|
||||||
|
let word_num_count = count_vec.len();
|
||||||
|
|
||||||
|
if first_number {
|
||||||
|
if word_num_count > 0 {
|
||||||
|
let last_word_num = count_vec.clone().into_iter().last();
|
||||||
|
match last_word_num {
|
||||||
|
None => {
|
||||||
|
},
|
||||||
|
Some(tupval) => {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if second_number {
|
||||||
|
if word_num_count > 0 {
|
||||||
|
let first_word_num = count_vec.clone().into_iter().nth(0);
|
||||||
|
match first_word_num {
|
||||||
|
None => {
|
||||||
|
},
|
||||||
|
Some(tupval) => {
|
||||||
|
// This needs to prepend instead of Push()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !first_number && !second_number {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// If we have a last char, get the numeric value for the first item
|
||||||
|
// If we have no chars,
|
||||||
|
// see if we have one item, if we do, repeat it twice
|
||||||
|
// see if we have two items, if we do, add them sequentially
|
||||||
|
// see if we have > 2 items, if we do, add the first and the last item.
|
||||||
println!("First Char: {} - Last Char {}\n", first_ch, last_ch);
|
println!("First Char: {} - Last Char {}\n", first_ch, last_ch);
|
||||||
// Create an index of valid words +location
|
// Create an index of valid words +location
|
||||||
// Valid numbers plus location
|
// Valid numbers plus location
|
||||||
@ -99,6 +135,7 @@ where P: AsRef<Path>, {
|
|||||||
Ok(io::BufReader::new(file).lines())
|
Ok(io::BufReader::new(file).lines())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
fn extract_numbers_from_string(input: String) -> u32 {
|
fn extract_numbers_from_string(input: String) -> u32 {
|
||||||
let mut nums_in_string: Vec<char> = vec![];
|
let mut nums_in_string: Vec<char> = vec![];
|
||||||
for char in input.chars() {
|
for char in input.chars() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user