From 0000001084a428bd408ad647da19fa1b1b93049d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Mon, 28 Nov 2022 22:59:04 +0100 Subject: [PATCH] solution: day1 --- src/bin/01.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/bin/01.rs diff --git a/src/bin/01.rs b/src/bin/01.rs new file mode 100644 index 000000000..ac4ce93 --- /dev/null +++ b/src/bin/01.rs @@ -0,0 +1,40 @@ +pub fn part_one(input: &str) -> Option { + let count = input.matches("(").count() - input.matches(")").count(); + Some(count.try_into().unwrap()) +} +pub fn part_two(input: &str) -> Option { + let mut floor = 0; + for (idx, char) in input.chars().enumerate() { + match char { + '(' => floor += 1, + ')' => floor -= 1, + _ => panic!("oops"), + }; + + if floor < 0 { + return Some(idx as u32 + 1); + } + } + None +} +fn main() { + let input = &aoc::read_file("inputs", 1); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + // let input = aoc::read_file("test_inputs", 1); + let input = "(()(()("; + assert_eq!(part_one(&input), Some(3)); + } + #[test] + fn test_part_two() { + // let input = aoc::read_file("test_inputs", 1); + let input = "()())"; + assert_eq!(part_two(&input), Some(5)); + } +}