Select Git revision

Evy Storozhenko authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.rs 1.63 KiB
use anyhow::Result;
use std::fs;
fn remove_art(line: &str) -> (u32, u32) {
let patterns = [
["one", "1"],
["two", "2"],
["three", "3"],
["four", "4"],
["five", "5"],
["six", "6"],
["seven", "7"],
["eight", "8"],
["nine", "9"],
];
let a = patterns
.iter()
.filter_map(|v| {
if let Some(found) = v.iter().filter_map(|v| line.find(v)).min() {
Some((v[1].parse::<u32>().unwrap(), found))
} else {
None
}
})
.min_by(|x, y| x.1.cmp(&y.1))
.map(|v| v.0.to_owned())
.unwrap();
let b = patterns
.iter()
.filter_map(|v| {
if let Some(found) = v.iter().filter_map(|v| line.rfind(v)).max() {
Some((v[1].parse::<u32>().unwrap(), found))
} else {
None
}
})
.max_by(|x, y| x.1.cmp(&y.1))
.map(|v| v.0.to_owned())
.unwrap();
(a, b)
}
fn main() -> Result<()> {
let data = fs::read_to_string("data.txt")?;
let result_a: u32 = data
.lines()
.map(|v| {
let a = v.chars().find(|v| v.is_ascii_digit()).unwrap();
let b = v.chars().rev().find(|v| v.is_ascii_digit()).unwrap();
format!("{a}{b}").parse::<u32>().unwrap()
})
.sum();
println!("{result_a}");
let result_b: u32 = data
.lines()
.map(|v| {
let (a, b) = remove_art(v);
format!("{a}{b}").parse::<u32>().unwrap()
})
.sum();
println!("{result_b}");
Ok(())
}