solution: day10

This commit is contained in:
Matej Janezic 2022-12-04 19:42:40 +01:00
parent 00000170f3
commit 00000180b3
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 56 additions and 1 deletions

54
src/bin/10.rs Normal file
View File

@ -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<u32> {
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<u32> {
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));
}
}

View File

@ -21,7 +21,7 @@ macro_rules! solve {
fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) { fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) {
let timer = Instant::now(); let timer = Instant::now();
let result = func(input); let result = func(input.trim());
let elapsed = timer.elapsed(); let elapsed = timer.elapsed();
match result { match result {
Some(result) => { Some(result) => {

1
src/test_inputs/10.txt Normal file
View File

@ -0,0 +1 @@
1