Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mod part_2 {
- pub fn calibrate(text: &str) -> u32 {
- let mut calibration_value = 0;
- for lines in text.split('\n') {
- calibration_value = calibration_value + decrypt_value(lines);
- }
- calibration_value
- }
- fn decrypt_value(line: &str) -> u32 {
- let mut digits = (0, 0);
- let (mut lower_index, mut upper_index) = (0, 1);
- while upper_index <= line.len() {
- let region = &line[lower_index .. upper_index];
- let update = regional_digit(region);
- if update == 0 { upper_index = upper_index + 1; continue; } // update = 0 means no digit at current region
- if digits.0 > 0 { digits.1 = update; } else { digits.0 = update; }
- lower_index = upper_index - 1;
- upper_index = upper_index + 1;
- }
- if digits.1 > 0 { digits.0*10 + digits.1 } else { digits.0*10 + digits.0 }
- }
- fn regional_digit(region: &str) -> u32 {
- let last_character = region.chars().last().unwrap();
- let mut update = 0;
- if last_character.is_digit(10) {
- update = last_character.to_digit(10).unwrap();
- }
- if region.contains("one") {
- update = 1;
- }
- if region.contains("two") {
- update = 2;
- }
- if region.contains("three") {
- update = 3;
- }
- if region.contains("four") {
- update = 4;
- }
- if region.contains("five") {
- update = 5;
- }
- if region.contains("six") {
- update = 6;
- }
- if region.contains("seven") {
- update = 7;
- }
- if region.contains("eight") {
- update = 8;
- }
- if region.contains("nine") {
- update = 9;
- }
- update
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment