diff --git a/rust/src/solutions/day_01.rs b/rust/src/solutions/day_01.rs index d254f89..d98ed9a 100644 --- a/rust/src/solutions/day_01.rs +++ b/rust/src/solutions/day_01.rs @@ -1,6 +1,6 @@ //use web_sys::console; -use wasm_bindgen::prelude::*; use regex::Regex; +use wasm_bindgen::prelude::*; #[wasm_bindgen(module = "/src/solutions/workerHelpers.js")] extern "C" { @@ -8,19 +8,22 @@ extern "C" { } pub fn solution_part_1() -> () { - postMessageToWorker(true, "Part 1: Concatenate the first and last digits found in a string.\n"); + postMessageToWorker( + true, + "Part 1: Concatenate the first and last digits found in a string.\n", + ); let mut iteration = -1; // let content = include_str!("input/day_01_part_1_test_input.txt"); let content = include_str!("input/day_01_input.txt"); - let is_digit_regex = Regex::new(r"\d").unwrap(); + let is_digit_regex = Regex::new(r"\d").unwrap(); let mut codes: Vec = Vec::new(); content.lines().for_each(|line| { // Provide a mechanism to limit the volume of output on the console. iteration += 1; let mut show_message = false; - if (iteration) % 300 == 0 { + if (iteration) % 300 == 0 { show_message = true; } @@ -31,7 +34,10 @@ pub fn solution_part_1() -> () { } postMessageToWorker(show_message, " "); - postMessageToWorker(show_message, &format!("Iteration: {}, input: {}", iteration, line)); + postMessageToWorker( + show_message, + &format!("Iteration: {}, input: {}", iteration, line), + ); let mut first_digit: Option<&char> = None; let mut last_digit: Option<&char> = None; @@ -56,21 +62,23 @@ pub fn solution_part_1() -> () { postMessageToWorker(true, &format!("⭐️ sum: {}", sum)); } - pub fn solution_part_2() -> () { - postMessageToWorker(true, "Part 2: Concatenate the first and last digits found in a string.\n"); + postMessageToWorker( + true, + "Part 2: Concatenate the first and last digits found in a string.\n", + ); let mut iteration = -1; // let content = include_str!("input/day_01_part_2_test_input.txt"); let content = include_str!("input/day_01_input.txt"); - let is_digit_regex = Regex::new(r"\d").unwrap(); + let is_digit_regex = Regex::new(r"\d").unwrap(); let mut codes: Vec = Vec::new(); content.lines().for_each(|line| { // Provide a mechanism to limit the volume of output on the console. iteration += 1; let mut show_message = false; - if (iteration) % 300 == 0 { + if (iteration) % 300 == 0 { show_message = true; } @@ -81,12 +89,17 @@ pub fn solution_part_2() -> () { } postMessageToWorker(show_message, " "); - postMessageToWorker(show_message, &format!("Iteration: {}, input: {}", iteration, line)); + postMessageToWorker( + show_message, + &format!("Iteration: {}, input: {}", iteration, line), + ); let mut first_digit: Option = None; let mut last_digit: Option = None; - let numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; + let numbers = [ + "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + ]; for (character_index, character) in characters.iter().enumerate() { if is_digit_regex.is_match(character.to_string().as_str()) { @@ -94,12 +107,18 @@ pub fn solution_part_2() -> () { let temp_string = character.to_string(); let char = temp_string.chars().nth(0).unwrap(); first_digit = Some(char); - postMessageToWorker(show_message, &format!("New first digit from numeric character: {}", character)); + postMessageToWorker( + show_message, + &format!("New first digit from numeric character: {}", character), + ); } let temp_string = character.to_string(); let char = temp_string.chars().nth(0).unwrap(); last_digit = Some(char); - postMessageToWorker(show_message, &format!("New last digit from numeric character: {}", character)); + postMessageToWorker( + show_message, + &format!("New last digit from numeric character: {}", character), + ); } for (number_index, number) in numbers.iter().enumerate() { let length = number.len(); @@ -107,18 +126,28 @@ pub fn solution_part_2() -> () { // postMessageToWorker(show_message, &format!("number: {}, slice: {}", number, slice)); if &slice == number { let found_scalar = number_index + 1; - + if first_digit.is_none() { let temp_string = found_scalar.to_string(); let char = temp_string.chars().nth(0).unwrap(); first_digit = Some(char); - postMessageToWorker(show_message, &format!("Found first digit: {}", first_digit.unwrap())); + postMessageToWorker( + show_message, + &format!("Found first digit: {}", first_digit.unwrap()), + ); } let temp_string = found_scalar.to_string(); let char = temp_string.chars().nth(0).unwrap(); last_digit = Some(char); - postMessageToWorker(show_message, &format!("New last digit from number ({}): {}", number, last_digit.unwrap())); + postMessageToWorker( + show_message, + &format!( + "New last digit from number ({}): {}", + number, + last_digit.unwrap() + ), + ); } } } @@ -134,10 +163,14 @@ pub fn solution_part_2() -> () { fn get_string_slice(input: &str, start: usize, chars: usize) -> &str { let start_index = input.char_indices().nth(start); - let end_index = input.char_indices().nth(start + chars).unwrap_or((input.len(), ' ')).0; + let end_index = input + .char_indices() + .nth(start + chars) + .unwrap_or((input.len(), ' ')) + .0; match start_index { Some((start_idx, _)) => &input[start_idx..end_index], - None => "" + None => "", } } diff --git a/rust/src/solutions/day_02.rs b/rust/src/solutions/day_02.rs index 7b75b91..d6be64d 100644 --- a/rust/src/solutions/day_02.rs +++ b/rust/src/solutions/day_02.rs @@ -1,8 +1,8 @@ //use web_sys::console; #![allow(unreachable_code)] -use wasm_bindgen::prelude::*; use regex::Regex; use std::collections::HashMap; +use wasm_bindgen::prelude::*; #[wasm_bindgen(module = "/src/solutions/workerHelpers.js")] extern "C" { @@ -24,7 +24,7 @@ pub fn solution_part_1() -> () { // Provide a mechanism to limit the volume of output on the console. iteration += 1; let mut show_message = false; - if (iteration) % 30 == 0 { + if (iteration) % 30 == 0 { show_message = true; } @@ -35,19 +35,21 @@ pub fn solution_part_1() -> () { } postMessageToWorker(show_message, " "); - postMessageToWorker(show_message, &format!("Iteration: {}, input: {}", iteration, line)); + postMessageToWorker( + show_message, + &format!("Iteration: {}, input: {}", iteration, line), + ); let mut proposed_setup = HashMap::new(); proposed_setup.insert("red", 12); proposed_setup.insert("green", 13); proposed_setup.insert("blue", 14); - if let Some(caps) = input_chunks.captures(line) { postMessageToWorker(show_message, &format!("")); let game = caps.get(1).map_or("", |m| m.as_str()); let game_int = game.parse::().expect("Should be able to parse game"); - + let mut demonstration_knowledge = HashMap::new(); // postMessageToWorker(show_message, &format!("game: {}", game)); @@ -64,35 +66,48 @@ pub fn solution_part_1() -> () { // added .expect()s with copilot here if demonstration_knowledge.contains_key(color) { - let current_count = *demonstration_knowledge.get(color).expect("Value must exist since key exists"); - let parsed_count = count.parse::().expect("Should be able to parse count"); + let current_count = *demonstration_knowledge + .get(color) + .expect("Value must exist since key exists"); + let parsed_count = + count.parse::().expect("Should be able to parse count"); if parsed_count > current_count { demonstration_knowledge.insert(color, parsed_count); } } else { - demonstration_knowledge.insert(color, count.parse::().expect("Should be able to parse count")); + demonstration_knowledge.insert( + color, + count.parse::().expect("Should be able to parse count"), + ); } - } } } - postMessageToWorker(show_message, &format!("demonstration_knowledge: {:?}", demonstration_knowledge)); - let is_possible = is_possible_game(&proposed_setup, &demonstration_knowledge); - if is_possible { - possible_games.push(game_int) - } - postMessageToWorker(show_message, &format!("is possible: {:?}", is_possible)); + postMessageToWorker( + show_message, + &format!("demonstration_knowledge: {:?}", demonstration_knowledge), + ); + let is_possible = is_possible_game(&proposed_setup, &demonstration_knowledge); + if is_possible { + possible_games.push(game_int) + } + postMessageToWorker(show_message, &format!("is possible: {:?}", is_possible)); } }); let sum: u32 = possible_games.iter().sum(); postMessageToWorker(true, &format!("⭐️ sum: {}", sum)); } -fn is_possible_game(proposed_setup: &HashMap<&str, u32>, demonstration_knowledge: &HashMap<&str, u32>) -> bool { +fn is_possible_game( + proposed_setup: &HashMap<&str, u32>, + demonstration_knowledge: &HashMap<&str, u32>, +) -> bool { let mut is_possible = true; for (color, count) in proposed_setup { if demonstration_knowledge.contains_key(color) { - let demonstration_count = *demonstration_knowledge.get(color).expect("Value must exist since key exists"); + let demonstration_count = *demonstration_knowledge + .get(color) + .expect("Value must exist since key exists"); if demonstration_count > *count { is_possible = false; } @@ -103,8 +118,6 @@ fn is_possible_game(proposed_setup: &HashMap<&str, u32>, demonstration_knowledge is_possible } - - pub fn solution_part_2() -> () { postMessageToWorker(true, "Part 2: Minimize valid case.\n"); let mut iteration = -1; @@ -120,7 +133,7 @@ pub fn solution_part_2() -> () { // Provide a mechanism to limit the volume of output on the console. iteration += 1; let mut show_message = false; - if (iteration) % 30 == 0 { + if (iteration) % 30 == 0 { show_message = true; } @@ -131,11 +144,14 @@ pub fn solution_part_2() -> () { } postMessageToWorker(show_message, " "); - postMessageToWorker(show_message, &format!("Iteration: {}, input: {}", iteration, line)); + postMessageToWorker( + show_message, + &format!("Iteration: {}, input: {}", iteration, line), + ); if let Some(caps) = input_chunks.captures(line) { postMessageToWorker(show_message, &format!("")); - + let mut demonstration_knowledge = HashMap::new(); // postMessageToWorker(show_message, &format!("game: {}", game)); @@ -152,28 +168,36 @@ pub fn solution_part_2() -> () { // added .expect()s with copilot here if demonstration_knowledge.contains_key(color) { - let current_count = *demonstration_knowledge.get(color).expect("Value must exist since key exists"); - let parsed_count = count.parse::().expect("Should be able to parse count"); + let current_count = *demonstration_knowledge + .get(color) + .expect("Value must exist since key exists"); + let parsed_count = + count.parse::().expect("Should be able to parse count"); if parsed_count > current_count { demonstration_knowledge.insert(color, parsed_count); } } else { - demonstration_knowledge.insert(color, count.parse::().expect("Should be able to parse count")); + demonstration_knowledge.insert( + color, + count.parse::().expect("Should be able to parse count"), + ); } - } } } - postMessageToWorker(show_message, &format!("demonstration_knowledge: {:?}", demonstration_knowledge)); - let mut game_power = 1; - for (_color, count) in demonstration_knowledge { - game_power = game_power * count; + postMessageToWorker( + show_message, + &format!("demonstration_knowledge: {:?}", demonstration_knowledge), + ); + let mut game_power = 1; + for (_color, count) in demonstration_knowledge { + game_power = game_power * count; } - game_powers.push(game_power); - postMessageToWorker(show_message, &format!("game_power: {:?}", game_power)); + game_powers.push(game_power); + postMessageToWorker(show_message, &format!("game_power: {:?}", game_power)); } }); let sum: u32 = game_powers.iter().sum(); postMessageToWorker(true, &format!("⭐️ sum: {}", sum)); -} \ No newline at end of file +} diff --git a/rust/src/solutions/day_03.rs b/rust/src/solutions/day_03.rs index 6a760cb..e9e2844 100644 --- a/rust/src/solutions/day_03.rs +++ b/rust/src/solutions/day_03.rs @@ -77,7 +77,10 @@ pub fn solution_part_1() -> () { postMessageToWorker(true, &format!("⭐️ part number sum: {}", sum)); } -fn calculate_sum_of_symbol_adjacent_parts(schematic: &Vec>, part_numbers: &mut Vec) { +fn calculate_sum_of_symbol_adjacent_parts( + schematic: &Vec>, + part_numbers: &mut Vec, +) { let mut symbols_found: u32 = 0; for (i, row) in schematic.iter().enumerate() { for (j, element) in row.iter().enumerate() { @@ -91,10 +94,13 @@ fn calculate_sum_of_symbol_adjacent_parts(schematic: &Vec> } // ! huge help: there are never symbols along the outside of the schematic - + postMessageToWorker( show_message, - &format!("Found a symbol, #{} to work '{}' at ({}, {})", symbols_found, symbol, i, j), + &format!( + "Found a symbol, #{} to work '{}' at ({}, {})", + symbols_found, symbol, i, j + ), ); symbols_found += 1; @@ -134,12 +140,14 @@ fn calculate_sum_of_symbol_adjacent_parts(schematic: &Vec> SchematicElement::Number(value) => { postMessageToWorker( show_message, - &format!("Found a neighbor number '{}' at ({}, {})", value, i, j), + &format!( + "Found a neighbor number '{}' at ({}, {})", + value, i, j + ), ); part_numbers.push(*value); - } - _ => { } // don't care about other things we find here + _ => {} // don't care about other things we find here } } } @@ -148,7 +156,6 @@ fn calculate_sum_of_symbol_adjacent_parts(schematic: &Vec> } } - fn parse_schematic_line( schematic: &mut Vec>, line_number: i32, @@ -259,7 +266,7 @@ pub fn solution_part_2() -> () { }); //postMessageToWorker(true, &format!("Schematic: {:?}", schematic)); - let mut gear_ratios : Vec = Vec::new(); + let mut gear_ratios: Vec = Vec::new(); calculate_sum_of_gear_ratios(&schematic, &mut gear_ratios); @@ -267,7 +274,10 @@ pub fn solution_part_2() -> () { postMessageToWorker(true, &format!("⭐️ gear ratio sum: {}", sum)); } -fn calculate_sum_of_gear_ratios(schematic: &Vec>, gear_ratios: &mut Vec) { +fn calculate_sum_of_gear_ratios( + schematic: &Vec>, + gear_ratios: &mut Vec, +) { let mut symbols_found: u32 = 0; for (i, row) in schematic.iter().enumerate() { for (j, element) in row.iter().enumerate() { @@ -280,7 +290,7 @@ fn calculate_sum_of_gear_ratios(schematic: &Vec>, gear_rat // only care about the gear symbols continue; } - + let mut show_message = false; if symbols_found % 100 == 0 { show_message = true; @@ -288,7 +298,10 @@ fn calculate_sum_of_gear_ratios(schematic: &Vec>, gear_rat postMessageToWorker( show_message, - &format!("Found a symbol, #{} to work '{}' at ({}, {})", symbols_found, symbol, i, j), + &format!( + "Found a symbol, #{} to work '{}' at ({}, {})", + symbols_found, symbol, i, j + ), ); symbols_found += 1; @@ -326,14 +339,18 @@ fn calculate_sum_of_gear_ratios(schematic: &Vec>, gear_rat SchematicElement::Number(value) => { postMessageToWorker( show_message, - &format!("Found a neighbor number '{}' at ({}, {})", value, i, j), + &format!( + "Found a neighbor number '{}' at ({}, {})", + value, i, j + ), ); neighbor_numbers.push(*value); } - _ => { } // don't care about other things we find here + _ => {} // don't care about other things we find here } } - if neighbor_numbers.len() == 2 { // some gears only touch one number + if neighbor_numbers.len() == 2 { + // some gears only touch one number let product: u32 = neighbor_numbers.iter().product(); postMessageToWorker( show_message,