day one part two

This commit is contained in:
Zynh0722 2023-12-01 04:35:54 -08:00
parent f0e5a71c84
commit 4202844320
5 changed files with 45 additions and 0 deletions

View file

@ -3,6 +3,14 @@ name = "one"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "pone"
path = "src/one.rs"
[[bin]]
name = "ptwo"
path = "src/two.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
one/output-two Normal file
View file

@ -0,0 +1 @@
54418

36
one/src/two.rs Normal file
View file

@ -0,0 +1,36 @@
use std::io::{self, Read};
fn main() {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).unwrap();
let buf = buf.replace("twone", "twoone");
let buf = buf.replace("threeight", "threeeight");
let buf = buf.replace("oneight", "oneeight");
let buf = buf.replace("eightwo", "eighttwo");
let buf = buf.replace("eighthree", "eightthree");
let buf = buf.replace("nineight", "nineeight");
let buf = buf.replace("one", "1");
let buf = buf.replace("two", "2");
let buf = buf.replace("three", "3");
let buf = buf.replace("four", "4");
let buf = buf.replace("five", "5");
let buf = buf.replace("six", "6");
let buf = buf.replace("seven", "7");
let buf = buf.replace("eight", "8");
let buf = buf.replace("nine", "9");
let lines: Vec<String> = buf.lines().map(ToOwned::to_owned).collect();
let answer: Result<i32, _> = lines
.into_iter()
.map(|l| l.chars().filter(char::is_ascii_digit).collect())
.map(|s: String| [s.chars().next().unwrap(), s.chars().last().unwrap()].to_owned())
.map(String::from_iter)
.map(|s| s.parse::<i32>())
.sum();
let answer = answer.unwrap();
print!("{answer}");
}