day 2
This commit is contained in:
parent
cb25f1c89c
commit
b9db43d7c1
4 changed files with 95 additions and 0 deletions
35
Cargo.lock
generated
35
Cargo.lock
generated
|
@ -2,6 +2,41 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
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]]
|
[[package]]
|
||||||
name = "aoc2022"
|
name = "aoc2022"
|
||||||
version = "0.1.0"
|
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"
|
||||||
|
|
|
@ -6,3 +6,5 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
regex = "1.7"
|
||||||
|
|
||||||
|
|
29
examples/day02_1.rs
Normal file
29
examples/day02_1.rs
Normal file
|
@ -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<dyn std::error::Error>> {
|
||||||
|
let lines: Vec<String> = 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(())
|
||||||
|
}
|
29
examples/day02_2.rs
Normal file
29
examples/day02_2.rs
Normal file
|
@ -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<dyn std::error::Error>> {
|
||||||
|
let lines: Vec<String> = 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(())
|
||||||
|
}
|
Loading…
Reference in a new issue