From 1d6cf18ff2f1c41327584d0772494dc18b040c3b Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 1 Dec 2022 23:56:36 -0600 Subject: [PATCH] Part 1 --- 2022/rust/src/day2.rs | 62 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/2022/rust/src/day2.rs b/2022/rust/src/day2.rs index 630a235..093b9d3 100644 --- a/2022/rust/src/day2.rs +++ b/2022/rust/src/day2.rs @@ -1,13 +1,59 @@ mod common; +// A -> Rock +// B -> Paper +// C -> Scissors + +// X -> Rock -> 1 +// Y -> Paper -> 2 +// Z -> Scissors -> 3 + fn main() { let input = common::day_input(2); println!("Part 1: {}", part1(&input)); println!("Part 2: {}", part2(&input)); } +fn rps(a: char, b: char) -> i32 { + match b { + 'X' => { + 1 + match a { + 'A' => 3, + 'B' => 0, + 'C' => 6, + _ => 1000000, + } + } + 'Y' => { + 2 + match a { + 'A' => 6, + 'B' => 3, + 'C' => 0, + _ => 1000000, + } + } + 'Z' => { + 3 + match a { + 'A' => 0, + 'B' => 6, + 'C' => 3, + _ => 1000000, + } + } + _ => 1000000, + } +} + fn part1(input: &str) -> i32 { - 0 + input + .lines() + .map(|s| { + println!("{:?}", s); + println!("{:?}", s.chars().nth(2)); + let mut c = s.chars(); + rps(c.nth(0).unwrap(), c.nth(1).unwrap()) + }) + .sum() } fn part2(input: &str) -> i32 { @@ -18,15 +64,17 @@ fn part2(input: &str) -> i32 { mod tests { use super::*; - const TEST_INPUT: &str = "0"; + const TEST_INPUT: &str = "A Y +B X +C Z"; #[test] fn test_part1() { - assert_eq!(part1(TEST_INPUT), 10000) + assert_eq!(part1(TEST_INPUT), 15) } - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 10000) - } + // #[test] + // fn test_part2() { + // assert_eq!(part2(TEST_INPUT), 10000) + // } }