From 6a32039ff0450d782b9b11ec943f641ac280c97f Mon Sep 17 00:00:00 2001 From: Connor Johnstone Date: Fri, 1 Dec 2023 16:58:31 -0700 Subject: [PATCH] Modest speed improvements --- day_01/src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/day_01/src/lib.rs b/day_01/src/lib.rs index e08aa58..7199520 100644 --- a/day_01/src/lib.rs +++ b/day_01/src/lib.rs @@ -1,8 +1,6 @@ -use std::iter::zip; - const WORD_LIST: [&str; 9] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; -// const REVERSE_WORD_LIST: [&str; 9] = ["eno", "owt", "eerht", "ruof", "evif", "xis", "neves", "thgie", "enin"]; -const DIGIT_LIST: [&str; 9] = ["o1e", "t2o", "t3e", "f4r", "f5e", "s6x", "s7n", "e8t", "n9e"]; +const REVERSE_WORD_LIST: [&str; 9] = ["eno", "owt", "eerht", "ruof", "evif", "xis", "neves", "thgie", "enin"]; +const DIGIT_LIST: [u32; 9] = [1, 2, 3, 4, 5, 6, 7, 8, 9]; fn find_first_digit(line: &str) -> u32 { match line.chars().find(|character| { character.is_digit(10) }) { @@ -18,12 +16,52 @@ fn find_last_digit(line: &str) -> u32 { } } -fn replace_with_digits(input: &str) -> String { - let mut output = input.to_string(); - for (word, digit) in zip(WORD_LIST, DIGIT_LIST) { - output = output.replace(word, digit).to_string(); +fn contains_digit_or_spelled(subline: &str) -> Option { + if subline.starts_with(WORD_LIST[0]) { + return Some(DIGIT_LIST[0]); + } else if subline.starts_with(WORD_LIST[1]) { + return Some(DIGIT_LIST[1]); + } else if subline.starts_with(WORD_LIST[2]) { + return Some(DIGIT_LIST[2]); + } else if subline.starts_with(WORD_LIST[3]) { + return Some(DIGIT_LIST[3]); + } else if subline.starts_with(WORD_LIST[4]) { + return Some(DIGIT_LIST[4]); + } else if subline.starts_with(WORD_LIST[5]) { + return Some(DIGIT_LIST[5]); + } else if subline.starts_with(WORD_LIST[6]) { + return Some(DIGIT_LIST[6]); + } else if subline.starts_with(WORD_LIST[7]) { + return Some(DIGIT_LIST[7]); + } else if subline.starts_with(WORD_LIST[8]) { + return Some(DIGIT_LIST[8]); + } else { + subline.chars().next().unwrap().to_digit(10) + } +} + +fn contains_digit_or_spelled_reverse(subline: &str) -> Option { + if subline.starts_with(REVERSE_WORD_LIST[0]) { + return Some(DIGIT_LIST[0]); + } else if subline.starts_with(REVERSE_WORD_LIST[1]) { + return Some(DIGIT_LIST[1]); + } else if subline.starts_with(REVERSE_WORD_LIST[2]) { + return Some(DIGIT_LIST[2]); + } else if subline.starts_with(REVERSE_WORD_LIST[3]) { + return Some(DIGIT_LIST[3]); + } else if subline.starts_with(REVERSE_WORD_LIST[4]) { + return Some(DIGIT_LIST[4]); + } else if subline.starts_with(REVERSE_WORD_LIST[5]) { + return Some(DIGIT_LIST[5]); + } else if subline.starts_with(REVERSE_WORD_LIST[6]) { + return Some(DIGIT_LIST[6]); + } else if subline.starts_with(REVERSE_WORD_LIST[7]) { + return Some(DIGIT_LIST[7]); + } else if subline.starts_with(REVERSE_WORD_LIST[8]) { + return Some(DIGIT_LIST[8]); + } else { + subline.chars().next().unwrap().to_digit(10) } - output } pub fn part1(input: &str) -> String { @@ -39,8 +77,28 @@ pub fn part1(input: &str) -> String { } pub fn part2(input: &str) -> String { - let parsed_inputs = &replace_with_digits(input); - part1(parsed_inputs) + let calibration_lines = input + .lines() + .map(|line| { + let mut first = 0; + for i in 0..line.len() { + if let Some(i) = contains_digit_or_spelled(&line[i..]) { + first = i; + break; + } + }; + let reversed_line = line.chars().rev().collect::(); + let mut last = 0; + for i in 0..line.len() { + if let Some(i) = contains_digit_or_spelled_reverse(&reversed_line[i..]) { + last = i; + break; + } + }; + format!("{}{}", first, last).parse::().unwrap() + }) + .collect::>(); + format!("{}", calibration_lines.into_iter().sum::()) } pub mod prelude {