qzazwxsx

Untitled

Dec 7th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.60 KB | Source Code | 0 0
  1. use crate::{Solution, SolutionPair};
  2. use std::collections::HashMap;
  3. use std::cmp::Ordering;
  4. ///////////////////////////////////////////////////////////////////////////////
  5.  
  6. fn map_chars_to_count(input_str: &str) -> HashMap<char, u32> {
  7.     let mut char_counts: HashMap<char, u32> = HashMap::new();
  8.  
  9.     for ch in input_str.chars() {
  10.         *char_counts.entry(ch).or_insert(0) += 1;
  11.     }
  12.  
  13.     char_counts
  14. }
  15.  
  16. fn get_top_values(char_counts: &HashMap<char, u32>) -> Vec<(char, u32)> {
  17.     let mut sorted_counts: Vec<_> = char_counts.iter().collect();
  18.     sorted_counts.sort_by(|a, b| b.1.cmp(a.1));
  19.  
  20.     sorted_counts
  21.         .iter()
  22.         .map(|(&ch, &count)| (ch, count as u32))
  23.         .collect()
  24. }
  25.  
  26. fn compare_hands(hand1: &str, hand2: &str, game1: bool) -> Ordering {
  27.     let mut strengths: Vec<char> = vec!['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
  28.  
  29.     let mut count1 = map_chars_to_count(hand1);
  30.     let mut count2 = map_chars_to_count(hand2);
  31.    
  32.  
  33.     let mut top1 = get_top_values(&count1);
  34.     let mut top2 = get_top_values(&count2);
  35.  
  36.     if !game1 {
  37.         strengths = vec!['A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'];
  38.  
  39.         if hand1 != "JJJJJ" {
  40.             let mut topcount1 = top1[0].0;
  41.             if topcount1 == 'J' {
  42.                 topcount1 = top1[1].0;
  43.             }
  44.            
  45.             if let Some(j_value) = count1.remove(&'J') {
  46.                 count1
  47.                 .entry(topcount1)
  48.                 .and_modify(|e| { *e += j_value });
  49.             }
  50.             top1 = get_top_values(&count1);
  51.         }
  52.        
  53.         if hand2 != "JJJJJ" {
  54.             let mut topcount2 = top2[0].0;
  55.             if topcount2 == 'J' {
  56.                 topcount2 = top2[1].0;
  57.             }
  58.  
  59.             if let Some(j_value) = count2.remove(&'J') {
  60.                 count2
  61.                     .entry(topcount2)
  62.                     .and_modify(|e| { *e += j_value });
  63.             }
  64.  
  65.             top2 = get_top_values(&count2);
  66.         }
  67.     }
  68.  
  69.     if top1[0].1 > top2[0].1 {
  70.         return Ordering::Greater
  71.     }
  72.     if top1[0].1 < top2[0].1 {
  73.         return Ordering::Less
  74.     }
  75.     if top1.len() > 1 && top1[1].1 > top2[1].1 {
  76.         return Ordering::Greater
  77.     }
  78.     if top1.len() > 1 && top1[1].1 < top2[1].1 {
  79.         return Ordering::Less
  80.     }
  81.  
  82.     let indices1: Vec<usize> = hand1
  83.         .chars()
  84.         .map(|i| strengths.iter().position(|&r| r == i).unwrap())
  85.         .collect();
  86.     let indices2: Vec<usize> = hand2
  87.         .chars()
  88.         .map(|i| strengths.iter().position(|&r| r == i).unwrap())
  89.         .collect();
  90.     for (elem1, elem2) in indices1.iter().zip(indices2.iter()) {
  91.         if elem1 < elem2 {
  92.             return Ordering::Greater
  93.         }
  94.         if elem1 > elem2 {
  95.             return Ordering::Less
  96.         }
  97.     }
  98.     return Ordering::Equal;
  99. }
  100.  
  101. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  102.  
  103.    let mut mapped: Vec<(&str, u64)> = input
  104.        .map(|i| {
  105.            let mut elems = i.split(" ");
  106.            return (elems.next().unwrap(), elems.next().unwrap().parse().unwrap())
  107.        })
  108.        .collect();
  109.  
  110.    mapped.sort_by(|a, b| compare_hands(a.0, b.0, true));
  111.    
  112.    let mut sol1: u64 = 0;
  113.    for (index, (a, b)) in mapped.iter().enumerate() {
  114.        sol1 += (index as u64 + 1) * b;
  115.    }
  116.    
  117.    mapped.sort_by(|a, b| compare_hands(a.0, b.0, false));
  118.  
  119.    let mut sol2: u64 = 0;
  120.    for (index, (a, b)) in mapped.iter().enumerate() {
  121.        sol2 += (index as u64 + 1) * b;
  122.    }
  123.  
  124.    (Solution::from(sol1), Solution::from(sol2))
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment