48 lines
1.1 KiB
Rust
48 lines
1.1 KiB
Rust
use aoc_runner_derive::{aoc, aoc_generator};
|
|
use std::collections::VecDeque;
|
|
#[aoc_generator(day10)]
|
|
fn parse(input: &str) -> Vec<char> {
|
|
input.chars().collect()
|
|
}
|
|
|
|
#[aoc(day10, part1)]
|
|
fn part1(input: &Vec<char>) -> usize {
|
|
solve(input, 40).len()
|
|
}
|
|
|
|
fn solve(input: &Vec<char>, runs: usize) -> String {
|
|
let mut saying: VecDeque<char> = input.iter().map(|c| *c).collect();
|
|
let mut next: VecDeque<char> = VecDeque::new();
|
|
for _ in 0..runs {
|
|
while let Some(current) = saying.pop_front() {
|
|
let mut count = 1;
|
|
while saying.front() == Some(¤t) {
|
|
count += 1;
|
|
saying.pop_front();
|
|
}
|
|
let s = format!("{count}{current}");
|
|
for c in s.chars() {
|
|
next.push_back(c);
|
|
}
|
|
}
|
|
saying = next;
|
|
next = VecDeque::new();
|
|
}
|
|
saying.into_iter().collect()
|
|
}
|
|
|
|
#[aoc(day10, part2)]
|
|
fn part2(input: &Vec<char>) -> usize {
|
|
solve(input, 50).len()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn part1_example() {
|
|
assert_eq!(solve(&parse("111221"), 1), "312211");
|
|
}
|
|
}
|