Quest 1 / Part 1

This commit is contained in:
Paul Campbell 2024-11-10 13:20:05 +00:00
parent 2043541679
commit bb72c12d9e
4 changed files with 46 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-part-1.txt Normal file
View file

@ -0,0 +1 @@
CCAAACACAACCAABAACABACBCCBBABBCAABCACBCBCCABAAACBBBCCACCBAACABBBBCBAAAACABACACBACACCCACACABABACCCCAABBABABBCCCBCBCBBCBBCACBBBAACCCBACBAABACABACAAACABCBCBCBCBCCBBAABBABAAABCCAABBCCABCACCCBBBCCAABBBCACACAACABCCACBBBBBCCCABBACCBAACABCAACCCCABBBCCCBCBBBCABACCCCCCACBBBCBACABCABBBCCBBBCCABACCBCCBBAABABBCCBCBABCCCAACAACCBBCCCACACAACAAABABBCBAABABCCCABABCAABABBBCBCCAAABABCAACAABBACCBCBABAABCAAABBBACBCACAACCBAACACABCBABABBAAACBAACCCCBCABAACBBABCBCABCAABABABACBAABCCABCABBCBCABBBBBABBBBAAABBACBAACBCABCCBCBACCBABBCBACCABAAAABCAACAABCABAABCCCAAABBAAABAACCAACCBBCBCBAAAAAABBAAABABBCCCBAACCABACCCBBBBBCBABCACCACBBACBBBBCABBCCCAACBACBAAABACACCABBCBCBACABBCACBABAAAACCCBAACACACBCCBBBAACCAACBBACCCBABACAACBCABBBACBBBBCCBCCBBCBBBBAACAACBBCCCBABBABBBBCAACBBACBACCACAAACCCBBCCABBCABCCBBBCAABBACBCBBAABCACBCBABCBBBACBAAAACBABCBABAACABCCABCBBAABCAACCCCABCBBBCCCBBBCABBBABBCACACCCCCBABCABBCCCAABACBCAAACCCCBBAACABBAACCBBCBBBABBBBAABCBCACABACBACBBBBACACCAACAABACBBBAACCBABBBBABBBBAAACBAAAAABBCCCACCACAACBAABBCAAABCCCCCB

34
src/bin/quest-1-part-1.rs Normal file
View file

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