feat: implement part 1 day 7
This commit is contained in:
parent
2ffad7325f
commit
e09005bb83
@ -60,7 +60,7 @@ fn solve_02(content: &str) -> anyhow::Result<i64> {
|
||||
.parse::<i64>().unwrap())
|
||||
.collect::<Vec<i64>>();
|
||||
|
||||
let mut races = vec![(result[0].clone(), result[1].clone())];
|
||||
let races = vec![(result[0].clone(), result[1].clone())];
|
||||
Ok(races
|
||||
.iter()
|
||||
.map(|(time, distance)| calc_possible_options(*time, *distance))
|
||||
|
161
src/day07.rs
Normal file
161
src/day07.rs
Normal file
@ -0,0 +1,161 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::panic::panic_any;
|
||||
use std::sync::mpsc::TrySendError::Full;
|
||||
use std::time::Instant;
|
||||
use itertools::Itertools;
|
||||
|
||||
#[derive(Eq, PartialEq, PartialOrd, Ord, Debug)]
|
||||
pub enum Card {
|
||||
A = 2,
|
||||
K = 3,
|
||||
Q = 4,
|
||||
J = 5,
|
||||
T = 6,
|
||||
N9 = 7,
|
||||
N8 = 8,
|
||||
N7 = 9,
|
||||
N6 = 10,
|
||||
N5 = 11,
|
||||
N4 = 12,
|
||||
N3 = 13,
|
||||
N2 = 14
|
||||
}
|
||||
|
||||
impl From<char> for Card {
|
||||
fn from(value: char) -> Self {
|
||||
match value {
|
||||
'A' => Card::A,
|
||||
'K' => Card::K,
|
||||
'Q' => Card::Q,
|
||||
'J' => Card::J,
|
||||
'T' => Card::T,
|
||||
'9' => Card::N9,
|
||||
'8' => Card::N8,
|
||||
'7' => Card::N7,
|
||||
'6' => Card::N6,
|
||||
'5' => Card::N5,
|
||||
'4' => Card::N4,
|
||||
'3' => Card::N3,
|
||||
'2' => Card::N2,
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Card {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, PartialOrd, Ord, Eq, Debug)]
|
||||
pub enum Hand {
|
||||
FiveOfAKind(Vec<Card>),
|
||||
FourOfAKind(Vec<Card>),
|
||||
FullHouse(Vec<Card>),
|
||||
ThreeOfAKind(Vec<Card>),
|
||||
TwoPair(Vec<Card>),
|
||||
OnePair(Vec<Card>),
|
||||
HighCard(Vec<Card>),
|
||||
}
|
||||
|
||||
impl From<Vec<Card>> for Hand {
|
||||
fn from(value: Vec<Card>) -> Self {
|
||||
let mut cards: HashMap<String, usize> = HashMap::new();
|
||||
for c in value.iter().clone() {
|
||||
*cards.entry(c.to_string()).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
match cards.values().len() {
|
||||
1 => Hand::FiveOfAKind(value), // can only be five of a kind
|
||||
2 => { // Can be four of a kind or Full house
|
||||
if *cards.values().sorted().rev().collect::<Vec<&usize>>()[0] == 4 {
|
||||
Hand::FourOfAKind(value)
|
||||
} else {
|
||||
Hand::FullHouse(value)
|
||||
}
|
||||
},
|
||||
3 => { // Can be two pairs or three of a kind
|
||||
if *cards.values().sorted().rev().collect::<Vec<&usize>>()[0] == 3 {
|
||||
Hand::ThreeOfAKind(value)
|
||||
} else {
|
||||
Hand::TwoPair(value)
|
||||
}
|
||||
},
|
||||
4 => Hand::OnePair(value), // can only be one pair
|
||||
5 => Hand::HighCard(value), // can only be high card
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute_task01(content: &str) {
|
||||
let start = Instant::now();
|
||||
let product_correct_races = solve_01(content).unwrap();
|
||||
let duration = start.elapsed();
|
||||
|
||||
assert_eq!(247815719, product_correct_races);
|
||||
println!("Day07 - Task01 - Duration: {duration:?} - Poker Ranks: {}", product_correct_races)
|
||||
}
|
||||
|
||||
pub fn execute_task02(content: &str) {
|
||||
let start = Instant::now();
|
||||
let product_correct_races = solve_02(content).unwrap();
|
||||
let duration = start.elapsed();
|
||||
|
||||
assert_eq!(32583852, product_correct_races);
|
||||
println!("Day07 - Task02 - Duration: {duration:?} - Correct Race (with Joker): {}", product_correct_races)
|
||||
}
|
||||
|
||||
fn solve_01(content: &str) -> anyhow::Result<usize> {
|
||||
Ok(content
|
||||
.lines()
|
||||
.map(|line| line.split_once(" ").unwrap())
|
||||
.map(|(hand, bid)| (hand.chars().map(|c| Card::from(c)).collect::<Vec<Card>>(), bid.parse::<usize>().unwrap()))
|
||||
.map(|(hand, bid)| (Hand::from(hand), bid))
|
||||
.sorted_by(|(hand, _), (second_hand, _)| Ord::cmp(hand, second_hand))
|
||||
.rev()
|
||||
.enumerate()
|
||||
.map(|(index, (_, bid))| (index + 1) * bid)
|
||||
.sum())
|
||||
}
|
||||
|
||||
fn solve_02(content: &str) -> anyhow::Result<i64> {
|
||||
Ok(content
|
||||
.lines()
|
||||
.map(|line| line.split_once(" ").unwrap())
|
||||
.map(|(hand, bid)| (hand.chars().map(|c| Card::from(c)).collect::<Vec<Card>>(), bid.parse::<usize>().unwrap()))
|
||||
.map(|(hand, bid)| (Hand::from(hand), bid))
|
||||
.sorted_by(|(hand, _), (second_hand, _)| Ord::cmp(hand, second_hand))
|
||||
.rev()
|
||||
.enumerate()
|
||||
.map(|(index, (_, bid))| (index + 1) * bid)
|
||||
.sum())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_solve_01() {
|
||||
let example = r#"32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483"#;
|
||||
|
||||
let result = solve_01(example).unwrap();
|
||||
|
||||
assert_eq!(6440, result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_solve_02() {
|
||||
let example = r#"32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483"#;
|
||||
|
||||
let result = solve_02(example).unwrap();
|
||||
|
||||
assert_eq!(5905g, result);
|
||||
}
|
1000
src/input/day07/input.txt
Normal file
1000
src/input/day07/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -5,6 +5,7 @@ mod day03;
|
||||
mod day04;
|
||||
mod day05;
|
||||
mod day06;
|
||||
mod day07;
|
||||
|
||||
const CONTENT01: &'static str = include_str!("input/day01/input.txt");
|
||||
const CONTENT02: &'static str = include_str!("input/day02/input.txt");
|
||||
@ -12,6 +13,7 @@ const CONTENT03: &'static str = include_str!("input/day03/input.txt");
|
||||
const CONTENT04: &'static str = include_str!("input/day04/input.txt");
|
||||
const CONTENT05: &'static str = include_str!("input/day05/input.txt");
|
||||
const CONTENT06: &'static str = include_str!("input/day06/input.txt");
|
||||
const CONTENT07: &'static str = include_str!("input/day07/input.txt");
|
||||
|
||||
fn main() {
|
||||
day01::execute_task01(CONTENT01);
|
||||
@ -31,4 +33,7 @@ fn main() {
|
||||
println!();
|
||||
day06::execute_task01(CONTENT06);
|
||||
day06::execute_task02(CONTENT06);
|
||||
println!();
|
||||
day07::execute_task01(CONTENT07);
|
||||
day07::execute_task02(CONTENT07);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user