Advertisement
qzazwxsx

Untitled

Dec 11th, 2023
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.95 KB | Source Code | 0 0
  1. use crate::{Solution, SolutionPair};
  2. use std::cmp::min;
  3. use std::cmp::max;
  4. use std::iter::repeat;
  5. ///////////////////////////////////////////////////////////////////////////////
  6.  
  7. fn process_input(input: std::str::Lines<'_>) -> (Vec<String>, Vec<(u64, u64)>) {
  8.    let mut result = Vec::new();
  9.    let mut hash_indices: Vec<(u64, u64)> = Vec::new();
  10.  
  11.    let row_len = input.clone().next().unwrap().len();
  12.    let mut rows_with_hash: Vec<bool> = vec![false; row_len];
  13.  
  14.    for col_index in 0..row_len {
  15.        let row_has_hash = input.clone().any(|row| row.chars().nth(col_index).unwrap() == '#');
  16.        rows_with_hash[col_index] = row_has_hash;
  17.    }
  18.  
  19.    for (row_index, line) in input.clone().enumerate() {
  20.        
  21.        let col_has_hash = line.chars().any(|c| c == '#');
  22.        if !col_has_hash {
  23.            result.push(repeat('2').take(row_len).collect());
  24.            continue;
  25.        }
  26.        let mut new_line_chars = Vec::with_capacity(row_len);
  27.  
  28.        for (col_index, ch) in line.chars().enumerate() {
  29.            if ch == '.' {
  30.                let row_has_hash: bool = rows_with_hash[col_index];
  31.                new_line_chars.push(if row_has_hash { '1' } else { '2' });
  32.            } else {
  33.                new_line_chars.push(ch);
  34.                hash_indices.push((row_index as u64, col_index as u64));
  35.            }
  36.        }
  37.  
  38.        result.push(new_line_chars.into_iter().collect());
  39.  
  40.    }
  41.  
  42.    (result, hash_indices)
  43. }
  44.  
  45. fn generate_indices(x1: usize, y1: usize, x2: usize, y2: usize) -> Vec<(usize, usize)> {
  46.    let mut indices = Vec::new();
  47.  
  48.    for x in min(x1, x2) + 1..=max(x1, x2) {
  49.        indices.push((x, y1));
  50.    }
  51.  
  52.    for y in min(y1, y2) + 1..=max(y1, y2) {
  53.        indices.push((x2, y));
  54.    }
  55.  
  56.    indices
  57. }
  58.  
  59. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  60.  
  61.     let (res, idxs) = process_input(input);
  62.  
  63.     let mut count_pairs = 0;
  64.     let mut allsum1: u64 = 0;
  65.     let mut allsum2: u64 = 0;
  66.     for (idx1, coords1) in idxs.iter().enumerate() {
  67.         let x1 = coords1.1;
  68.         let y1 = coords1.0;
  69.         for (idx2, coords2) in idxs[idx1 + 1..].iter().enumerate() {
  70.             let x2 = coords2.1;
  71.             let y2 = coords2.0;
  72.             count_pairs += 1;
  73.             let mut sum1: u64 = 0;
  74.             let mut sum2: u64 = 0;
  75.             for index in generate_indices(x1 as usize, y1 as usize, x2 as usize, y2 as usize) {
  76.                 let x = index.1 as usize;
  77.                 let y = index.0;
  78.                 match res[x].chars().nth(y) {
  79.                     Some('2') => {
  80.                         sum1 += 2;
  81.                         sum2 += 1000000;
  82.                     },
  83.                     _ => {
  84.                         sum1 += 1;
  85.                         sum2 += 1;
  86.                     },
  87.                 }
  88.             }
  89.             allsum1 += sum1;
  90.             allsum2 += sum2;
  91.         }
  92.     }
  93.  
  94.     (Solution::from(allsum1), Solution::from(allsum2))
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement