2015/08-10, 2015/12-13, started on 2015/14

This commit is contained in:
2017-12-05 08:01:57 +01:00
parent b6a89a7ab6
commit 6fc08571bf
20 changed files with 1072 additions and 156 deletions

100
2015/10/src/main.rs Normal file
View File

@@ -0,0 +1,100 @@
use std::io::{self, Read};
use std::iter::Iterator;
#[derive(Debug, PartialEq, Clone, Copy)]
enum Digit {
One,
Two,
Three,
}
struct LookAndSay {
values: Vec<Digit>,
}
impl LookAndSay {
fn from_string(input: &str) -> Self {
let values = input
.chars()
.map(|c| match c {
'1' => Digit::One,
'2' => Digit::Two,
'3' => Digit::Three,
_ => panic!("invalid input"),
})
.collect::<Vec<_>>();
LookAndSay { values: values }
}
}
impl Iterator for LookAndSay {
type Item = Vec<Digit>;
fn next(&mut self) -> Option<Self::Item> {
let mut values = Vec::new();
let mut state = self.values[0];
let mut count = 0;
for value in &self.values {
if *value == state {
count += 1;
} else {
values.push(match count {
1 => Digit::One,
2 => Digit::Two,
3 => Digit::Three,
_ => unreachable!(),
});
values.push(state);
state = *value;
count = 1;
}
}
values.push(match count {
1 => Digit::One,
2 => Digit::Two,
3 => Digit::Three,
_ => unreachable!(),
});
values.push(state);
self.values = values.clone();
Some(values)
}
}
fn main() {
let mut input = String::new();
io::stdin()
.read_to_string(&mut input)
.expect("faild to read input");
for line in input
.lines()
.map(|line| line.trim())
.filter(|line| !line.is_empty())
{
let mut look_and_say = LookAndSay::from_string(line);
// Skip 39 iterations to get number 40.
if let Some(value) = look_and_say.nth(39) {
println!("part_one={}", value.len());
}
// Skip 9 more iterations to get number 50.
if let Some(value) = look_and_say.nth(9) {
println!("part_two={}", value.len());
}
}
}