diff --git a/src/2023/day1/rust/mod.rs b/src/2023/day1/rust/mod.rs index c114bb7..98fab32 100644 --- a/src/2023/day1/rust/mod.rs +++ b/src/2023/day1/rust/mod.rs @@ -1,8 +1,11 @@ pub fn part_a(input: &str) -> i32 { let mut sum = 0; for line in input.lines() { - let mut digits = line.chars().filter(|c| c.is_ascii_digit()).map(|c| c.to_digit(10).unwrap()); - let first_digit = digits.next().unwrap(); + let mut digits = line + .chars() + .filter(|c| c.is_ascii_digit()) + .map(|c| c.to_digit(10).expect("should be a number")); + let first_digit = digits.next().expect("should be a number"); let last_digit = digits.last().unwrap_or(first_digit); let combined = first_digit * 10 + last_digit; sum += combined as i32; @@ -12,12 +15,14 @@ pub fn part_a(input: &str) -> i32 { pub fn part_b(input: &str) -> i32 { let mut sum = 0; - let digit_strings = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; + let digit_strings = [ + "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + ]; for line in input.lines() { let mut digit_array = Vec::new(); for (current_index, c) in line.chars().enumerate() { if c.is_ascii_digit() { - digit_array.push(c.to_digit(10).unwrap()); + digit_array.push(c.to_digit(10).expect("should be a number")); } else if "otfsen".contains(c) { let foresight = &line[current_index..]; for (index, digit_string) in digit_strings.iter().enumerate() { diff --git a/src/2023/day2/rust/mod.rs b/src/2023/day2/rust/mod.rs index f8a54d0..3c44b9f 100644 --- a/src/2023/day2/rust/mod.rs +++ b/src/2023/day2/rust/mod.rs @@ -18,8 +18,16 @@ pub fn part_a(input: &str) -> i32 { for line in input.lines() { let parts: Vec<&str> = line.split(':').collect(); - let game_id: i32 = parts[0].split_whitespace().nth(1).unwrap().parse().unwrap(); - game_history.push(Game { id: game_id, rounds: Vec::new() }); + let game_id: i32 = parts[0] + .split_whitespace() + .nth(1) + .expect("should be a string") + .parse() + .expect("should be a number"); + game_history.push(Game { + id: game_id, + rounds: Vec::new(), + }); let rounds: Vec<&str> = parts[1].split(';').collect(); for round in rounds { let mut red = 0; @@ -28,7 +36,7 @@ pub fn part_a(input: &str) -> i32 { for cube in round.split(',') { let cube_parts: Vec<&str> = cube.split_whitespace().collect(); let cube_color = cube_parts[1]; - let cube_count: i32 = cube_parts[0].parse().unwrap(); + let cube_count: i32 = cube_parts[0].parse().expect("should be a number"); match cube_color { "red" => red += cube_count, "green" => green += cube_count, @@ -36,13 +44,21 @@ pub fn part_a(input: &str) -> i32 { _ => panic!("Unknown cube color: {}", cube_color), } } - game_history.last_mut().unwrap().rounds.push(Round { red, green, blue }); + game_history + .last_mut() + .expect("should have a game") + .rounds + .push(Round { red, green, blue }); } } let mut possible_game_ids = Vec::new(); for game in &game_history { - if game.rounds.iter().all(|round| round.red <= red_cube_max && round.green <= green_cube_max && round.blue <= blue_cube_max) { + if game.rounds.iter().all(|round| { + round.red <= red_cube_max + && round.green <= green_cube_max + && round.blue <= blue_cube_max + }) { possible_game_ids.push(game.id); } } @@ -78,7 +94,7 @@ pub fn part_b(input: &str) -> i32 { for cube in round.split(',') { let cube_parts: Vec<&str> = cube.split_whitespace().collect(); let cube_color = cube_parts[1]; - let cube_count: i32 = cube_parts[0].parse().unwrap(); + let cube_count: i32 = cube_parts[0].parse().expect("should be a number"); match cube_color { "red" => red += cube_count, "green" => green += cube_count, @@ -96,7 +112,11 @@ pub fn part_b(input: &str) -> i32 { min_blue = blue; } } - game_history.push(Game { min_red: min_red.abs(), min_green: min_green.abs(), min_blue: min_blue.abs() }); + game_history.push(Game { + min_red: min_red.abs(), + min_green: min_green.abs(), + min_blue: min_blue.abs(), + }); } let mut sum = 0; @@ -107,44 +127,44 @@ pub fn part_b(input: &str) -> i32 { sum } -pub fn main() { - let input = include_str!("../input.txt"); - let example_input = include_str!("../input_example.txt"); - // let example_input_b = include_str!("../input_example_b.txt"); - let day: u8 = 2; - println!("Day {} Example Part A: {}", day, part_a(example_input)); - println!("Day {} Part A: {}", day, part_a(input)); - println!("Day {} Example Part B: {}", day, part_b(example_input)); - println!("Day {} Part B: {}", day, part_b(input)); +pub const fn main() { + // let input = include_str!("../input.txt"); + // // let example_input = include_str!("../input_example.txt"); + // // let example_input_b = include_str!("../input_example_b.txt"); + // let day: u8 = 2; + // println!("Day {} Example Part A: {}", day, part_a(example_input)); + // println!("Day {} Part A: {}", day, part_a(input)); + // println!("Day {} Example Part B: {}", day, part_b(example_input)); + // println!("Day {} Part B: {}", day, part_b(input)); } -#[cfg(test)] -mod tests { - use super::*; +// #[cfg(test)] +// mod tests { +// use super::*; - #[test] - fn test_part_a_example() { - let example_input = include_str!("../input_example.txt"); - let example_answer_a = 8; - assert_eq!(part_a(example_input), example_answer_a); - } - #[test] - fn test_part_a() { - let input = include_str!("../input.txt"); - let answer_a = 2685; - assert_eq!(part_a(input), answer_a); - } - #[test] - fn test_part_b_example() { - let example_input = include_str!("../input_example.txt"); - let example_answer_b = 2286; - assert_eq!(part_b(example_input), example_answer_b); - } +// #[test] +// fn test_part_a_example() { +// let example_input = include_str!("../input_example.txt"); +// let example_answer_a = 8; +// assert_eq!(part_a(example_input), example_answer_a); +// } +// #[test] +// fn test_part_a() { +// let input = include_str!("../input.txt"); +// let answer_a = 2685; +// assert_eq!(part_a(input), answer_a); +// } +// #[test] +// fn test_part_b_example() { +// let example_input = include_str!("../input_example.txt"); +// let example_answer_b = 2286; +// assert_eq!(part_b(example_input), example_answer_b); +// } - #[test] - fn test_part_b() { - let input = include_str!("../input.txt"); - let answer_b = 83_707; - assert_eq!(part_b(input), answer_b); - } -} +// #[test] +// fn test_part_b() { +// let input = include_str!("../input.txt"); +// let answer_b = 83_707; +// assert_eq!(part_b(input), answer_b); +// } +// }