From 5233b40c3a644fa7a36ac59c8d658c937a5ec828 Mon Sep 17 00:00:00 2001 From: MelKam Date: Tue, 6 Dec 2022 09:44:54 +0000 Subject: [PATCH] add pointers to function arguments --- 2022/day_2/rust/src/main.rs | 16 ++++++++-------- 2022/day_3/rust/src/main.rs | 25 +++++++++++++------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/2022/day_2/rust/src/main.rs b/2022/day_2/rust/src/main.rs index eb324a1..e3ba854 100644 --- a/2022/day_2/rust/src/main.rs +++ b/2022/day_2/rust/src/main.rs @@ -53,9 +53,9 @@ fn parse_game_result(string_game_result: &str) -> GameResult { } } -fn get_player_game_score(opponent_move: MoveType, player_move: MoveType) -> i32 { +fn get_player_game_score(opponent_move: &MoveType, player_move: &MoveType) -> i32 { let mut score: i32 = 0; - let player_move_score = player_move as i32; + let player_move_score = *player_move as i32; if opponent_move == player_move { // Draw @@ -63,7 +63,7 @@ fn get_player_game_score(opponent_move: MoveType, player_move: MoveType) -> i32 return score; } - if ((opponent_move as i32) - player_move_score).rem_euclid(MOVE_COUNTS) == 1 { + if ((*opponent_move as i32) - player_move_score).rem_euclid(MOVE_COUNTS) == 1 { // Lose score += GameResult::Lose.get_score() + player_move_score; } else { @@ -74,12 +74,12 @@ fn get_player_game_score(opponent_move: MoveType, player_move: MoveType) -> i32 return score; } -fn get_move_by_game_result(opponent_move: MoveType, expected_game_result: GameResult) -> MoveType { +fn get_move_by_game_result(opponent_move: &MoveType, expected_game_result: GameResult) -> MoveType { if expected_game_result == GameResult::Draw { - return opponent_move; + return *opponent_move; }; - let num = expected_game_result as i32 - opponent_move as i32; + let num = expected_game_result as i32 - *opponent_move as i32; if num == 0 { return MoveType::from(MOVE_COUNTS); @@ -98,9 +98,9 @@ fn main() { }; let opponent_move = parse_move(opponent); - let player_move = get_move_by_game_result(opponent_move, parse_game_result(game_result)); + let player_move = get_move_by_game_result(&opponent_move, parse_game_result(game_result)); - return get_player_game_score(opponent_move, player_move); + return get_player_game_score(&opponent_move, &player_move); }) .sum::(); diff --git a/2022/day_3/rust/src/main.rs b/2022/day_3/rust/src/main.rs index 1d77d2d..67ec289 100644 --- a/2022/day_3/rust/src/main.rs +++ b/2022/day_3/rust/src/main.rs @@ -27,7 +27,7 @@ fn get_appear_arr(string: &str) -> AppearCharArray { return store; } -fn get_appear_sum(unique_chars_arrays: Vec) -> [u8; CHARS_COUNT] { +fn get_appear_sum(unique_chars_arrays: &Vec) -> [u8; CHARS_COUNT] { let mut result: [u8; CHARS_COUNT] = [0; 52]; for index in 0..CHARS_COUNT { @@ -43,11 +43,11 @@ fn get_appear_sum(unique_chars_arrays: Vec) -> [u8; CHARS_COUNT return result; } -fn get_appear_chars_sum(array: [u8; CHARS_COUNT], number: u8) -> i32 { +fn get_appear_chars_sum(array: &[u8; CHARS_COUNT], number: u8) -> i32 { let mut sum: i32 = 0; for (index, num) in array.into_iter().enumerate() { - if num == number { + if *num == number { sum += index as i32 + 1; } } @@ -69,20 +69,21 @@ fn main() { let appears = Vec::from(lines.map(|line| get_appear_arr(line))); - let chars_sum = get_appear_chars_sum(get_appear_sum(appears), lines.len() as u8); + let chars_sum = get_appear_chars_sum(&get_appear_sum(&appears), lines.len() as u8); sum += chars_sum; } - // let sum: i32 = data - // .lines() - // .map(|line| { - // let (left, right) = line.split_at(line.len() / 2); + let sum_2: i32 = data + .lines() + .map(|line| { + let (left, right) = line.split_at(line.len() / 2); - // let appears = vec![get_appear_arr(left), get_appear_arr(right)]; + let appears = vec![get_appear_arr(left), get_appear_arr(right)]; - // return get_appear_chars_sum(get_appear_sum(appears), 2); - // }) - // .sum(); + return get_appear_chars_sum(&get_appear_sum(&appears), 2); + }) + .sum(); println!("{sum}"); + println!("{sum_2}"); }