-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ edition = "2021" | |
aoc = { path = "../aoc" } | ||
color-eyre = "0.6.2" | ||
nom = "7.1.3" | ||
regex = "1.10.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org> | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE', which is part of this source code package. | ||
|
||
use color_eyre::{eyre, Result}; | ||
use std::io::{stdin, BufRead}; | ||
|
||
use regex::Regex; | ||
|
||
use day01::*; | ||
|
||
fn readnum(s: &str) -> Result<u32> { | ||
s.parse::<u32>().or(match s { | ||
"zero" => Ok(0), | ||
"one" => Ok(1), | ||
"two" => Ok(2), | ||
"three" => Ok(3), | ||
"four" => Ok(4), | ||
"five" => Ok(5), | ||
"six" => Ok(6), | ||
"seven" => Ok(7), | ||
"eight" => Ok(8), | ||
"nine" => Ok(9), | ||
_ => Err(eyre!("invalid number")), | ||
}) | ||
} | ||
|
||
fn process(bufin: impl BufRead) -> Result<u32> { | ||
let re1 = Regex::new(r"([0-9]|zero|one|two|three|four|five|six|seven|eight|nine)")?; | ||
let re2 = Regex::new(r"([0-9]|enin|thgie|neves|xis|evif|ruof|eerht|owt|eno|orez)")?; | ||
let lines = parser::parse(bufin)?; | ||
Ok(lines | ||
.into_iter() | ||
.map(|line| { | ||
let first = re1 | ||
.find(&line) | ||
.ok_or_else(|| eyre!("could not match re1"))?; | ||
let linerev = line.chars().rev().collect::<String>(); | ||
let lastrev = re2 | ||
.find(&linerev) | ||
.ok_or_else(|| eyre!("could not match re2"))?; | ||
let last = lastrev.as_str().chars().rev().collect::<String>(); | ||
let first = readnum(&first.as_str())?; | ||
let last = readnum(&last)?; | ||
let value = first * 10 + last; | ||
Ok(value) | ||
}) | ||
.collect::<eyre::Result<Vec<_>>>()? | ||
.into_iter() | ||
.sum()) | ||
} | ||
|
||
#[test] | ||
fn test() -> Result<()> { | ||
assert_eq!(process(EXAMPLE2.as_bytes())?, 281); | ||
Ok(()) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
color_eyre::install()?; | ||
println!("{}", process(stdin().lock())?); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters