diff --git a/day02/src/bin/day02_part1.rs b/day02/src/bin/day02_part1.rs index 328d22d..9e1a046 100644 --- a/day02/src/bin/day02_part1.rs +++ b/day02/src/bin/day02_part1.rs @@ -1,4 +1,3 @@ -use anyhow::Error; use regex::Regex; use std::str::FromStr; @@ -24,7 +23,7 @@ fn parse_input(inputs: Vec) -> Vec { let result: Vec = inputs .iter() .map(|input| { - let (game_id_str, rounds_str) = input.split_once(":").unwrap(); + let (game_id_str, rounds_str) = input.split_once(':').unwrap(); let game_id = u64::from_str( game_re .captures(game_id_str) @@ -36,7 +35,7 @@ fn parse_input(inputs: Vec) -> Vec { .unwrap(); let rounds: Vec = rounds_str - .split_terminator(";") + .split_terminator(';') .map(|x| { let red_cubes: u64 = match red_re.captures(x) { None => 0, diff --git a/day02/src/bin/day02_part2.rs b/day02/src/bin/day02_part2.rs index 5f2373c..b9be256 100644 --- a/day02/src/bin/day02_part2.rs +++ b/day02/src/bin/day02_part2.rs @@ -1,4 +1,3 @@ -use anyhow::Error; use regex::Regex; use std::str::FromStr; @@ -11,12 +10,10 @@ struct Round { #[derive(Default)] struct Game { - id: u64, rounds: Vec, } fn parse_input(inputs: Vec) -> Vec { - let game_re = Regex::new(r"^Game\s(\d+)$").unwrap(); let red_re = Regex::new(r"(?P\d+)\sred").unwrap(); let green_re = Regex::new(r"(?P\d+)\sgreen").unwrap(); let blue_re = Regex::new(r"(?P\d+)\sblue").unwrap(); @@ -24,19 +21,10 @@ fn parse_input(inputs: Vec) -> Vec { let result: Vec = inputs .iter() .map(|input| { - let (game_id_str, rounds_str) = input.split_once(":").unwrap(); - let game_id = u64::from_str( - game_re - .captures(game_id_str) - .unwrap() - .get(1) - .unwrap() - .as_str(), - ) - .unwrap(); + let (_, rounds_str) = input.split_once(':').unwrap(); let rounds: Vec = rounds_str - .split_terminator(";") + .split_terminator(';') .map(|x| { let red_cubes: u64 = match red_re.captures(x) { None => 0, @@ -60,7 +48,6 @@ fn parse_input(inputs: Vec) -> Vec { .collect(); Game { - id: game_id, rounds, } }) @@ -74,7 +61,7 @@ fn get_result(games: Vec) -> u64 { let max_red = game.rounds.iter().map(|x| x.red).max().unwrap(); let max_green = game.rounds.iter().map(|x| x.green).max().unwrap(); let max_blue = game.rounds.iter().map(|x| x.blue).max().unwrap(); - result += (max_red * max_green * max_blue); + result += max_red * max_green * max_blue; } result } diff --git a/day03/src/bin/day03_part1.rs b/day03/src/bin/day03_part1.rs index 4b641e9..118874c 100644 --- a/day03/src/bin/day03_part1.rs +++ b/day03/src/bin/day03_part1.rs @@ -53,8 +53,7 @@ fn get_number_positions(input: &str) -> (Vec, Vec) { } } - if current_number_start_index.is_some() { - let start_index = current_number_start_index.unwrap(); + if let Some(start_index) = current_number_start_index { let number = u64::from_str(current_number_digits.as_str()).unwrap(); numbers.push(Number { y: line_index, @@ -70,7 +69,7 @@ fn get_number_positions(input: &str) -> (Vec, Vec) { (numbers, symbols) } -fn is_symbol_neighbor(number: &Number, symbol_covered_positions: &Vec<(usize, usize)>) -> bool { +fn is_symbol_neighbor(number: &Number, symbol_covered_positions: &[(usize, usize)]) -> bool { for idx in number.start_x..number.end_x + 1 { let index_covered = symbol_covered_positions.contains(&(idx, number.y)); if !index_covered { @@ -79,7 +78,7 @@ fn is_symbol_neighbor(number: &Number, symbol_covered_positions: &Vec<(usize, us return true; } } - return false; + false } fn get_symbol_covered_positions(symbols: Vec) -> Vec<(usize, usize)> { @@ -96,18 +95,16 @@ fn get_symbol_covered_positions(symbols: Vec) -> Vec<(usize, usize)> { let idy_0 = x.line; let idy_p1 = x.line + 1; - if opt_idy_n1.is_some() { - let idy_n1 = opt_idy_n1.unwrap(); + if let Some(idy_n1) = opt_idy_n1 { results.extend_from_slice(&[(idx_0, idy_n1), (idx_p1, idy_n1)]); } - if opt_idx_n1.is_some() { - let idx_n1 = opt_idx_n1.unwrap(); + if let Some(idx_n1) = opt_idx_n1 { results.extend_from_slice(&[(idx_n1, idy_0), (idx_n1, idy_p1)]); } - if opt_idx_n1.is_some() && opt_idy_n1.is_some() { - let idy_n1 = opt_idy_n1.unwrap(); - let idx_n1 = opt_idx_n1.unwrap(); - results.push((idx_n1, idy_n1)); + if let Some(idx_n1) = opt_idx_n1 { + if let Some(idy_n1) = opt_idy_n1 { + results.push((idx_n1, idy_n1)); + } } results.extend_from_slice(&[(idx_0, idy_p1), (idx_p1, idy_p1), (idx_p1, idy_0)]); @@ -139,7 +136,7 @@ mod tests { #[test] fn test_input01() { - let input = include_str!("../resources/test_input_01.txt"); + let input = include_str!("../../resources/test_input_01.txt"); let (numbers, symbols) = get_number_positions(input); assert_eq!(numbers.len(), 10); assert_eq!(symbols.len(), 6); @@ -150,7 +147,7 @@ mod tests { #[test] fn test_input02() { - let input = include_str!("../resources/test_input_02.txt"); + let input = include_str!("../../resources/test_input_02.txt"); let (numbers, symbols) = get_number_positions(input); let result = get_result(numbers, symbols); assert_eq!(result, 413); @@ -158,7 +155,7 @@ mod tests { #[test] fn test_input03() { - let input = include_str!("../resources/test_input_03.txt"); + let input = include_str!("../../resources/test_input_03.txt"); let (numbers, symbols) = get_number_positions(input); assert_eq!(symbols.len(), 9); assert_eq!(numbers.len(), 19); diff --git a/day03/src/bin/day03_part2.rs b/day03/src/bin/day03_part2.rs index 2c230ff..1546c07 100644 --- a/day03/src/bin/day03_part2.rs +++ b/day03/src/bin/day03_part2.rs @@ -53,8 +53,7 @@ fn get_positions(input: &str) -> (Vec, Vec) { } } - if current_number_start_index.is_some() { - let start_index = current_number_start_index.unwrap(); + if let Some(start_index) = current_number_start_index { let number = u64::from_str(current_number_digits.as_str()).unwrap(); numbers.push(Number { y: line_index, @@ -70,7 +69,7 @@ fn get_positions(input: &str) -> (Vec, Vec) { (numbers, gears) } -fn is_gear_neighbor(number: &Number, gear_covered_positions: &Vec<(usize, usize)>) -> bool { +fn is_gear_neighbor(number: &Number, gear_covered_positions: &[(usize, usize)]) -> bool { for idx in number.start_x..number.end_x + 1 { let index_covered = gear_covered_positions.contains(&(idx, number.y)); if !index_covered { @@ -93,18 +92,16 @@ fn get_gear_covered_positions(gear: &Gear) -> Vec<(usize, usize)> { let idy_0 = gear.y; let idy_p1 = gear.y + 1; - if opt_idy_n1.is_some() { - let idy_n1 = opt_idy_n1.unwrap(); + if let Some(idy_n1) = opt_idy_n1 { results.extend_from_slice(&[(idx_0, idy_n1), (idx_p1, idy_n1)]); } - if opt_idx_n1.is_some() { - let idx_n1 = opt_idx_n1.unwrap(); + if let Some(idx_n1) = opt_idx_n1 { results.extend_from_slice(&[(idx_n1, idy_0), (idx_n1, idy_p1)]); } - if opt_idx_n1.is_some() && opt_idy_n1.is_some() { - let idy_n1 = opt_idy_n1.unwrap(); - let idx_n1 = opt_idx_n1.unwrap(); - results.push((idx_n1, idy_n1)); + if let Some(idy_n1) = opt_idy_n1 { + if let Some(idx_n1) = opt_idx_n1 { + results.push((idx_n1, idy_n1)); + } } results.extend_from_slice(&[(idx_0, idy_p1), (idx_p1, idy_p1), (idx_p1, idy_0)]);