From 645f0e7fd494d8db798ce9619a391e295d4c951c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 10 Nov 2024 14:28:06 +0000 Subject: [PATCH] Quest 1 / Part 3 --- data/quest-1-part-3.txt | 1 + src/bin/quest-1-part-3.rs | 88 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 data/quest-1-part-3.txt create mode 100644 src/bin/quest-1-part-3.rs diff --git a/data/quest-1-part-3.txt b/data/quest-1-part-3.txt new file mode 100644 index 0000000..ff7b545 --- /dev/null +++ b/data/quest-1-part-3.txt @@ -0,0 +1 @@ +CCAxDAxBBDBAAAABDBCCxAxCxBxBDDDABDADBAADAABAADADDABBxCACDACxDAAxDBxDCCAACCCxxDxCCDBDxxxCDAxDBDCBxxBDDBDxDABBCxBCDDCDADxxCCBACCBDDBCAABCCBDCCxAxABxDxBBCBDABDBBDABxDDABDCxxxACABBxCBACABxAAxACDBxACBDBACxDCBAAACBBCxDAxDCAABxxBACBDBCxAAADDxxDDBCABBCBACDDBBxCxBxACxxADBAADCCxCBBCDxCCACxxDDxCDDCCABCDABDABDABABBBACDDCxDDDDDxxBBDAxBBxDBDBBxCDxDADDxxDxBCBDBDBCBBACCCxDCDABCBDBACDACAAADDDCCBCxxBADCxDBBCCxxxBBxBCxxDCxDCCCDxxDACDCAABCBxACADCxDDDDxBBCACBDxCBCxDxxBAAAxxCxxxBDxDCDxDBxCxxAABBxABCBBACCDCDADxDACDBBBDCCxADDBADxBCxxAADACAAxCBCDBCxCDBBAAxCAACCxCDAABAAxxxxAADCCDxCxCxAABDABCDBACBDCDxxxABCBxCDDADAACCABAxCADADxAxDDDBDAxAxxCACDDADDCDABDAxCBxCAACxCBxxAACDCCxCCDAxDACCAAADCBDDAxxBBDCxAxxDDCBBDBxDCBDCAAxBCxBAxDCADBAACxCBDCxAABCxAADBDDCDCACCBBDCBxAAxDABxDACxAAxxADDCBBDAABCxADxAxBAAAxADCDCCDABACDDCxDBCACBDCDCCxxCxCCxCBDCDCxABADBADDBBxCCBCACABxABxCBBAABCAABCBDDADADDCBDAADCABDCDCBCCCCBCDCxADCCxAxxCBxxxxxCCABBDAACDBBxDBCDxxCxBBxDDCBxBADBBxCCAADxDCCDxCDDxCBBCDCCBAxDCxBxxCxABAACCBBAxBDxxBCBADCCBBAxxDAxCxxCDBxDxBxCACxAxCDDADCBBDACADAAABxBDDxDBAxAxxCCCCBxDDAxBCxBBBACBxCCDCDCAxCDABAAxBABBBABxADDACABDBBDAxCCCxDCAAADxxCxCBxADAADxDBAxDDDCAxCBCCDxDxABAxCDDDCCACBDCxCCxBDDCDACDBAABDADAxDBACBxDCxDCBABBxAxxBCBBDAxxBxxBCBxxxxBDADxAAAABCDDxxCxDCACDDCxADDCDBxCDDBBxCDDDCAxBAxADACDDAABxxxxxBDDxxAxACACxxxBDDCAxBABBDDBAxBAxABCDABACAxxCDCCDxCxBAxBxACDBCBDCCCDDCDDCAxxDAADxCAxBBCBDBxDxxDDxACCxBADBABBBBBBxxDACCxBACCACAxDBADDCDCDCCxABCDDxDxDCxDxAABADCxBxCAxCxCCACAABAxBBACCxxDBCxBxACADDDADCCxCAxADAxBxBDxxDxAxACBxBCxAAABAADAxDxDCxxDBADBAADCBDxBxBDAxxCBABxDxxBAxAABCCxDBABBABxDADBDxBDAAACBxDABxDDDAxxBBBCCCDCDCCDDAxCxCBBCxxxxCCCACADxACBBACBAADCxxxAxACCxABxxADADDCDCBDBxBABDAxDDxADCxxACCDAAAxBDADBBCAABAxBxABxCCADCCAADABxAxxABCBxCABxCBADCACDDBxADxCDBBDxAxBDBCDABBDBxDxCBAAAxAxCABCBCDDCDxxBAxDCADDBACxBDBACCxDBABABAADxDCDDxDAACCAACCDCCDCBxxxACxAAxACxCADBCAABCAACDxCCxBBAxxCxDxCxCAxACCCxAAxxCABxABADAxxACDDCABABDxCBABBDAxDDxDAxDxAADDAADBBDxDABxDxDDCDBCADBxAxDDAxAABCBAxBBBCBCCCDBBCACBCBDACxADxDDDDABxBDAADADCACDxxCCADBDBBADxADDxCDAADDBBBBxxAABDBxDCACBxCCCBBCxCDCACABBDCABACAACDCBAxxABBADBxAxDCABABxACCCDxAxBBxxABBCCDDxAxDCxDACBDCCCBCxADxBxAxBDxxABxADDAxADBxBDDCDBDxxDADABxBxABDBCCxBDDAxDxDCAxAACDADBCACCxABBDABDCCDDACxxCCxDCxABDDCBxDxCAxBCBCAxBxxDABABCxDCxxxCBBAABDxDACxBAxCADABBDBACADCxDxCCBDxCxABDACDBDDxCADABAAxAxxBCBAxCCADCxACCDAADAAAxxxCDBDDDBCCxAxxACDCxDAxAAABxCxADBCAxCAADDCDACBDDACCCDBxxDCBBxDCDxDDABCDDBAACCBBBBxxDxDDDBDBBxBDAxBAADDDxCxxxBDxAxCBBAxBCBxDxDBAxABxBBACDxBBCxBxxDxCxxBDCDBCAABDACxBCBDABBCACACCxBCxBxDCADDxCAxDDABAADBxBCBCAxBDACxCAxCDxxDCCBDDxBABBBCDxBAACBCxxxCxBxADxxADAxAAxAADDABCDAADxAxBDDxxBDBBACDCCDDADCxBCBxADDDACBxCADDDDBDAxCABAxCDCBxADBBCABxBCAxxBxDxBBCACxADAAxDxAxADxCBAAxCxCCBAACBCAxxxDDxADAABCDABCABxADBxAAxACCCCBDABBCxCCBABCxDABDBACCBDBABBxABAxCCBCxxBACxBCxBxxDxCDBxABCBBAABADBxBCxCCCCACCCBBACDxCCBAxBBBxDDAABCxCDBxBCxCBxCDADDBxCxxxDDBDCBDxBADCxCDDxDCxxCxxxCADxxCCDxCCxxCAAABxDDBxADBxxCDBBDBxDAxADCCBCBCDABDADADDBBAxxDBxDAxDDDBxBCBDxCAABACDxCDBxBCADBDxAADCCDBCxxDxCCxDBAABBCCDADBDxCDADDDDADAxxDxBxDBADACAxAxCBDCBxCBxBDCACCCACBxCCCxDCAxBxADDBCDDxCCCBDBAACBBCDADxxABxBBDCxAxADCxCCxCBDBCCDDxDCxxBACDBDxBBADBCBxAxDACBBCDADCxAABxDACCxABDDDACxxxBBDxCCDAABCCxDxDDCDxBCDBDxAACBCDCBABxDDCCCDxDxxBBxxAxDAxCxBCBCxACxCDDCABxADADCxxDACBxBxxCCABBxBxxDBCAxCDBDABCABBDxAAAADAAAxCBCBBDxDCAACCCBxCBAxBCBBxADxDAxACBDDCxDCBBAxCBBCADCCDABxCACxDAxCCxxAxCBCABCADABBxAADCxxACxDBAABDBDDxBxBCxBDBDAACAxDADBDCCAAxADAADBDCABCBxxDAAAACDAABADACBxDDABDBACAxDCBCBAxCxCCAxDAABxxAxBBCxCAABBABCDCxBBxDBDDxACBAAxBCBCACABBDDCCAABCABAACACCBBCCDBBDBACCCABABDBBCBxxBAABBCBxAxxABACxDDDxxDCCDxxCDBBBxDCxxCBBxxDxCxDCxBABADDBBBxAxBACBCDDxADABxDDxBACBBCABCCAxxBxCCxDBBBxxAAxxxCCCBxBBAABCDBDBDDDBxCBAxBAABBBDDxxADDDAxDDADABDDCDBBBAxxBxCAxDxDCBBCCBDADCCCDBxDDADxCACxDxxADACCAADBCDCDADDxABADDCDCCxCBBxxCBBxxDDAxDBxxxBACDxxABDDBxDxDABABDCxxxBxxCCxACxABDDCACDABACADADBBxxxBADCCBACxBDCBCCxAABBDDBDBCCxxABCCxDBBAxxDDADCBCDBDBxDBCxDBxBAxCCDxACBDCDCxDCxCDCCCCxDCADxDAxACDCBBCDDABBAAxAADxCxCCACDBABDAACCDACAABCAABDBCAxDABCADBBDDDDxBxDxDCDCADDBDCxAAxAxCCxDDDBxxBCBBxBBxDADBDCxADAAxDCDxDxDxxxCACDCCCDACxxBDxxDAxxxCCAADCxACAABDBDABAAxBxBCAABxxCCCBxAABDBACxCACAxBAxCCAxCACCxACCABDxCADxCxBDxCDDBBACBBxBBDDCCCBDAAxxxDDDABxBBBxAACDDBDDDADxDAACDBDCCDCBBDCACABCDBADxABDxCAAxDDDAAxDCBxDBBxxABDBCACACCBxBBxDDxBABADACCDBCxxDAxBAAACCDAxDxxDBxBBBBCxAxABCDBBxCCxAAAADxABCxDBBDBxCxxACAxADAAxBxCDCADBxCCBDABCBxCxAxCDCCxCBDABAABCABBADDCACAxDDCxxACCxxBCACxDCCAxxDBDxDxADxABxADDBAxxACBBDABABBBACAxAADADxBxBxDDCBACxxBCBADADCDCAxBADxCCCBBBAAADAAxAxACxABxxBADxxBDBDDABBABxAAxDCADCCCCAxCxABxxCDABDCDAACADxCBCCxxxxxDAAxCxBDCBxDAxDxxDDCxCBDBDDxDDCACDxxACxCACCDBABxDBBADACBADABBAABBBDCCCBCxDDCDDBxCAxDACBBDABABDAxAxxCABxCBCDBDBDDACxBDBACCxAABDxBBCxCAxxAAABCBCDDACBCDDDBABABAAACADDDADxxDxBADxDCxABBxAACxxDBDAxCxDABCDBxCAADCACxADBCDxBxCDxBxDBDxxDxAABCDCBDACBDxCBBAACAADABBABDCACxDADxBADBABDBDDCDBADCBCADxCDDAxBxxCxxCBABDBDDBxBBBCBxCxADBxxDxABCDDBDBCxDADDBxDBBAxxAxDDAAAABDDxDxBCDBDAxBxABAxDDBDACxBDBCDAxDCCCAxDDDADAxxCBCCDxxCDAxACxABBAACBCBBACxxDCAAxCBABxAABDxAAxxAABCACCDABAABBDCCxxADDCDBCxBBCCDDxAADBCAADCCxxCDADDCBxBBBxDCCDBACDBDADAAAxxDDxAADBxCxxxDDxABAAxCBBDDAxxBDxDxBADCCDBACDBDBDxDACBxxDAABDADDBDAACBxACAABxBCDxAxBxABDxxxDCCxABCxBxBABBDAxBBCxADAxABxxxCAxBCxADCCDCBBDxBBABBBBxAAAxBCAxCBxxACCCBxBxxABDCDCCBBDCCCDDxxBCACABAAxxCAAABxDCCBxCCCxBBCBABABADADDCADBBBDCBADDBBBACBBxDxDCxCxAAxCxBCBDBADBAADCCxAAAAAADBxAxxxDBCDADxxDxDxCADCDDDACDCDBBBDBCBCCxCAAABAACACCxCDDDDACABBxCxxxxxxBDxDBDDxDCCDxDxxxBBCBACxxDADCBCxAADCAADDDCxxxxCxBxDAACxCCACxxDBDCxxCCAADAxCDBADDABADCAAAxDAxxCACCAxBxDDACBBADCAAxCCDCACCDBDBDBBBCCBCCCxCBCBxxDCCACABDxAAACDACCCDDCxBCBCBxAxDDDACxxBDDBAxADxCACxCCxxDADAAxDDAACAxDCxxABBDxACxCADADBADBCABBDDCCDBADxCBCDxBCABAADBCCAxxADBCBBBBADDxCAACABxADACAxBCAxCxxBDCxxBxDCDBDACCCABDxDDCBADCCADxxDBxDxBADDCDDBBxCDDBDBABBxxBBCDCBAxxBAxCxCCDADAAAAxxBCBxBCxDxDAxxBADDBBBADCADBxBACxxCCBAxCCDxABxDxDBxxCxCxBABDCxBBDCxxxCxCxCCAxAxxABxCCxAAxBxBBAAxAxDBxAxBDDxBDCABAxDCADABxCBABCCDCBxAxDxxAxCABAAACBCCBACBCCBBADACCCxDABCCADDBDBxCCADCCBDxADBCABDxCBBBCDCxBBxxACxBADAxxDCDAxCDDBDxCACBAxBDxAxBBAxBxxCCxDxCDCDDxxAADDAACAxBxABAACCBxxDxBBDxCBDBACBBBCCBDBDDCxDAxADABDxBCBBCCBxBAABxxAxDBxCAxBACxBBxxxxxCAAxxCxDDDCCAACCCBBADACCDxBACCBDxACDDDBBADCCxABDACABABxBBDCDAxxADxDxBCBBCxAxCBABACxxDDBDCCBxxBCDDACCxxCDCABDCABBxACACCCADDCADxADAAAxBBBCDDABBBxCCCADABxBCCAADACCDBDBxCBCBxBBxDCCBDBBDACBBCxBBCxCCCxBCDDxxxABADxCBABCDABDBAADxBxABxCDBAxDAABABDAAAxAxADDCAAABDDxABADCBBDCxBACDBBADBBxCxBDDCADBDDAAxDBDAxABDBAAAxxACBCCBDBBCxAxxDxxCACxADACCxDADBBxCxACCCDxCABBCDBCCADCDACAABAAADCCxDADCCxADDCxxBxDADADAAABxADCAxDAAxDDAADxxACxDBBCBDDxAxDBxCACADxDCAxDACDxCxADDxCBCABDBCBxCBCAxxACDBAxCCBAxCxBDABxABxCBCxCCABAxCxBCDBxBCxBBxDCBxDADDBCDCxDxBxDBDxCxADDACxBCCDxDDxDxCxxCBCDABDxxxDCBDAxDAxDxABxBCAABxBxxCACDBCDBBCCxxxDxAABBDBABxxBBDDBDCDCBBBDADBDBCCCDCADxDDCACCCBBDDBxBDBACxDBDxCCBCDABCAADACDAAAAxxDCxADxAACACCDCDBDBDxDDBCDDAAxDCxDCDDDxDBDCCDBxDBADAxxDBDAxBCxCBAxBxCBxCDCBCBBBAAxAxDCADCxxxxBBxDxDACCDBxDxCAxCBCxADxxDxxxCAxxBDxDBDxAxABBADxADCDAAAxBCxxBxDxABxBBDBDABBACCAABDAADCBCBxDBxxDDDDBxCADDACDAADxCxCxBABBDCBAACBBCCCACCCDBAAxBAAAxACDAxxADAADBDxABDDBDDAxxDxDDBxACCCCBDAACCCAABABDBCAxCBxDxCBCCBCDxBDDBBxxBDBCACDAxAxxBACACCxxACxADBCxxxCCxDDCCCCBAACDADABDCxAAxCCAADDxAADDxDDADxCCABCABAxBxxCCAAxCDAADCxDBDDCxABxBCDCxBBDDBDCBBxBAABCCxCxxDAxBABDDDxDBxCBACxACDAACxCBBADBBxBxxBCxCBABAABxCAACCCxDBCxDBAAxBBCDDxDBBBxBBACDCxAxCxAxxxxCBCDABACCACADDCACBADCCCDBBDxxxADBBCBACBBxACDDABBxDDBCDCABxxDABxCCAAxxCBDADDCADxxBADDCxAABDCBADDCBACBxCCCCCDDAACCCCDADxDDBDCxACDxAADDxBDAxAxAxBDDCDCDDDBDDxBxABCCxxDCDBBCBAxCBBDDxBABDCCBCxCCxxDACCCBAxxABDDxCxxBxABADDDADCBCxxDADDAxDxACBDBBCCADAADAxBBxBBxxCACDCxCDxBCxxBCBBCDCBCCACAxDBCxBADCAAxADxBBAxDDBDCCBABxDBCABBBADDxAAxCADDCBDxCBCDBCBADBADAACCAxBxBAACCADDCCAACBxxDDBAACCBBxDxDCDAxCADxCCDDABDCCxxABADBDADAxADBBAxxDADABCDCAACBBBxCDxCBBxxBBxDDxCxxDCCBCCACADCABABDxDxADABCDBCCBxCBCBDDDAAxCDxCCAxCDxBxBDxxACAxCxxxxCDBxxCxBDxCCxxCBBxBDACxDDCDDABBCDCAABCDADBACCAADDDxAxBBxxBDxDxCxDBBBCxAAACBAxAACCxBCCBCCCBDBCCCxCCCBBABBxxDxAxDxxDxCxAxDxDxDCxxxACDCDBCADDCACDxADBACDCxDDDABDDBxBxDDAxBxDCAxxABxBDBBBAxDAAACDBBBACABDxDACCDDxxxCAxDBxCDBxxDxAACACCCxxxBDBCCxABCBBDxDCBCxCDCBxDBACBABBBxAAxACCACxAADCxCxCBAACDCBBDDACxxDxAACACADBDCAxBADAxCCBBBBxCCAABCDBCBCBCACBBxABACCBABCACxDDDCCBxDDACxAADCBCCCDCxxDBBxAxDBxADCABACAxAADCCDDAxDAACCDDADxCCBCxCDABCCABDCCBCxxCDDxDxBCDBCDAxCxAADCBCBxxCDCBCBDDBCxDBxBxAAxADDCBCxCxDCBABDDxADBxCxDCCCCDCDxABDACxxDADDxBADADxACCxBxDCABxDBDAxxBDxBBAxDBxBCDDBxDxADCBCDCBABAACBDCABCBBADAxAxCABBBAxCBACCCDAAxADCBxxDCxCABCBBDACBxCDCDCxACCDCBCxxxBxCAxABAAAxCxBBCBBAxDBAAxxxAxxDBxxxBDCDBDCDDCDAxBDxCABDxADCCDDABxAABBBDCADBADDCxBDBCBDDAxCCAxDDBxDDxDDDCCCCBCBAxACxAABDADAAAxxCBCCxABBADCDABAxxCADxDBxBDDBxDDCCBBADxABAADDBACDxDACDAACDAxBCDxDAACAACABBxAxBAxxBDBCxDAxBAABBCDxBxAAxCAABDACAxAxDDACDxDDCBAxxDAxDAABCAADBDACBxxBCxBAAADDxBBAABDAAAxBCCABAxADDCAABDABDDDBBCBxBAADADCBxDDDDCBxADxCABCBAxBBxxCCACCxABCxADxAACCAxADAxxAxDBCAxDABCDBCBBCDCBxxABBCDACAACDCBDCDxCADCABABxDCACBCADBCCxxxBxDCDBACxBDBxxABACxxCAAAACBxAACxxDxxAACxDDBACDABxxxDDDBBBAxCADBABCxDxDBDCBxABBDCCxBBDxxBBxCBCAxACxB diff --git a/src/bin/quest-1-part-3.rs b/src/bin/quest-1-part-3.rs new file mode 100644 index 0000000..cd4369f --- /dev/null +++ b/src/bin/quest-1-part-3.rs @@ -0,0 +1,88 @@ +use std::{collections::HashMap, path::PathBuf}; + +type Result = std::result::Result>; + +fn main() -> Result<()> { + let file_path = PathBuf::from("./data/quest-1-part-3.txt"); + let data = std::fs::read_to_string(file_path)?; + let counter = quest_part_3(&data); + println!("Quest 1 / Part 3: {counter}"); + Ok(()) +} + +#[derive(PartialEq, Eq)] +enum Position { + Left, + Middle, + Right, +} +#[derive(Debug, Default)] +struct Fight { + left: i32, + middle: i32, + right: i32, + combatants: i32, +} + +fn quest_part_3(data: &str) -> i32 { + let table = HashMap::from([('A', 0), ('B', 1), ('C', 3), ('D', 5)]); + let mut counter = 0; + + let mut position = Position::Left; + let mut fight = Fight::default(); + for beast in data.chars() { + match (&position, table.get(&beast)) { + (Position::Left, None) => { + fight.left = 0; + fight.combatants = 0; + } + (Position::Left, Some(score)) => { + fight.left = *score; + fight.combatants = 1; + } + (Position::Middle, None) => { + fight.middle = 0; + } + (Position::Middle, Some(score)) => { + fight.middle = *score; + fight.combatants += 1; + } + (Position::Right, None) => { + fight.right = 0; + } + (Position::Right, Some(score)) => { + fight.right = *score; + fight.combatants += 1; + } + } + if position == Position::Right { + let score = fight.left + fight.middle + fight.right; + let bonus = match fight.combatants { + 3 => 6, // two per combatant + 2 => 2, // one per combatant + 1 => 0, // no bonus + _ => 0, // fail-safe + }; + counter += score + bonus; + println!("{fight:?} - {score} - {bonus} -> {} --> {counter}", score + bonus); + } + position = match &position { + Position::Left => Position::Middle, + Position::Middle => Position::Right, + Position::Right => Position::Left, + } + } + counter +} + +#[cfg(test)] +mod tests { + use super::quest_part_3; + + #[test] + fn sample() { + let data = "xBxAAABCDxCC"; + let result = quest_part_3(data); + assert_eq!(result, 30); + } +}