2023(1): Treat written digits as digits

This is wrong, though, as cases like `oneight` should return `83` whereas
I replace the `one`s first, so that `1ight` will remain that way.

See <https://www.reddit.com/r/adventofcode/comments/1884fpl/2023_day_1for_those_who_stuck_on_part_2/>.
This commit is contained in:
2023-12-02 22:03:52 +01:00
parent ce39b976ab
commit 1c17fcede7
2 changed files with 51 additions and 2 deletions

View File

@ -9,8 +9,32 @@ fn main() {
if let Ok(lines) = read_lines("./input") {
for line in lines {
if let Ok(text) = line {
sum = sum + first_digit(&text);
mus = mus + last_digit(&text);
// TODO: Please us something like a collection instead of
// hardcoding every replacement.
let sanitext = text
.replace("one", "1")
.replace("two", "2")
.replace("three", "3")
.replace("four", "4")
.replace("five", "5")
.replace("six", "6")
.replace("seven", "7")
.replace("eight", "8")
.replace("nine", "9")
;
// DEBUG:
println!(
"{} => {} yields {} + {} = {}",
text,
sanitext,
first_digit(&sanitext),
last_digit(&sanitext),
first_digit(&sanitext) + last_digit(&sanitext)
);
sum = sum + first_digit(&sanitext);
mus = mus + last_digit(&sanitext);
} else {
println!("Error on line \"{:?}\"", line);
}
}
}