qzazwxsx

Untitled

Dec 2nd, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.86 KB | Source Code | 0 0
  1. use crate::{Solution, SolutionPair};
  2. use std::collections::HashMap;
  3. use std::cmp::max;
  4. ///////////////////////////////////////////////////////////////////////////////
  5.  
  6. fn parse_game_data(game_data: &str) -> Vec<Vec<(i32, String)>> {
  7.     game_data
  8.         .split("; ")
  9.         .map(|sub_game| {
  10.             sub_game
  11.                 .split(", ")
  12.                 .map(|s| {
  13.                     let parts: Vec<_> = s.split_whitespace().collect();
  14.                     let number = parts[0].parse::<i32>().unwrap();
  15.                     let color = parts[1].to_string();
  16.                     (number, color)
  17.                 })
  18.                 .collect()
  19.         })
  20.         .collect()
  21. }
  22.  
  23. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  24.    let sol1: u64 = 0;
  25.    let sol2: u64 = 0;
  26.  
  27.    let mut impossible_game_ids_sum: u64 = 0;
  28.    let mut sum_powersets: u64 = 0;
  29.    
  30.    for game in input {
  31.        let mut parts = game.split(": ");
  32.        let id = parts.next().unwrap().split_whitespace().nth(1).unwrap().parse::<u64>().unwrap();
  33.        let data = parts.next().unwrap();
  34.        
  35.        let game_amts_and_colors = parse_game_data(data);
  36.        let mut sum_mult = 1;
  37.  
  38.        let mut max_color_amounts: HashMap<String, i32> = HashMap::from([
  39.            ("red".to_string(), 0),
  40.            ("green".to_string(), 0),
  41.            ("blue".to_string(), 0),
  42.        ]);
  43.  
  44.        for sub_game in &game_amts_and_colors {
  45.            let mut curr_color_amounts: HashMap<String, i32> = HashMap::from([
  46.                ("red".to_string(), 0),
  47.                ("green".to_string(), 0),
  48.                ("blue".to_string(), 0),
  49.            ]);
  50.            for (amt, color) in sub_game {
  51.                let count_curr = curr_color_amounts.get_mut(color).unwrap();
  52.                *count_curr += amt;
  53.            }
  54.            let red_bool: bool   = curr_color_amounts.get("red").unwrap()   > &12;
  55.            let green_bool: bool = curr_color_amounts.get("green").unwrap() > &13;
  56.            let blue_bool: bool  = curr_color_amounts.get("blue").unwrap()  > &14;
  57.            if red_bool || green_bool || blue_bool {
  58.                impossible_game_ids_sum += id * sum_mult;
  59.                sum_mult = 0;
  60.            }
  61.            for (amt, color) in sub_game {
  62.                let count_curr = curr_color_amounts.get_mut(color).unwrap();
  63.                let count_max  = max_color_amounts.get_mut(color).unwrap();
  64.                *count_max = max(*count_max, *count_curr);
  65.            }
  66.        }
  67.        let mut power_of_cubeset = 1;
  68.        for (key, value) in &max_color_amounts {
  69.            power_of_cubeset *= value;
  70.        }
  71.        sum_powersets += power_of_cubeset as u64;
  72.    }
  73.  
  74.    let sum_all_ids = 101 * 50;
  75.  
  76.    // println!("{split_input:?}");
  77.    (Solution::from(sum_all_ids - impossible_game_ids_sum), Solution::from(sum_powersets))
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment