Advertisement
qzazwxsx

Untitled

Dec 11th, 2023 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.15 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. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  46.  
  47.     let (res, idxs) = process_input(input);
  48.  
  49.     let mut allsum1: u64 = 0;
  50.     let mut allsum2: u64 = 0;
  51.    
  52.     let res_chars: Vec<Vec<char>> = res.iter().map(|s| s.chars().collect()).collect();
  53.  
  54.     for (idx1, coords1) in idxs.iter().enumerate() {
  55.         let x1: usize = coords1.1 as usize;
  56.         let y1: usize = coords1.0 as usize;
  57.         for (idx2, coords2) in idxs[idx1 + 1..].iter().enumerate() {
  58.             let x2: usize = coords2.1 as usize;
  59.             let y2: usize = coords2.0 as usize;
  60.  
  61.             let mut sum1: u64 = 0;
  62.             let mut sum2: u64 = 0;
  63.  
  64.             // you could make the thing below a function but i'm not gonna bother for now B)
  65.             for x in min(x1, x2) + 1..=max(x1, x2) {
  66.                 let y = y1;
  67.                 match res_chars[y][x] {
  68.                     '2' => {
  69.                         sum1 += 2;
  70.                         sum2 += 1000000;
  71.                     },
  72.                     _ => {
  73.                         sum1 += 1;
  74.                         sum2 += 1;
  75.                     },
  76.                 }
  77.             }
  78.            
  79.             for y in min(y1, y2) + 1..=max(y1, y2) {
  80.                 let x = x2;
  81.                 match res_chars[y][x] {
  82.                     '2' => {
  83.                         sum1 += 2;
  84.                         sum2 += 1000000;
  85.                     },
  86.                     _ => {
  87.                         sum1 += 1;
  88.                         sum2 += 1;
  89.                     },
  90.                 }
  91.             }
  92.  
  93.             allsum1 += sum1;
  94.             allsum2 += sum2;
  95.         }
  96.     }
  97.  
  98.     (Solution::from(allsum1), Solution::from(allsum2))
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement