From 9fbea909f0449ad6225ee3a6b6e6d3d2d0ec2118 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 10 Nov 2024 13:20:05 +0000 Subject: [PATCH] Quest 1 / Part 1 --- .gitignore | 6 +++++- Cargo.toml | 6 ++++++ data/quest-1.txt | 1 + src/bin/quest-1.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 data/quest-1.txt create mode 100644 src/bin/quest-1.rs diff --git a/.gitignore b/.gitignore index d01bd1a..efe3eb1 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,8 @@ Cargo.lock # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0e605b3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ec" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/data/quest-1.txt b/data/quest-1.txt new file mode 100644 index 0000000..7d4d1dd --- /dev/null +++ b/data/quest-1.txt @@ -0,0 +1 @@ +CCAAACACAACCAABAACABACBCCBBABBCAABCACBCBCCABAAACBBBCCACCBAACABBBBCBAAAACABACACBACACCCACACABABACCCCAABBABABBCCCBCBCBBCBBCACBBBAACCCBACBAABACABACAAACABCBCBCBCBCCBBAABBABAAABCCAABBCCABCACCCBBBCCAABBBCACACAACABCCACBBBBBCCCABBACCBAACABCAACCCCABBBCCCBCBBBCABACCCCCCACBBBCBACABCABBBCCBBBCCABACCBCCBBAABABBCCBCBABCCCAACAACCBBCCCACACAACAAABABBCBAABABCCCABABCAABABBBCBCCAAABABCAACAABBACCBCBABAABCAAABBBACBCACAACCBAACACABCBABABBAAACBAACCCCBCABAACBBABCBCABCAABABABACBAABCCABCABBCBCABBBBBABBBBAAABBACBAACBCABCCBCBACCBABBCBACCABAAAABCAACAABCABAABCCCAAABBAAABAACCAACCBBCBCBAAAAAABBAAABABBCCCBAACCABACCCBBBBBCBABCACCACBBACBBBBCABBCCCAACBACBAAABACACCABBCBCBACABBCACBABAAAACCCBAACACACBCCBBBAACCAACBBACCCBABACAACBCABBBACBBBBCCBCCBBCBBBBAACAACBBCCCBABBABBBBCAACBBACBACCACAAACCCBBCCABBCABCCBBBCAABBACBCBBAABCACBCBABCBBBACBAAAACBABCBABAACABCCABCBBAABCAACCCCABCBBBCCCBBBCABBBABBCACACCCCCBABCABBCCCAABACBCAAACCCCBBAACABBAACCBBCBBBABBBBAABCBCACABACBACBBBBACACCAACAABACBBBAACCBABBBBABBBBAAACBAAAAABBCCCACCACAACBAABBCAAABCCCCCB diff --git a/src/bin/quest-1.rs b/src/bin/quest-1.rs new file mode 100644 index 0000000..4aaaa71 --- /dev/null +++ b/src/bin/quest-1.rs @@ -0,0 +1,40 @@ +use std::{collections::HashMap, path::PathBuf}; + +type Result = std::result::Result>; + +fn main() -> Result<()> { + println!("Quest 1!"); + let file_path = PathBuf::from("./data/quest-1.txt"); + + let data = std::fs::read_to_string(file_path)?; + + let counter = quest(&data); + + println!("Result: {counter}"); + + Ok(()) +} + +fn quest(data: &str) -> i32 { + let table = HashMap::from([('A', 0), ('B', 1), ('C', 3)]); + + let mut counter = 0; + for beast in data.chars() { + if let Some(potions) = table.get(&beast) { + counter += potions; + } + } + counter +} + +#[cfg(test)] +mod tests { + use super::quest; + + #[test] + fn sample() { + let data = "ABBAC"; + let result = quest(data); + assert_eq!(result, 5); + } +}