diff --git a/Cargo.lock b/Cargo.lock index d37200f..a8b886b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,41 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "aoc2022" version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" diff --git a/Cargo.toml b/Cargo.toml index c1cd145..24a0910 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex = "1.7" + diff --git a/examples/day02_1.rs b/examples/day02_1.rs new file mode 100644 index 0000000..e55e7f0 --- /dev/null +++ b/examples/day02_1.rs @@ -0,0 +1,29 @@ +fn score(a: char, b: char) -> i32 { + match (a, b) { + ('A', 'X') => 1 + 3, + ('A', 'Y') => 2 + 6, + ('A', 'Z') => 3 + 0, + ('B', 'X') => 1 + 0, + ('B', 'Y') => 2 + 3, + ('B', 'Z') => 3 + 6, + ('C', 'X') => 1 + 6, + ('C', 'Y') => 2 + 0, + ('C', 'Z') => 3 + 3, + _ => todo!() + } +} + +fn main() -> Result<(), Box> { + let lines: Vec = std::io::stdin().lines().map(|x| x.unwrap()).collect(); + let mut total = 0; + for line in lines { + if line.is_empty() { continue; } + let mut chars = line.chars(); + let a = chars.next().unwrap(); + chars.next(); + let b = chars.next().unwrap(); + total += score(a, b); + } + println!("{}", total); + Ok(()) +} diff --git a/examples/day02_2.rs b/examples/day02_2.rs new file mode 100644 index 0000000..68cf8f4 --- /dev/null +++ b/examples/day02_2.rs @@ -0,0 +1,29 @@ +fn score(a: char, b: char) -> i32 { + match (a, b) { + ('A', 'X') => 3 + 0, + ('A', 'Y') => 1 + 3, + ('A', 'Z') => 2 + 6, + ('B', 'X') => 1 + 0, + ('B', 'Y') => 2 + 3, + ('B', 'Z') => 3 + 6, + ('C', 'X') => 2 + 0, + ('C', 'Y') => 3 + 3, + ('C', 'Z') => 1 + 6, + _ => todo!() + } +} + +fn main() -> Result<(), Box> { + let lines: Vec = std::io::stdin().lines().map(|x| x.unwrap()).collect(); + let mut total = 0; + for line in lines { + if line.is_empty() { continue; } + let mut chars = line.chars(); + let a = chars.next().unwrap(); + chars.next(); + let b = chars.next().unwrap(); + total += score(a, b); + } + println!("{}", total); + Ok(()) +}