Skip to content

Instantly share code, notes, and snippets.

@tbarusseau
Created December 1, 2023 11:54
Show Gist options
  • Save tbarusseau/d3413ff69019bc2798629f002e50d26e to your computer and use it in GitHub Desktop.
Save tbarusseau/d3413ff69019bc2798629f002e50d26e to your computer and use it in GitHub Desktop.
fn compute_result(input: &str) -> u32 {
input
.replace(char::is_alphabetic, "")
.lines()
.map(|l| {
let mut iter = l.chars().flat_map(|c| char::to_digit(c, 10)).peekable();
let first = *iter.peek().expect("no first numerical character");
let last = iter.last().expect("no last numerical character");
first * 10 + last
})
.sum()
}
fn solve_part1(input: &str) -> Box<dyn std::fmt::Display> {
let res = compute_result(input.trim_end());
Box::new(res)
}
fn replace_spelled_out_digits(input: &str) -> String {
let mut ret = String::new();
let mut slice = input;
let numerals = vec![
("one", '1'),
("two", '2'),
("three", '3'),
("four", '4'),
("five", '5'),
("six", '6'),
("seven", '7'),
("eight", '8'),
("nine", '9'),
];
loop {
let len = slice.len();
if len < 3 {
ret.push_str(slice);
break;
}
let mut found = false;
for (s, c) in numerals.iter() {
if slice.starts_with(s) {
ret.push(*c);
found = true;
break;
}
}
if !found {
// For simplicity sake, discard anything that we don't want.
let next_char = slice.chars().next().expect("no next char");
if next_char.is_numeric() || next_char == '\n' {
ret.push(next_char);
}
}
// Advance the slice char by char to make sure we don't miss cases where
// the last letters of a numeral form the first letters of another one.
slice = &slice[1..];
}
ret
}
fn solve_part2(input: &str) -> Box<dyn std::fmt::Display> {
let replaced = replace_spelled_out_digits(input.trim_end());
let res = compute_result(&replaced);
Box::new(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment