Refactor to use common lib

Added solution for AoC 2022/2

Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
main
TuDatTr 2022-12-02 08:37:33 +01:00
parent be832438c5
commit 8cd8032741
14 changed files with 872 additions and 31 deletions

1
2022/common/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

95
2022/common/Cargo.lock generated Normal file
View File

@ -0,0 +1,95 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"tracing",
]
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "proc-macro2"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tracing"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
dependencies = [
"once_cell",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"

8
2022/common/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "common"
version = "0.1.0"
edition = "2021"
[dependencies]
tracing = "0.1.0"

22
2022/common/src/lib.rs Normal file
View File

@ -0,0 +1,22 @@
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use tracing::{debug, info};
pub fn create_file(path: &Path, content: String) {
info!("Creating file");
debug!("Writing file from: {:?}\nWith:\n{}", path, content);
let mut file = File::create(path).unwrap();
let _ = file.write_all(content.as_bytes());
}
pub fn read_file(path: &Path) -> std::io::Result<String> {
info!("Reading file");
debug!("Reading file from: {:?}", path);
let mut file = File::open(path)?;
let mut contents = String::new();
let _ = file.read_to_string(&mut contents)?;
Ok(contents.trim().to_string())
}

View File

@ -57,11 +57,19 @@ dependencies = [
"os_str_bytes", "os_str_bytes",
] ]
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"tracing",
]
[[package]] [[package]]
name = "day-1_calorie_counting" name = "day-1_calorie_counting"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"common",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]

View File

@ -19,4 +19,5 @@ path = "src/bin.rs"
[dependencies] [dependencies]
clap = {version = "4.0.22", features = ["derive"]} clap = {version = "4.0.22", features = ["derive"]}
tracing = "0.1" tracing = "0.1"
tracing-subscriber = {version = "0.3.16", features = ["env-filter"]} tracing-subscriber = {version = "0.3.16", features = ["env-filter"]}
common = {path = "../common/"}

View File

@ -2,7 +2,7 @@ use clap::Parser;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about="CLI tool to output latency at a given gps location.", long_about = None)] #[command(author, version, about=None, long_about = None)]
pub struct Cli { pub struct Cli {
/// Path to the input file. /// Path to the input file.
#[arg(short, long)] #[arg(short, long)]

View File

@ -1,35 +1,14 @@
use std::fs::File; use std::path::PathBuf;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::collections::BinaryHeap; use std::collections::BinaryHeap;
use tracing::{debug, info}; use tracing::{debug, info};
use common::read_file;
mod elf; mod elf;
use crate::elf::Elf; use crate::elf::Elf;
#[allow(dead_code)]
fn create_file(path: &Path, content: String) {
info!("Creating file");
debug!("Writing file from: {:?}\nWith:\n{}", path, content);
let mut file = File::create(path).unwrap();
let _ = file.write_all(content.as_bytes());
}
#[allow(dead_code)]
fn read_file(path: &Path) -> std::io::Result<String> {
info!("Reading file");
debug!("Reading file from: {:?}", path);
let mut file = File::open(path)?;
let mut contents = String::new();
let _ = file.read_to_string(&mut contents)?;
Ok(contents.trim().to_string())
}
/// Finds the Elf carrying the most Calories. /// Finds the Elf carrying the most Calories.
/// Outputs how many total Calories that Elf is carrying. /// Outputs how many total Calories that Elf is carrying.
#[allow(dead_code)]
pub fn task_1a(input: &PathBuf) { pub fn task_1a(input: &PathBuf) {
debug!("Running task 1a"); debug!("Running task 1a");
info!("Running task 1a with {:?}", input); info!("Running task 1a with {:?}", input);
@ -41,7 +20,6 @@ pub fn task_1a(input: &PathBuf) {
/// Finds the top three Elves carrying the most Calories. /// Finds the top three Elves carrying the most Calories.
/// Outputs how many Calories those Elves are carrying in total. /// Outputs how many Calories those Elves are carrying in total.
#[allow(dead_code)]
pub fn task_1b(input: &PathBuf) { pub fn task_1b(input: &PathBuf) {
debug!("Running task 1b"); debug!("Running task 1b");
info!("Running task 1b with {:?}", input); info!("Running task 1b with {:?}", input);
@ -64,25 +42,26 @@ mod tests {
use super::*; use super::*;
use std::fs::remove_file; use std::fs::remove_file;
use std::str::from_utf8; use std::str::from_utf8;
use common::{create_file, read_file};
const PATH: &str = "input.txt"; const PATH: &str = "input.txt";
const CONTENT: &[u8; 55] = const CONTENT: &[u8; 54] =
b"1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n"; b"1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000";
fn create_test_setup() { fn create_test_setup() {
let file_path = Path::new(PATH); let file_path = &PathBuf::from(PATH);
let original = CONTENT; let original = CONTENT;
create_file(file_path, from_utf8(original).unwrap().to_string()); create_file(file_path, from_utf8(original).unwrap().to_string());
} }
fn cleanup_test_setup() { fn cleanup_test_setup() {
let _ = remove_file(Path::new(PATH)); let _ = remove_file(&PathBuf::from(PATH));
} }
/// Test if we corretly read file input. /// Test if we corretly read file input.
#[test] #[test]
fn file_io() { fn file_io() {
create_test_setup(); create_test_setup();
let content = read_file(Path::new(PATH)); let content = read_file(&PathBuf::from(PATH));
cleanup_test_setup(); cleanup_test_setup();
assert_eq!(from_utf8(CONTENT).unwrap(), content.unwrap()); assert_eq!(from_utf8(CONTENT).unwrap(), content.unwrap());
} }

View File

@ -0,0 +1,2 @@
/input
/target

528
2022/day-2_rock_paper_scissors/Cargo.lock generated Normal file
View File

@ -0,0 +1,528 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d"
dependencies = [
"bitflags",
"clap_derive",
"clap_lex",
"is-terminal",
"once_cell",
"strsim",
"termcolor",
]
[[package]]
name = "clap_derive"
version = "4.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"tracing",
]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [
"libc",
]
[[package]]
name = "io-lifetimes"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "linux-raw-sys"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata",
]
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "pin-project-lite"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "rock_paper_scissors"
version = "0.1.0"
dependencies = [
"clap",
"common",
"strum",
"strum_macros",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "rustix"
version = "0.36.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb93e85278e08bb5788653183213d3a60fc242b10cb9be96586f5a73dcb67c23"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "rustversion"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8"
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]]
name = "smallvec"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strum"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f"
[[package]]
name = "strum_macros"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]]
name = "syn"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "thread_local"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180"
dependencies = [
"once_cell",
]
[[package]]
name = "tracing"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922"
dependencies = [
"lazy_static",
"log",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
[[package]]
name = "windows_i686_gnu"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
[[package]]
name = "windows_i686_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"

View File

@ -0,0 +1,23 @@
[package]
name = "rock_paper_scissors"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "rock_paper_scissors_lib"
path = "src/lib.rs"
[[bin]]
name = "rock_paper_scissors"
path = "src/bin.rs"
[dependencies]
common = {path = "../common/"}
clap = {version = "4.0.22", features = ["derive"]}
tracing = "0.1"
tracing-subscriber = {version = "0.3.16", features = ["env-filter"]}
strum = "0.24.1"
strum_macros = "0.24"

View File

@ -0,0 +1,16 @@
use rock_paper_scissors_lib::{task_1a, task_1b};
use clap::Parser;
use tracing::debug;
mod cli;
use crate::cli::Cli;
pub fn main() {
tracing_subscriber::fmt::init();
let args = Cli::parse();
debug!("Args: {:?}", args);
println!("Result (1a): {}", task_1a(&args.input));
println!("Result (1b): {}", task_1b(&args.input));
}

View File

@ -0,0 +1,10 @@
use std::path::PathBuf;
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about=None, long_about = None)]
pub struct Cli {
/// Path to input file
#[arg(short, long)]
pub input: PathBuf
}

View File

@ -0,0 +1,148 @@
use common::read_file;
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter};
use std::path::PathBuf;
use tracing::{info, debug};
#[derive(Display, Clone, Eq, PartialEq, EnumIter)]
enum Choice {
Rock,
Paper,
Scissors,
}
#[derive(Display, Clone, Eq, PartialEq)]
enum GameResult {
Win,
Loose,
Draw
}
impl Choice {
fn win(i: &Choice, other: &Choice) -> GameResult {
debug!("Get GameResult based on {}, {}", i, other);
info!("Get GameResult");
match (i, other) {
(Choice::Rock, Choice::Scissors) => GameResult::Win,
(Choice::Paper, Choice::Rock) => GameResult::Win,
(Choice::Scissors, Choice::Paper) => GameResult::Win,
_ if i == other => GameResult::Draw,
_ => GameResult::Loose
}
}
fn play(&self, other: &Choice) -> u64 {
debug!("Get score based on {}, {}", &self, other);
info!("Get score");
let hand_value = match self {
Choice::Rock => 1,
Choice::Paper => 2,
Choice::Scissors => 3,
};
let game_value = match Choice::win(self, other) {
GameResult::Win => 6,
GameResult::Draw => 3,
GameResult::Loose => 0,
};
hand_value + game_value
}
fn cheat(&self, outcome: &GameResult) -> u64 {
debug!("Get score by cheating based on {}, {}", &self, outcome);
info!("Get score by cheating");
let user_choice = Choice::iter().filter(|c| &Choice::win(&c, &self) == outcome).next();
user_choice.unwrap().play(&self)
}
}
fn calculate_result_1a(line: &str) -> u64 {
debug!("Get score for 1a of game {}", line);
info!("Get score for 1a");
let mut input = line.chars();
let opponent_choice = match input.next() {
Some('A') => Choice::Rock,
Some('B') => Choice::Paper,
Some('C') => Choice::Scissors,
_ => Choice::Rock,
};
input.next();
let user_choice = match input.next() {
Some('X') => Choice::Rock,
Some('Y') => Choice::Paper,
Some('Z') => Choice::Scissors,
_ => Choice::Rock,
};
user_choice.play(&opponent_choice)
}
fn calculate_result_1b(line: &str) -> u64 {
debug!("Get score for 1b of game {}", line);
info!("Get score for 1b");
let mut input = line.chars();
let opponent_choice = match input.next() {
Some('A') => Choice::Rock,
Some('B') => Choice::Paper,
Some('C') => Choice::Scissors,
_ => Choice::Rock, // Error handling, this shouldn't happen
};
input.next();
let outcome = match input.next() {
Some('X') => GameResult::Loose,
Some('Y') => GameResult::Draw,
Some('Z') => GameResult::Win,
_ => GameResult::Loose, // Error handling, this shouldn't happen
};
opponent_choice.cheat(&outcome)
}
pub fn task_1a(input: &PathBuf) -> u64 {
let content = read_file(input).unwrap();
content.split('\n').map(|l| calculate_result_1a(l)).sum()
}
pub fn task_1b(input: &PathBuf) -> u64 {
let content = read_file(input).unwrap();
content.split('\n').map(|l| calculate_result_1b(l)).sum()
}
#[cfg(test)]
mod tests {
use super::*;
use common::create_file;
use std::fs::remove_file;
const PATH: &str = "input.txt";
const CONTENT: &str = "A Y\nB X\nC Z";
#[test]
fn test_task_1a() {
let test_file = PathBuf::from(PATH);
create_file(&test_file, CONTENT.to_string());
let result = task_1a(&test_file);
let _ = remove_file(&test_file);
let expected = 15u64;
assert_eq!(result, expected);
}
#[test]
fn test_task_1b() {
let test_file = PathBuf::from(PATH);
create_file(&test_file, CONTENT.to_string());
let result = task_1b(&test_file);
let _ = remove_file(&test_file);
let expected = 12u64;
assert_eq!(result, expected);
}
}