This commit is contained in:
Dan Chadwick
2024-11-23 22:14:51 -05:00
parent cc3991380f
commit c7906c9fea
15 changed files with 0 additions and 0 deletions

BIN
2023/src/.DS_Store vendored Normal file

Binary file not shown.

146
2023/src/day1/mod.rs Normal file
View File

@@ -0,0 +1,146 @@
use crate::utils::read_lines;
use std::collections::HashMap;
pub fn process() -> u32 {
process_day_1_part_2()
}
fn process_day_1() -> u32 {
let file_path = "./input_sources/day1.txt";
let mut to_sum: Vec<u32> = vec![];
if let Ok(lines) = read_lines(file_path) {
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
}
fn process_day_1_part_2() -> u32 {
let file_path = "./input_sources/day1.txt";
let mut to_sum: Vec<u32> = vec![];
if let Ok(lines) = read_lines(file_path) {
for line in lines.flatten() {
let line_result: u32 = process_line(line);
to_sum.push(line_result);
}
}
// //println!("\nNumbers to Sum: {:?}", to_sum);
let sum: u32 = to_sum.iter().sum();
sum
}
fn process_line(input_line: String) -> u32 {
//println!("{}", input_line);
let mut num_string: String = String::new();
let mut collector: Vec<(u32, u32)> = vec![];
let mut r_collector: Vec<(u32, u32)> = vec![];
let targets = HashMap::from([
("zero".to_string(), 0),
("0".to_string(), 0),
("one".to_string(), 1),
("1".to_string(), 1),
("two".to_string(), 2),
("2".to_string(), 2),
("three".to_string(), 3),
("3".to_string(), 3),
("four".to_string(), 4),
("4".to_string(), 4),
("five".to_string(), 5),
("5".to_string(), 5),
("six".to_string(), 6),
("6".to_string(), 6),
("seven".to_string(), 7),
("7".to_string(), 7),
("eight".to_string(), 8),
("8".to_string(), 8),
("nine".to_string(), 9),
("9".to_string(), 9)
]);
for (target, value) in targets {
let found_target: Option<usize> = input_line.find(&target);
let r_found_target: Option<usize> = input_line.rfind(&target);
match found_target {
None => {
},
Some(index) => {
collector.push((value, index as u32));
//println!("Found {} at {}", value, index);
}
}
match r_found_target {
None => {
},
Some(index) => {
collector.push((value, index as u32));
//println!("Rfound {} at {}", value, index);
}
}
}
collector.sort_by_key(|k| k.1);
let mut first_number: String = String::new();
let mut last_number: String = String::new();
// Wacky override
if input_line.chars().last().unwrap().is_numeric() {
last_number = input_line.clone().chars().last().unwrap().to_string();
} else {
last_number = collector.last().unwrap().0.to_string();
}
// Wacky override
if input_line.chars().nth(0).unwrap().is_numeric() {
first_number = input_line.clone().chars().nth(0).unwrap().to_string();
} else {
first_number = collector.first().unwrap().0.to_string();
}
//println!("{:?}", String::from(first_number.to_owned() + &last_number).trim().parse::<u32>().unwrap());
String::from(first_number.to_owned() + &last_number).trim().parse::<u32>().unwrap()
}
fn extract_numbers_from_string(input: String) -> u32 {
let mut nums_in_string: Vec<char> = vec![];
for char in input.chars() {
if char.is_numeric() {
nums_in_string.push(char);
}
}
let char_count = nums_in_string.len();
let number: u32;
if char_count == 1 {
// add chars to string.
let num_string = nums_in_string[0].clone().to_string() + &nums_in_string[0].clone().to_string();
// convert to u32.
number = num_string.trim().parse().unwrap();
} else {
let first_item = nums_in_string[0].clone();
let last_item = nums_in_string.last().copied();
let num_string = first_item.to_string() + &last_item.expect("should be two chars.").to_string();
number = num_string.trim().parse().unwrap()
}
number
}
#[test]
fn test_extract_numbers_from_string() {
let expected: u32 = 74;
let test_value = String::from("742fiveeightnvjjpx4eight");
let actual = extract_numbers_from_string(test_value);
assert_eq!(expected, actual);
}
#[test]
fn test_process() {
let expected: u32 = 54927;
let actual: u32 = process_day_1();
assert_eq!(expected, actual);
}

76
2023/src/day2/mod.rs Normal file
View File

@@ -0,0 +1,76 @@
use crate::utils::read_lines;
const RED_MAX: u32 = 12;
const GREEN_MAX: u32 = 13;
const BLUE_MAX: u32 = 14;
pub fn process() -> u32 {
let file_path = "./input_sources/day2.txt";
let mut total: u32 = 0;
if let Ok(lines) = read_lines(file_path) {
for game in lines.flatten() {
total = total + get_color_minimums(game.clone());
//println!("{:?}", total);
}
}
total
}
fn get_game_id(input_line: String) -> u32 {
let game_section: Vec<&str> = input_line.split(":").collect();
let game_section_split: Vec<&str> = game_section[0].split(" ").collect();
game_section_split[1].trim().parse().unwrap()
}
fn get_color_minimums(game: String) -> u32 {
let split_on_colon: Vec<&str> = game.split(":").collect();
let game_results: Vec<&str> = split_on_colon[1].split(";").collect();
let mut valid_game: bool = true;
let mut total: u32 = 1;
let mut green_min: u32 = 0;
let mut red_min: u32 = 0;
let mut blue_min: u32 = 0;
for game_result in game_results {
let selections: Vec<&str> = game_result.split(",").collect();
for selection in selections {
let split_selection: Vec<&str> = selection.trim().split(" ").collect();
let num_selected: u32 = split_selection[0].trim().parse().unwrap();
let color_selected: &str = split_selection[1].trim();
match color_selected {
"green" => {
if num_selected > green_min {
green_min = num_selected;
}
},
"blue" => {
if num_selected > blue_min {
blue_min = num_selected;
}
},
"red" => {
if num_selected > red_min {
red_min = num_selected;
}
},
&_ => todo!()
}
}
}
if green_min > 0 {
total = green_min * total;
}
if red_min > 0 {
total = red_min * total;
}
if blue_min > 0 {
total = blue_min * total;
}
total
}

51
2023/src/day3/mod.rs Normal file
View File

@@ -0,0 +1,51 @@
use crate::utils::read_lines;
use linecount::count_lines;
use std::io;
use std::io::{BufRead, BufReader};
use std::fs::File;
pub fn process() -> u32 {
let file_path = "./input_sources/day3short.txt";
let mut total: u32 = 0;
let mut lines_vec: Vec<String> = vec![];
if let Ok(lines) = read_lines(file_path) {
for line in lines.flatten() {
lines_vec.push(line);
}
}
let length: usize = lines_vec.len();
let mut i = 0;
while i < length {
let cur_line = &lines_vec[i];
println!("{:?}", &lines_vec[i]);
if i < 9 {
let next_line = &lines_vec[i + 1];
}
if i > 0 {
let prev_line = &lines_vec[i - 1];
println!("Prev line is {:?}", prev_line);
}
i = i + 1;
}
total
}
fn handle_line(input_line: String) -> String {
// Get previous line.
// Get next line.
// On current line, iterate over each char
// build digits and their locations.
// For each digit
// go to prior line, check if symbol in (place, place +1, place -1)
// do the same for the next line.
// If a symbol is found, return true for the number
// Sum numbers
// println!("{:?}", input_line);
String::from("Hello")
}

18
2023/src/main.rs Normal file
View File

@@ -0,0 +1,18 @@
#![allow(warnings)]
mod day1;
mod day2;
mod day3;
mod utils;
fn main() {
let day1_results = day1::process();
let day2_results = day2::process();
let day3_results = day3::process();
println!("The Answer to Day 1 is: {:?}", day1_results);
println!("The Answer to Day 2 is: {:?}", day2_results);
println!("The Answer to Day 3 is: {:?}", day3_results);
}

10
2023/src/utils/mod.rs Normal file
View File

@@ -0,0 +1,10 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}