Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "day10-elves_look_elves_say"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,50 @@
pub fn run(input: &str, count: u8) -> String {
let mut look = String::from(input);
for _ in 0..count {
look = look_and_say(&look);
}
look
}
fn look_and_say(number: &str) -> String {
let mut say = String::new();
let mut last_digit = ' ';
let mut count = 0;
for digit in number.chars() {
if digit == last_digit {
count += 1;
} else {
say += &(count.to_string() + &last_digit.to_string());
count = 1;
last_digit = digit;
}
}
say += &(count.to_string() + &last_digit.to_string());
say[2..].to_string()
}
#[cfg(test)]
mod tests {
use super::*;
/*use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {}", name)[..])
}*/
#[test]
fn test_sample() {
let sample_input = "1";
assert_eq!(run(sample_input, 5), "312211".to_string());
}
#[test]
fn test_challenge() {
let challenge_input = "1113122113";
let after_40 = run(challenge_input, 40);
assert_eq!(after_40.len(), 360154);
assert_eq!(run(&after_40, 10).len(), 5103798);
}
}