Advertisement
qzazwxsx

Untitled

Dec 9th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.32 KB | Source Code | 0 0
  1. use crate::{Solution, SolutionPair};
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4.  
  5. pub fn solve(input: std::str::Lines<'_>) -> SolutionPair {
  6.  
  7.    let mut sol1: u64 = 0;
  8.    let mut sol2: i64 = 0;
  9.  
  10.    for line in input {
  11.        let mut all_ints: Vec<i64> = line
  12.            .split(" ")
  13.            .map(|i| i.parse().unwrap())
  14.            .collect();
  15.        
  16.        let mut all_last_digits: Vec<i64> =  vec![*all_ints.last().unwrap()];
  17.        let mut all_first_digits: Vec<i64> = vec![*all_ints.first().unwrap()];
  18.        while all_ints.iter().any(|&x| x != 0) {
  19.            let new_ints: Vec<i64> = all_ints
  20.                .windows(2)
  21.                .map(|i| i[1] - i[0])
  22.                .collect();
  23.            all_last_digits.push( *new_ints.last().unwrap());
  24.            all_first_digits.push(*new_ints.first().unwrap());
  25.            all_ints = new_ints;
  26.        }
  27.  
  28.        let temp: i64 = all_last_digits.iter().sum();
  29.        sol1 += temp as u64;
  30.        
  31.        all_first_digits.reverse();
  32.        let extrapolated_first_digits = all_first_digits
  33.            .iter()
  34.            .skip(1)
  35.            .fold(all_first_digits[0], |acc, &curr| curr - acc);
  36.        sol2 += extrapolated_first_digits;
  37.    }
  38.  
  39.    (Solution::from(sol1), Solution::from(sol2 as u64))
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement