From 42028443207d8b0edd9e727e355ee41ec2747f7c Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Fri, 1 Dec 2023 04:35:54 -0800 Subject: [PATCH] day one part two --- one/Cargo.toml | 8 ++++++++ one/{output => output-one} | 0 one/output-two | 1 + one/src/{main.rs => one.rs} | 0 one/src/two.rs | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+) rename one/{output => output-one} (100%) create mode 100644 one/output-two rename one/src/{main.rs => one.rs} (100%) create mode 100644 one/src/two.rs diff --git a/one/Cargo.toml b/one/Cargo.toml index dcf4350..342a5c1 100644 --- a/one/Cargo.toml +++ b/one/Cargo.toml @@ -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] diff --git a/one/output b/one/output-one similarity index 100% rename from one/output rename to one/output-one diff --git a/one/output-two b/one/output-two new file mode 100644 index 0000000..c2b86cf --- /dev/null +++ b/one/output-two @@ -0,0 +1 @@ +54418 \ No newline at end of file diff --git a/one/src/main.rs b/one/src/one.rs similarity index 100% rename from one/src/main.rs rename to one/src/one.rs diff --git a/one/src/two.rs b/one/src/two.rs new file mode 100644 index 0000000..c548c01 --- /dev/null +++ b/one/src/two.rs @@ -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 = buf.lines().map(ToOwned::to_owned).collect(); + + let answer: Result = 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::()) + .sum(); + + let answer = answer.unwrap(); + + print!("{answer}"); +}