Day 7 Part 1 Attempt :(
This commit is contained in:
parent
c4c423871e
commit
12fe856e43
1000
src/input/day07
1000
src/input/day07
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,5 @@
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
@ -0,0 +1,5 @@
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
@ -14,7 +14,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
Box::new(day04::Day04 {}),
|
||||
Box::new(day05::Day05 {}),
|
||||
Box::new(day06::Day06 {}),
|
||||
// Box::new(day07::Day07 {}),
|
||||
Box::new(day07::Day07 {}),
|
||||
// Box::new(day08::Day08 {}),
|
||||
// Box::new(day09::Day09 {}),
|
||||
// Box::new(day10::Day10 {}),
|
||||
@ -57,6 +57,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
println!("========= {}ms =========\n", day.time.as_millis());
|
||||
}
|
||||
|
||||
println!("Ran AoC 2023 in {}ms", SystemTime::now().duration_since(start_time)?.as_millis());
|
||||
println!(
|
||||
"Ran AoC 2023 in {}ms",
|
||||
SystemTime::now().duration_since(start_time)?.as_millis()
|
||||
);
|
||||
Ok(())
|
||||
}
|
@ -1,13 +1,46 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::Solution;
|
||||
|
||||
pub struct Day07 {}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum Hands {
|
||||
HC = 1,
|
||||
OP,
|
||||
TP,
|
||||
ThreeOAK,
|
||||
FH,
|
||||
FourOAK,
|
||||
FiveOAK,
|
||||
}
|
||||
|
||||
impl Solution for Day07 {
|
||||
fn part1(
|
||||
&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)?;
|
||||
let mut totals = vec![];
|
||||
|
||||
for hand in hands.iter() {
|
||||
totals.push((hand.0.clone(), hand.1, self.get_hand_type(&hand.0)? as u32));
|
||||
}
|
||||
|
||||
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() {
|
||||
println!("{} * {}, {:?} {}", i + 1, cards.1, cards.0, cards.2);
|
||||
score += (cards.1 * (i + 1) as u32) as u64;
|
||||
}
|
||||
Ok(Box::new(score))
|
||||
}
|
||||
|
||||
fn part2(
|
||||
@ -22,7 +55,80 @@ impl Solution for Day07 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Day07 {}
|
||||
impl Day07 {
|
||||
fn get_hands_from_string(
|
||||
&self,
|
||||
input: &mut Vec<String>,
|
||||
) -> Result<Vec<(Vec<u32>, u32)>, Box<dyn std::error::Error>> {
|
||||
let mut hands = vec![];
|
||||
|
||||
// Get the string from each line and split into hand and stake
|
||||
for hand in input {
|
||||
let (card, mut stake) = hand.split_at(5);
|
||||
stake = stake.trim();
|
||||
|
||||
let mut hand = vec![];
|
||||
for i in card.chars() {
|
||||
hand.push(match i {
|
||||
'T' => 10,
|
||||
'J' => 11,
|
||||
'Q' => 12,
|
||||
'K' => 13,
|
||||
'A' => 14,
|
||||
_ => i.to_digit(10).unwrap(),
|
||||
});
|
||||
}
|
||||
|
||||
hand.sort();
|
||||
hands.push((hand.iter().rev().map(|c| *c).collect(), stake.parse()?));
|
||||
}
|
||||
Ok(hands)
|
||||
}
|
||||
|
||||
fn get_hand_type(&self, hand: &Vec<u32>) -> Result<Hands, Box<dyn std::error::Error>> {
|
||||
let mut map_cards = HashMap::new();
|
||||
|
||||
for c in hand {
|
||||
map_cards.entry(c).and_modify(|c| *c += 1).or_insert(1u32);
|
||||
}
|
||||
|
||||
let mut counts: Vec<&u32> = map_cards.values().into_iter().collect();
|
||||
counts.sort();
|
||||
|
||||
// If there is a single key in the map we have 5OAK
|
||||
if map_cards.len() == 1 {
|
||||
return Ok(Hands::FiveOAK);
|
||||
}
|
||||
|
||||
// 4OAK
|
||||
// Full House
|
||||
if map_cards.len() == 2 {
|
||||
// 4OAK
|
||||
if *counts[1] == 4u32 {
|
||||
return Ok(Hands::FourOAK);
|
||||
}
|
||||
return Ok(Hands::FH);
|
||||
}
|
||||
|
||||
if map_cards.len() == 3 {
|
||||
// 3OAK
|
||||
if *counts[2] == 3 {
|
||||
return Ok(Hands::ThreeOAK);
|
||||
}
|
||||
|
||||
// Two Pair
|
||||
return Ok(Hands::TP);
|
||||
}
|
||||
|
||||
// One Pair
|
||||
if map_cards.len() == 4 {
|
||||
return Ok(Hands::OP);
|
||||
}
|
||||
|
||||
// High Card
|
||||
Ok(Hands::HC)
|
||||
}
|
||||
}
|
||||
|
||||
/// Test from puzzle input
|
||||
#[cfg(test)]
|
||||
@ -44,7 +150,7 @@ mod test {
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
assert_eq!(answer, "Ready");
|
||||
assert_eq!(answer, "6440");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user