Skip to content

Commit

Permalink
add pointers to function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 6, 2022
1 parent ed648a0 commit 5233b40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
16 changes: 8 additions & 8 deletions 2022/day_2/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ 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
score += GameResult::Draw.get_score() + player_move_score;
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 {
Expand All @@ -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);
Expand All @@ -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::<i32>();

Expand Down
25 changes: 13 additions & 12 deletions 2022/day_3/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn get_appear_arr(string: &str) -> AppearCharArray {
return store;
}

fn get_appear_sum(unique_chars_arrays: Vec<AppearCharArray>) -> [u8; CHARS_COUNT] {
fn get_appear_sum(unique_chars_arrays: &Vec<AppearCharArray>) -> [u8; CHARS_COUNT] {
let mut result: [u8; CHARS_COUNT] = [0; 52];

for index in 0..CHARS_COUNT {
Expand All @@ -43,11 +43,11 @@ fn get_appear_sum(unique_chars_arrays: Vec<AppearCharArray>) -> [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;
}
}
Expand All @@ -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}");
}

0 comments on commit 5233b40

Please sign in to comment.