Skip to content

Commit

Permalink
submit day 7 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastil committed Dec 13, 2023
1 parent e42271b commit 2113ab0
Showing 1 changed file with 49 additions and 28 deletions.
77 changes: 49 additions & 28 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
advent_of_code::solution!(7);

pub fn part_one(input: &str) -> Option<u32> {
Some(get_bids_sum(input, false))
}

pub fn part_two(input: &str) -> Option<u32> {
Some(get_bids_sum(input, true))
}

fn get_bids_sum(input: &str, has_jokers: bool) -> u32 {
let mut hands = input
.lines()
.map(|line| {
let (left, right) = line.split_at(6);
(get_hand_rank(&left[..5]), right.parse::<u32>().unwrap())
(
get_hand_rank(&left[..5], has_jokers),
right.parse::<u32>().unwrap(),
)
})
.collect::<Vec<_>>();

hands.sort_by_key(|(rank, _)| *rank);

let sum = hands
hands
.iter()
.enumerate()
.fold(0, |sum, (index, (_, current_bid))| {
sum + (index as u32 + 1) * current_bid
});

Some(sum)
}

pub fn part_two(input: &str) -> Option<u32> {
None
})
}

fn get_hand_rank(hand: &str) -> u32 {
let kind_distribution = get_kind_distribution(hand);
fn get_hand_rank(hand: &str, has_jokers: bool) -> u32 {
let kind_distribution = get_kind_distribution(hand, has_jokers);

let mut hand_rank = get_hand_type_rank(kind_distribution);

for card in hand.chars() {
hand_rank <<= 4;
hand_rank |= get_rank_for_card(&card);
hand_rank |= get_rank_for_card(&card, has_jokers);
}

hand_rank
}

fn get_kind_distribution(hand: &str) -> [u32; 5] {
fn get_kind_distribution(hand: &str, has_jokers: bool) -> [u32; 5] {
let mut kind_values: [Option<(char, u32)>; 5] = [None; 5];
let mut jokers_count = 0;

for card in hand.chars() {
if card == 'J' && has_jokers {
jokers_count += 1;
continue;
}

for option in kind_values.iter_mut() {
if let Some((current_card, count)) = option {
if *current_card == card {
Expand All @@ -59,6 +70,10 @@ fn get_kind_distribution(hand: &str) -> [u32; 5] {

kind_distribution.sort_by(|a, b| b.cmp(a));

if jokers_count > 0 {
kind_distribution[0] += jokers_count;
}

kind_distribution
}

Expand All @@ -75,21 +90,27 @@ fn get_hand_type_rank(kind_distribution: [u32; 5]) -> u32 {
}
}

fn get_rank_for_card(card: &char) -> u32 {
fn get_rank_for_card(card: &char, has_jokers: bool) -> u32 {
match card {
'A' => 0xc,
'K' => 0xb,
'Q' => 0xa,
'J' => 0x9,
'T' => 0x8,
'9' => 0x7,
'8' => 0x6,
'7' => 0x5,
'6' => 0x4,
'5' => 0x3,
'4' => 0x2,
'3' => 0x1,
'2' => 0x0,
'A' => 0xd,
'K' => 0xc,
'Q' => 0xb,
'J' => {
if has_jokers {
0x0
} else {
0xa
}
}
'T' => 0x9,
'9' => 0x8,
'8' => 0x7,
'7' => 0x6,
'6' => 0x5,
'5' => 0x4,
'4' => 0x3,
'3' => 0x2,
'2' => 0x1,
_ => panic!("Invalid card"),
}
}
Expand All @@ -107,6 +128,6 @@ mod tests {
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(5905));
}
}

0 comments on commit 2113ab0

Please sign in to comment.