Quest 1 / Part 1

This commit is contained in:
Paul Campbell 2024-11-10 13:20:05 +00:00
parent 2043541679
commit 9fbea909f0
4 changed files with 52 additions and 1 deletions

4
.gitignore vendored
View file

@ -19,3 +19,7 @@ Cargo.lock
# and can be added to the global gitignore or merged into this file. For a more nuclear # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Added by cargo
/target

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "ec"
version = "0.1.0"
edition = "2021"
[dependencies]

1
data/quest-1.txt Normal file
View file

@ -0,0 +1 @@
CCAAACACAACCAABAACABACBCCBBABBCAABCACBCBCCABAAACBBBCCACCBAACABBBBCBAAAACABACACBACACCCACACABABACCCCAABBABABBCCCBCBCBBCBBCACBBBAACCCBACBAABACABACAAACABCBCBCBCBCCBBAABBABAAABCCAABBCCABCACCCBBBCCAABBBCACACAACABCCACBBBBBCCCABBACCBAACABCAACCCCABBBCCCBCBBBCABACCCCCCACBBBCBACABCABBBCCBBBCCABACCBCCBBAABABBCCBCBABCCCAACAACCBBCCCACACAACAAABABBCBAABABCCCABABCAABABBBCBCCAAABABCAACAABBACCBCBABAABCAAABBBACBCACAACCBAACACABCBABABBAAACBAACCCCBCABAACBBABCBCABCAABABABACBAABCCABCABBCBCABBBBBABBBBAAABBACBAACBCABCCBCBACCBABBCBACCABAAAABCAACAABCABAABCCCAAABBAAABAACCAACCBBCBCBAAAAAABBAAABABBCCCBAACCABACCCBBBBBCBABCACCACBBACBBBBCABBCCCAACBACBAAABACACCABBCBCBACABBCACBABAAAACCCBAACACACBCCBBBAACCAACBBACCCBABACAACBCABBBACBBBBCCBCCBBCBBBBAACAACBBCCCBABBABBBBCAACBBACBACCACAAACCCBBCCABBCABCCBBBCAABBACBCBBAABCACBCBABCBBBACBAAAACBABCBABAACABCCABCBBAABCAACCCCABCBBBCCCBBBCABBBABBCACACCCCCBABCABBCCCAABACBCAAACCCCBBAACABBAACCBBCBBBABBBBAABCBCACABACBACBBBBACACCAACAABACBBBAACCBABBBBABBBBAAACBAAAAABBCCCACCACAACBAABBCAAABCCCCCB

40
src/bin/quest-1.rs Normal file
View file

@ -0,0 +1,40 @@
use std::{collections::HashMap, path::PathBuf};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
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);
}
}