Modest speed improvements
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
use std::iter::zip;
|
|
||||||
|
|
||||||
const WORD_LIST: [&str; 9] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
|
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 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 DIGIT_LIST: [u32; 9] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
|
||||||
fn find_first_digit(line: &str) -> u32 {
|
fn find_first_digit(line: &str) -> u32 {
|
||||||
match line.chars().find(|character| { character.is_digit(10) }) {
|
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 {
|
fn contains_digit_or_spelled(subline: &str) -> Option<u32> {
|
||||||
let mut output = input.to_string();
|
if subline.starts_with(WORD_LIST[0]) {
|
||||||
for (word, digit) in zip(WORD_LIST, DIGIT_LIST) {
|
return Some(DIGIT_LIST[0]);
|
||||||
output = output.replace(word, digit).to_string();
|
} 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<u32> {
|
||||||
|
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 {
|
pub fn part1(input: &str) -> String {
|
||||||
@@ -39,8 +77,28 @@ pub fn part1(input: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &str) -> String {
|
pub fn part2(input: &str) -> String {
|
||||||
let parsed_inputs = &replace_with_digits(input);
|
let calibration_lines = input
|
||||||
part1(parsed_inputs)
|
.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::<String>();
|
||||||
|
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::<u32>().unwrap()
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
format!("{}", calibration_lines.into_iter().sum::<u32>())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
|
|||||||
Reference in New Issue
Block a user