From 00000180b3708c958de5803aaf6fe1dbc81797bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sun, 4 Dec 2022 19:42:40 +0100 Subject: [PATCH] solution: day10 --- src/bin/10.rs | 54 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- src/test_inputs/10.txt | 1 + 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/bin/10.rs create mode 100644 src/test_inputs/10.txt diff --git a/src/bin/10.rs b/src/bin/10.rs new file mode 100644 index 000000000..152dc85 --- /dev/null +++ b/src/bin/10.rs @@ -0,0 +1,54 @@ +fn process(s: &str) -> String { + let mut new_s = String::new(); + let mut count = 0; + let mut current = s.chars().next().unwrap(); + + for c in s.chars() { + if c == current { + count += 1; + } else { + new_s.push_str(&count.to_string()); + new_s.push(current); + current = c; + count = 1 + } + } + + new_s.push_str(&count.to_string()); + new_s.push(current); + new_s +} + +pub fn part_one(input: &str) -> Option { + let mut res = input.to_string(); + for _ in 0..40 { + res = process(&res); + } + Some(res.len() as u32) +} +pub fn part_two(input: &str) -> Option { + let mut res = input.to_string(); + for _ in 0..50 { + res = process(&res); + } + Some(res.len() as u32) +} +fn main() { + let input = &aoc::read_file("inputs", 10); + 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", 10); + assert_eq!(part_one(&input), Some(124496)); + } + #[test] + fn test_part_two() { + let input = aoc::read_file("test_inputs", 10); + assert_eq!(part_two(&input), Some(1766402)); + } +} diff --git a/src/lib.rs b/src/lib.rs index a789162..70e577e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,7 +21,7 @@ macro_rules! solve { fn print_result(func: impl FnOnce(&str) -> Option, input: &str) { let timer = Instant::now(); - let result = func(input); + let result = func(input.trim()); let elapsed = timer.elapsed(); match result { Some(result) => { diff --git a/src/test_inputs/10.txt b/src/test_inputs/10.txt new file mode 100644 index 000000000..d00491f --- /dev/null +++ b/src/test_inputs/10.txt @@ -0,0 +1 @@ +1