1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#[test]
fn test_day_10() {
let mut foo = LookAndSay { seq: vec![2, 1, 1] };
assert_eq!(vec![1, 2, 2, 1], foo.next().unwrap());
foo = LookAndSay { seq: vec![1] };
assert_eq!(vec![1, 1], foo.next().unwrap());
assert_eq!(vec![2, 1], foo.next().unwrap());
assert_eq!(vec![1, 2, 1, 1], foo.next().unwrap());
assert_eq!(vec![1, 1, 1, 2, 2, 1], foo.next().unwrap());
assert_eq!(vec![3, 1, 2, 2, 1, 1], foo.next().unwrap());
}
pub fn solve(input: &str) -> usize {
let seq = input
.chars()
.map(|c| c.to_digit(10).unwrap() as usize)
.collect::<Vec<usize>>();
let mut look_and_say = LookAndSay { seq: seq };
for _ in 0..50 {
look_and_say.next();
}
look_and_say.seq.len()
}
struct LookAndSay {
seq: Vec<usize>,
}
impl Iterator for LookAndSay {
type Item = Vec<usize>;
fn next(&mut self) -> Option<Vec<usize>> {
let mut s = Vec::new();
let mut digit = self.seq[0];
let mut count = 0;
for &i in &self.seq {
if i != digit {
s.push(count);
s.push(digit);
digit = i;
count = 1;
} else {
count += 1;
}
}
s.push(count);
s.push(digit);
self.seq = s.clone();
Some(s)
}
}