y2015d10
This commit is contained in:
parent
5f7d4ea185
commit
b2403336a5
14
y2015/src/bin/d10.rs
Normal file
14
y2015/src/bin/d10.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use y2015::days::d10;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
part1();
|
||||||
|
part2();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1() {
|
||||||
|
println!("{}", d10::process_part1("1321131112"));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2() {
|
||||||
|
println!("{}", d10::process_part2("1321131112"));
|
||||||
|
}
|
57
y2015/src/days/d10.rs
Normal file
57
y2015/src/days/d10.rs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
pub fn process_part1(input: &str) -> u32 {
|
||||||
|
let mut number = input.to_string();
|
||||||
|
for _ in 0..40 {
|
||||||
|
number = look_and_say(&number);
|
||||||
|
}
|
||||||
|
number.len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_part2(input: &str) -> u32 {
|
||||||
|
let mut number = input.to_string();
|
||||||
|
for _ in 0..50 {
|
||||||
|
number = look_and_say(&number);
|
||||||
|
}
|
||||||
|
number.len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn look_and_say(number: &str) -> String {
|
||||||
|
let mut new_number = String::new();
|
||||||
|
let mut last_char = 'x';
|
||||||
|
let mut char_counter = 1;
|
||||||
|
for (idx, char) in number.chars().enumerate() {
|
||||||
|
if char == last_char {
|
||||||
|
char_counter += 1;
|
||||||
|
} else {
|
||||||
|
if idx != 0 {
|
||||||
|
new_number.push_str(format!("{char_counter}").as_str());
|
||||||
|
new_number.push(last_char);
|
||||||
|
}
|
||||||
|
last_char = char;
|
||||||
|
char_counter = 1;
|
||||||
|
}
|
||||||
|
if idx == number.len() - 1 {
|
||||||
|
new_number.push_str(format!("{char_counter}").as_str());
|
||||||
|
new_number.push(char);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_number
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests_10 {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let result = look_and_say("1");
|
||||||
|
assert_eq!(result, "11".to_string());
|
||||||
|
let result = look_and_say("11");
|
||||||
|
assert_eq!(result, "21".to_string());
|
||||||
|
let result = look_and_say("21");
|
||||||
|
assert_eq!(result, "1211".to_string());
|
||||||
|
let result = look_and_say("1211");
|
||||||
|
assert_eq!(result, "111221".to_string());
|
||||||
|
let result = look_and_say("111221");
|
||||||
|
assert_eq!(result, "312211".to_string());
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
pub mod d1;
|
pub mod d1;
|
||||||
|
pub mod d10;
|
||||||
pub mod d2;
|
pub mod d2;
|
||||||
pub mod d3;
|
pub mod d3;
|
||||||
pub mod d4;
|
pub mod d4;
|
||||||
|
Loading…
Reference in New Issue
Block a user