qzazwxsx

Untitled

Dec 3rd, 2023
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.11 KB | Source Code | 0 0
  1. use crate::{Solution, SolutionPair};
  2. use std::cmp;
  3. use std::collections::HashMap;
  4. ///////////////////////////////////////////////////////////////////////////////
  5.  
  6. fn get_bounds(col: usize, row_low: usize, row_high: usize) -> Vec<(usize, usize)> {
  7.     let min_l = 0;
  8.     let max_l = 139;
  9.     let lowest_col = cmp::max(col as i32 - 1, min_l) as usize;
  10.     let highest_col = cmp::min(col + 1, max_l);
  11.     let lowest_row = cmp::max(row_low as i32 - 1, min_l) as usize;
  12.     let highest_row = cmp::min(row_high + 1, max_l);
  13.  
  14.     let mut ret_vec: Vec<(usize, usize)> = Vec::new();
  15.  
  16.     for c in lowest_col..=highest_col {
  17.         for r in lowest_row..=highest_row {
  18.             ret_vec.push((c, r));
  19.         }
  20.     }
  21.  
  22.     ret_vec
  23. }
  24.  
  25.  
  26. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  27.  
  28.    let lines: Vec<&str> = input.clone().collect();
  29.  
  30.    let mut total_sum = 0;
  31.  
  32.    let mut map: HashMap<Vec<(usize, usize)>, u32> = HashMap::new();
  33.  
  34.    for (idx_y, line) in lines.iter().enumerate() {
  35.  
  36.        let mut in_digit_block = false;
  37.        let mut curr_start = 0;
  38.        let mut curr_sum = 0;
  39.  
  40.        for (idx_x, character) in line.chars().enumerate() {
  41.            if character.is_digit(10) && !in_digit_block {
  42.                in_digit_block = true;
  43.                curr_start = idx_x;
  44.            }
  45.            if (!character.is_digit(10) && in_digit_block) || (character.is_digit(10) && idx_x == 139) {
  46.                if character.is_digit(10) && idx_x == 139 {
  47.                    curr_sum = curr_sum * 10 + character.to_digit(10).unwrap();
  48.                }
  49.                let bounds = get_bounds(idx_y, curr_start, idx_x - 1);
  50.                map.insert(bounds.clone(), curr_sum);
  51.                in_digit_block = false;
  52.                
  53.                let mut found_char = false;
  54.                
  55.                for (y, x) in bounds {
  56.                    let inp = lines[y].chars().nth(x).unwrap();
  57.                    if !(inp.is_digit(10) || inp == '.') {
  58.                        found_char = true;
  59.                        break;
  60.                    }
  61.                }
  62.                if found_char {
  63.                    total_sum += curr_sum;
  64.                }
  65.                curr_sum = 0;
  66.            }
  67.            if character.is_digit(10) && in_digit_block {
  68.                curr_sum = curr_sum * 10 + character.to_digit(10).unwrap();
  69.            }
  70.        }
  71.    }
  72.  
  73.    let mut total_sum_pt2 = 0;
  74.    for (idx_y, line) in lines.iter().enumerate() {
  75.        for (idx_x, character) in line.chars().enumerate() {
  76.            let ch = lines[idx_y].chars().nth(idx_x).unwrap();
  77.            if ch == '*' {
  78.                let mut prod = 1;
  79.                let mut count = 0;
  80.                for (key, value) in &map {
  81.                    if key.contains(&(idx_y, idx_x)) {
  82.                        count += 1;
  83.                        prod *= value;
  84.                    }
  85.                }
  86.                if count == 2 {
  87.                    total_sum_pt2 += prod;
  88.                }
  89.            }
  90.        }
  91.    }
  92.  
  93.    (Solution::from(total_sum), Solution::from(total_sum_pt2))
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment