Guest User

Untitled

a guest
Dec 2nd, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.84 KB | Source Code | 0 0
  1. mod part_2 {
  2.     pub fn calibrate(text: &str) -> u32 {
  3.         let mut calibration_value = 0;
  4.         for lines in text.split('\n') {
  5.             calibration_value = calibration_value + decrypt_value(lines);
  6.         }
  7.         calibration_value
  8.    }
  9.  
  10.     fn decrypt_value(line: &str) -> u32 {
  11.         let mut digits = (0, 0);
  12.         let (mut lower_index, mut upper_index) = (0, 1);
  13.  
  14.         while upper_index <= line.len() {
  15.             let region = &line[lower_index .. upper_index];
  16.             let update = regional_digit(region);
  17.             if update == 0 { upper_index = upper_index + 1; continue; } // update = 0 means no digit at current region
  18.  
  19.             if digits.0 > 0 { digits.1 = update; } else { digits.0 = update; }
  20.             lower_index = upper_index - 1;
  21.             upper_index = upper_index + 1;
  22.         }
  23.  
  24.         if digits.1 > 0 { digits.0*10 + digits.1 } else { digits.0*10 + digits.0 }
  25.     }
  26.  
  27.     fn regional_digit(region: &str) -> u32 {
  28.         let last_character = region.chars().last().unwrap();
  29.         let mut update = 0;
  30.         if last_character.is_digit(10) {
  31.             update = last_character.to_digit(10).unwrap();
  32.         }
  33.         if region.contains("one") {
  34.             update = 1;
  35.         }
  36.         if region.contains("two") {
  37.             update = 2;
  38.         }
  39.         if region.contains("three") {
  40.             update = 3;
  41.         }
  42.         if region.contains("four") {
  43.             update = 4;
  44.         }
  45.         if region.contains("five") {
  46.             update = 5;
  47.         }
  48.         if region.contains("six") {
  49.             update = 6;
  50.         }
  51.         if region.contains("seven") {
  52.             update = 7;
  53.         }
  54.         if region.contains("eight") {
  55.             update = 8;
  56.         }
  57.         if region.contains("nine") {
  58.             update = 9;
  59.         }
  60.         update
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment