From bb72c12d9ef294d467780217a4bb951aa61ad1d7 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-part-1.txt | 1 + src/bin/quest-1-part-1.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 data/quest-1-part-1.txt create mode 100644 src/bin/quest-1-part-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-part-1.txt b/data/quest-1-part-1.txt new file mode 100644 index 0000000..7d4d1dd --- /dev/null +++ b/data/quest-1-part-1.txt @@ -0,0 +1 @@ +CCAAACACAACCAABAACABACBCCBBABBCAABCACBCBCCABAAACBBBCCACCBAACABBBBCBAAAACABACACBACACCCACACABABACCCCAABBABABBCCCBCBCBBCBBCACBBBAACCCBACBAABACABACAAACABCBCBCBCBCCBBAABBABAAABCCAABBCCABCACCCBBBCCAABBBCACACAACABCCACBBBBBCCCABBACCBAACABCAACCCCABBBCCCBCBBBCABACCCCCCACBBBCBACABCABBBCCBBBCCABACCBCCBBAABABBCCBCBABCCCAACAACCBBCCCACACAACAAABABBCBAABABCCCABABCAABABBBCBCCAAABABCAACAABBACCBCBABAABCAAABBBACBCACAACCBAACACABCBABABBAAACBAACCCCBCABAACBBABCBCABCAABABABACBAABCCABCABBCBCABBBBBABBBBAAABBACBAACBCABCCBCBACCBABBCBACCABAAAABCAACAABCABAABCCCAAABBAAABAACCAACCBBCBCBAAAAAABBAAABABBCCCBAACCABACCCBBBBBCBABCACCACBBACBBBBCABBCCCAACBACBAAABACACCABBCBCBACABBCACBABAAAACCCBAACACACBCCBBBAACCAACBBACCCBABACAACBCABBBACBBBBCCBCCBBCBBBBAACAACBBCCCBABBABBBBCAACBBACBACCACAAACCCBBCCABBCABCCBBBCAABBACBCBBAABCACBCBABCBBBACBAAAACBABCBABAACABCCABCBBAABCAACCCCABCBBBCCCBBBCABBBABBCACACCCCCBABCABBCCCAABACBCAAACCCCBBAACABBAACCBBCBBBABBBBAABCBCACABACBACBBBBACACCAACAABACBBBAACCBABBBBABBBBAAACBAAAAABBCCCACCACAACBAABBCAAABCCCCCB diff --git a/src/bin/quest-1-part-1.rs b/src/bin/quest-1-part-1.rs new file mode 100644 index 0000000..aad509b --- /dev/null +++ b/src/bin/quest-1-part-1.rs @@ -0,0 +1,34 @@ +use std::{collections::HashMap, path::PathBuf}; + +type Result = std::result::Result>; + +fn main() -> Result<()> { + let file_path = PathBuf::from("./data/quest-1-part-1.txt"); + let data = std::fs::read_to_string(file_path)?; + let counter = quest_part_1(&data); + println!("Quest 1 / Part 1: {counter}"); + Ok(()) +} + +fn quest_part_1(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_part_1; + + #[test] + fn sample() { + let data = "ABBAC"; + let result = quest_part_1(data); + assert_eq!(result, 5); + } +}