blob: 17799730b168a469c2368a9fde4996389ba97682 [file] [log] [blame]
Skyler Grey1ce1cfc2023-12-01 21:54:23 +00001use regex::Regex;
2use phf::phf_map;
3
4static NUMBERS: phf::Map<&'static str, u8> = phf_map! {
5 "zero" => 0,
6 "one" => 1,
7 "two" => 2,
8 "three" => 3,
9 "four" => 4,
10 "five" => 5,
11 "six" => 6,
12 "seven" => 7,
13 "eight" => 8,
14 "nine" => 9,
15 "0" => 0,
16 "1" => 1,
17 "2" => 2,
18 "3" => 3,
19 "4" => 4,
20 "5" => 5,
21 "6" => 6,
22 "7" => 7,
23 "8" => 8,
24 "9" => 9,
25};
26
27
28fn main() {
29 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();
30 let only_one_digit: Regex = Regex::new(r"([0-9])").unwrap();
31 let input = include_str!("../input.txt");
32
33 let mut vals: Vec<u16> = vec![];
34
35 for line in input.split('\n') {
36 if line == "" {
37 continue;
38 }
39
40 let result = first_and_last_digits.captures(line);
41
42 let group1;
43 let group2;
44
45 if let Some(captured) = result {
46 [group1, group2] = captured.extract().1;
47 } else {
48 [group1] = only_one_digit.captures(line).expect("Line did not have a capture").extract().1;
49 group2 = group1;
50 }
51
52 let first_digit = NUMBERS.get(group1).expect("Regex group 1 and phf hashmap were mismatched");
53 let last_digit = NUMBERS.get(group2).expect("Regex group 2 and phf hashmap were mismatched");
54
55 vals.push((first_digit * 10 + last_digit).into());
56 }
57
58 println!("{}", vals.into_iter().fold(0, | acc, curr | acc + curr));
59}