Day 07 Part 2 complete (Ignore the messy code!)
This commit is contained in:
parent
cbfc3a4dfc
commit
f2ec4b3ba9
@ -20,7 +20,7 @@ impl Solution for Day07 {
|
||||
&self,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
|
||||
let hands = self.get_hands_from_string(input)?;
|
||||
let hands = self.get_hands_from_string(input, false)?;
|
||||
let mut totals = vec![];
|
||||
|
||||
for hand in hands.iter() {
|
||||
@ -37,7 +37,6 @@ impl Solution for Day07 {
|
||||
|
||||
let mut score: u64 = 0;
|
||||
for (i, cards) in totals.iter().enumerate() {
|
||||
println!("{} * {}, {:?} {}", i + 1, cards.1, cards.0, cards.2);
|
||||
score += (cards.1 * (i + 1) as u32) as u64;
|
||||
}
|
||||
Ok(Box::new(score))
|
||||
@ -45,9 +44,34 @@ impl Solution for Day07 {
|
||||
|
||||
fn part2(
|
||||
&self,
|
||||
_input: &mut Vec<String>,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
|
||||
Ok(Box::new("Ready"))
|
||||
let hands = self.get_hands_from_string(input, true)?;
|
||||
let mut totals = vec![];
|
||||
|
||||
for hand in hands.iter() {
|
||||
let adapted_hand = self.replace_wildcard(&hand.0)?;
|
||||
totals.push((
|
||||
hand.0.clone(),
|
||||
hand.1,
|
||||
self.get_hand_type(&adapted_hand)? as u32,
|
||||
));
|
||||
println!("{:?}", hand);
|
||||
}
|
||||
|
||||
totals.sort_by(|a, b| {
|
||||
if a.2 == b.2 {
|
||||
a.0.partial_cmp(&b.0).unwrap()
|
||||
} else {
|
||||
(a.2 as u32).cmp(&(b.2 as u32))
|
||||
}
|
||||
});
|
||||
|
||||
let mut score: u64 = 0;
|
||||
for (i, cards) in totals.iter().enumerate() {
|
||||
score += (cards.1 * (i + 1) as u32) as u64;
|
||||
}
|
||||
Ok(Box::new(score))
|
||||
}
|
||||
|
||||
fn get_day(&self) -> u8 {
|
||||
@ -59,6 +83,7 @@ impl Day07 {
|
||||
fn get_hands_from_string(
|
||||
&self,
|
||||
input: &mut Vec<String>,
|
||||
wildcard: bool,
|
||||
) -> Result<Vec<(Vec<u32>, u32)>, Box<dyn std::error::Error>> {
|
||||
let mut hands = vec![];
|
||||
|
||||
@ -71,7 +96,13 @@ impl Day07 {
|
||||
for i in card.chars() {
|
||||
hand.push(match i {
|
||||
'T' => 10,
|
||||
'J' => 11,
|
||||
'J' => {
|
||||
if wildcard {
|
||||
0
|
||||
} else {
|
||||
11
|
||||
}
|
||||
}
|
||||
'Q' => 12,
|
||||
'K' => 13,
|
||||
'A' => 14,
|
||||
@ -128,6 +159,51 @@ impl Day07 {
|
||||
// High Card
|
||||
Ok(Hands::HC)
|
||||
}
|
||||
|
||||
fn replace_wildcard<'a>(
|
||||
&self,
|
||||
input: &Vec<u32>,
|
||||
) -> Result<Vec<u32>, Box<dyn std::error::Error>> {
|
||||
let mut map_cards = HashMap::new();
|
||||
|
||||
for c in input.iter() {
|
||||
map_cards.entry(c).and_modify(|c| *c += 1).or_insert(1u32);
|
||||
}
|
||||
|
||||
// Get the wildcard count from the map and replace with the most common card
|
||||
let wc_count = *map_cards.get(&0).unwrap_or(&0);
|
||||
|
||||
// Remove wildcards as we have already
|
||||
map_cards.remove_entry(&0);
|
||||
|
||||
// Find the maximum frequency
|
||||
let max_frequency = map_cards.values().cloned().max().unwrap_or(0);
|
||||
|
||||
// Filter numbers with the maximum frequency
|
||||
let most_frequenct_cards: Vec<u32> = map_cards
|
||||
.clone()
|
||||
.into_iter()
|
||||
.filter(|&(_, frequency)| frequency == max_frequency)
|
||||
.map(|(number, _)| *number)
|
||||
.collect::<Vec<u32>>();
|
||||
|
||||
if most_frequenct_cards.len() == 0 {
|
||||
return Ok(input.clone());
|
||||
}
|
||||
|
||||
map_cards.entry(&most_frequenct_cards[0]).and_modify(|v| {
|
||||
*v += wc_count;
|
||||
});
|
||||
|
||||
let result: Vec<u32> = map_cards
|
||||
.into_iter()
|
||||
.flat_map(|(key, count)| vec![*key; count as usize])
|
||||
.collect();
|
||||
|
||||
println!("Replaced: {:?}", result);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Test from puzzle input
|
||||
@ -167,6 +243,6 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "5905");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user