Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2015/day10-elves_look_elves_say/Cargo.toml
Normal file
8
2015/day10-elves_look_elves_say/Cargo.toml
Normal 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]
|
50
2015/day10-elves_look_elves_say/src/lib.rs
Normal file
50
2015/day10-elves_look_elves_say/src/lib.rs
Normal 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);
|
||||
}
|
||||
}
|
0
2015/day10-elves_look_elves_say/tests/challenge_input
Normal file
0
2015/day10-elves_look_elves_say/tests/challenge_input
Normal file
0
2015/day10-elves_look_elves_say/tests/sample_input
Normal file
0
2015/day10-elves_look_elves_say/tests/sample_input
Normal file
Loading…
Add table
Add a link
Reference in a new issue