Skip to content

Commit

Permalink
Fix clippy problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberacda committed Dec 3, 2023
1 parent 60c1d84 commit 87853c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 45 deletions.
5 changes: 2 additions & 3 deletions day02/src/bin/day02_part1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::Error;
use regex::Regex;
use std::str::FromStr;

Expand All @@ -24,7 +23,7 @@ fn parse_input(inputs: Vec<String>) -> Vec<Game> {
let result: Vec<Game> = 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)
Expand All @@ -36,7 +35,7 @@ fn parse_input(inputs: Vec<String>) -> Vec<Game> {
.unwrap();

let rounds: Vec<Round> = rounds_str
.split_terminator(";")
.split_terminator(';')
.map(|x| {
let red_cubes: u64 = match red_re.captures(x) {
None => 0,
Expand Down
19 changes: 3 additions & 16 deletions day02/src/bin/day02_part2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::Error;
use regex::Regex;
use std::str::FromStr;

Expand All @@ -11,32 +10,21 @@ struct Round {

#[derive(Default)]
struct Game {
id: u64,
rounds: Vec<Round>,
}

fn parse_input(inputs: Vec<String>) -> Vec<Game> {
let game_re = Regex::new(r"^Game\s(\d+)$").unwrap();
let red_re = Regex::new(r"(?P<red>\d+)\sred").unwrap();
let green_re = Regex::new(r"(?P<green>\d+)\sgreen").unwrap();
let blue_re = Regex::new(r"(?P<blue>\d+)\sblue").unwrap();

let result: Vec<Game> = 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<Round> = rounds_str
.split_terminator(";")
.split_terminator(';')
.map(|x| {
let red_cubes: u64 = match red_re.captures(x) {
None => 0,
Expand All @@ -60,7 +48,6 @@ fn parse_input(inputs: Vec<String>) -> Vec<Game> {
.collect();

Game {
id: game_id,
rounds,
}
})
Expand All @@ -74,7 +61,7 @@ fn get_result(games: Vec<Game>) -> 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
}
Expand Down
27 changes: 12 additions & 15 deletions day03/src/bin/day03_part1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ fn get_number_positions(input: &str) -> (Vec<Number>, Vec<Symbol>) {
}
}

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,
Expand All @@ -70,7 +69,7 @@ fn get_number_positions(input: &str) -> (Vec<Number>, Vec<Symbol>) {
(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 {
Expand All @@ -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<Symbol>) -> Vec<(usize, usize)> {
Expand All @@ -96,18 +95,16 @@ fn get_symbol_covered_positions(symbols: Vec<Symbol>) -> 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)]);

Expand Down Expand Up @@ -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);
Expand All @@ -150,15 +147,15 @@ 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);
}

#[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);
Expand Down
19 changes: 8 additions & 11 deletions day03/src/bin/day03_part2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ fn get_positions(input: &str) -> (Vec<Number>, Vec<Gear>) {
}
}

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,
Expand All @@ -70,7 +69,7 @@ fn get_positions(input: &str) -> (Vec<Number>, Vec<Gear>) {
(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 {
Expand All @@ -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)]);

Expand Down

0 comments on commit 87853c3

Please sign in to comment.