From 7df3dc99955e9a7fdf3a425394ca870cf02a10e0 Mon Sep 17 00:00:00 2001 From: BakerNet Date: Fri, 1 Dec 2023 16:34:11 -0800 Subject: [PATCH] slightly prettier solution --- src/bin/01.rs | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/bin/01.rs b/src/bin/01.rs index 497ffc9..4489772 100644 --- a/src/bin/01.rs +++ b/src/bin/01.rs @@ -18,6 +18,18 @@ pub fn part_one(input: &str) -> Option { ) } +static NUMBERS: &'static [(&'static str, u32); 9] = &[ + ("one", 1), + ("two", 2), + ("three", 3), + ("four", 4), + ("five", 5), + ("six", 6), + ("seven", 7), + ("eight", 8), + ("nine", 9), +]; + pub fn part_two(input: &str) -> Option { Some( input @@ -27,27 +39,13 @@ pub fn part_two(input: &str) -> Option { if let Some(digit) = c.1.to_digit(10) { Some(digit) } else { - if line[c.0..].starts_with("one") { - Some(1) - } else if line[c.0..].starts_with("two") { - Some(2) - } else if line[c.0..].starts_with("three") { - Some(3) - } else if line[c.0..].starts_with("four") { - Some(4) - } else if line[c.0..].starts_with("five") { - Some(5) - } else if line[c.0..].starts_with("six") { - Some(6) - } else if line[c.0..].starts_with("seven") { - Some(7) - } else if line[c.0..].starts_with("eight") { - Some(8) - } else if line[c.0..].starts_with("nine") { - Some(9) - } else { - None - } + NUMBERS.iter().find_map(|ntup| { + if line[c.0..].starts_with(ntup.0) { + Some(ntup.1) + } else { + None + } + }) } }); let first = digits.next().expect("Expected a digit");