prettify and lint optimization
This commit is contained in:
@@ -7,14 +7,12 @@ fn read_hands(filename: &str) -> io::Result<Vec<(String, i32)>> {
|
||||
let lines = io::BufReader::new(file).lines();
|
||||
|
||||
let mut hands = Vec::new();
|
||||
for line in lines {
|
||||
if let Ok(line) = line {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() == 2 {
|
||||
let hand = parts[0].to_string();
|
||||
let bid: i32 = parts[1].parse().unwrap();
|
||||
hands.push((hand, bid));
|
||||
}
|
||||
for line in lines.flatten() {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() == 2 {
|
||||
let hand = parts[0].to_string();
|
||||
let bid: i32 = parts[1].parse().unwrap();
|
||||
hands.push((hand, bid));
|
||||
}
|
||||
}
|
||||
Ok(hands)
|
||||
@@ -22,11 +20,11 @@ fn read_hands(filename: &str) -> io::Result<Vec<(String, i32)>> {
|
||||
|
||||
fn replace_face_cards(hand: &str) -> String {
|
||||
let mut replaced = hand.to_string();
|
||||
replaced = replaced.replace("T", "a");
|
||||
replaced = replaced.replace("J", "1");
|
||||
replaced = replaced.replace("Q", "c");
|
||||
replaced = replaced.replace("K", "d");
|
||||
replaced = replaced.replace("A", "e");
|
||||
replaced = replaced.replace('T', "a");
|
||||
replaced = replaced.replace('J', "1");
|
||||
replaced = replaced.replace('Q', "c");
|
||||
replaced = replaced.replace('K', "d");
|
||||
replaced = replaced.replace('A', "e");
|
||||
replaced
|
||||
}
|
||||
|
||||
@@ -44,7 +42,11 @@ fn calculate_strength(hand: &str) -> (i32, String) {
|
||||
|
||||
let mut best_category = 4; // Default to High card
|
||||
for (card, count) in &counts {
|
||||
let adjusted_count = if *card != '1' { count + joker_count } else { *count };
|
||||
let adjusted_count = if *card != '1' {
|
||||
count + joker_count
|
||||
} else {
|
||||
*count
|
||||
};
|
||||
best_category = best_category.max(match adjusted_count {
|
||||
5 => 10,
|
||||
4 => 9,
|
||||
@@ -56,7 +58,10 @@ fn calculate_strength(hand: &str) -> (i32, String) {
|
||||
|
||||
// Use original hand for tie-breaking
|
||||
let tie_breaker_hand = hand.chars().collect::<String>();
|
||||
println!("Hand: {}, Strength: {}, Tie-breaker hand: {}", hand, best_category, tie_breaker_hand);
|
||||
println!(
|
||||
"Hand: {}, Strength: {}, Tie-breaker hand: {}",
|
||||
hand, best_category, tie_breaker_hand
|
||||
);
|
||||
(best_category, tie_breaker_hand)
|
||||
}
|
||||
|
||||
@@ -69,17 +74,30 @@ fn calculate_total_winnings(hands: Vec<(String, i32)>) -> i32 {
|
||||
(strength_b, tie_breaker_b).cmp(&(strength_a, tie_breaker_a))
|
||||
});
|
||||
|
||||
sorted_hands.iter().enumerate().fold(0, |acc, (i, (hand, bid))| {
|
||||
let winnings = bid * (i as i32 + 1);
|
||||
println!("Hand: {}, Rank: {}, Bid: {}, Winnings: {}", hand, i + 1, bid, winnings);
|
||||
acc + winnings
|
||||
})
|
||||
sorted_hands
|
||||
.iter()
|
||||
.enumerate()
|
||||
.fold(0, |acc, (i, (hand, bid))| {
|
||||
let winnings = bid * (i as i32 + 1);
|
||||
println!(
|
||||
"Hand: {}, Rank: {}, Bid: {}, Winnings: {}",
|
||||
hand,
|
||||
i + 1,
|
||||
bid,
|
||||
winnings
|
||||
);
|
||||
acc + winnings
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let test_hands = read_hands("../test.txt").expect("Failed to read test file");
|
||||
let test_total = calculate_total_winnings(test_hands);
|
||||
assert_eq!(test_total, 5905, "Test failed: expected 5905, got {}", test_total);
|
||||
assert_eq!(
|
||||
test_total, 5905,
|
||||
"Test failed: expected 5905, got {}",
|
||||
test_total
|
||||
);
|
||||
println!("Test passed: Total winnings = {}", test_total);
|
||||
|
||||
let hands = read_hands("../input.txt").expect("Failed to read input file");
|
||||
|
||||
Reference in New Issue
Block a user