blob: 17799730b168a469c2368a9fde4996389ba97682 [file] [log] [blame]
use regex::Regex;
use phf::phf_map;
static NUMBERS: phf::Map<&'static str, u8> = phf_map! {
"zero" => 0,
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4,
"five" => 5,
"six" => 6,
"seven" => 7,
"eight" => 8,
"nine" => 9,
"0" => 0,
"1" => 1,
"2" => 2,
"3" => 3,
"4" => 4,
"5" => 5,
"6" => 6,
"7" => 7,
"8" => 8,
"9" => 9,
};
fn main() {
let first_and_last_digits: Regex = Regex::new(r"(zero|one|two|three|four|five|six|seven|eight|nine|[0-9]).*(zero|one|two|three|four|five|six|seven|eight|nine|[0-9])").unwrap();
let only_one_digit: Regex = Regex::new(r"([0-9])").unwrap();
let input = include_str!("../input.txt");
let mut vals: Vec<u16> = vec![];
for line in input.split('\n') {
if line == "" {
continue;
}
let result = first_and_last_digits.captures(line);
let group1;
let group2;
if let Some(captured) = result {
[group1, group2] = captured.extract().1;
} else {
[group1] = only_one_digit.captures(line).expect("Line did not have a capture").extract().1;
group2 = group1;
}
let first_digit = NUMBERS.get(group1).expect("Regex group 1 and phf hashmap were mismatched");
let last_digit = NUMBERS.get(group2).expect("Regex group 2 and phf hashmap were mismatched");
vals.push((first_digit * 10 + last_digit).into());
}
println!("{}", vals.into_iter().fold(0, | acc, curr | acc + curr));
}