2015/08-10, 2015/12-13, started on 2015/14
This commit is contained in:
4
2015/10/Cargo.lock
generated
Normal file
4
2015/10/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
|
||||
6
2015/10/Cargo.toml
Normal file
6
2015/10/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day-10"
|
||||
version = "0.1.0"
|
||||
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
1
2015/10/input.txt
Normal file
1
2015/10/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
1113122113
|
||||
100
2015/10/src/main.rs
Normal file
100
2015/10/src/main.rs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user