-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2015_10.rs
67 lines (54 loc) · 1.46 KB
/
solution_2015_10.rs
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
60
61
62
63
64
65
66
67
fn parse(input: &str) -> Vec<u8> {
input.chars().map(|ch| ch as u8 - b'0').collect()
}
fn convert(data: &[u8]) -> Vec<u8> {
let mut result: Vec<u8> = Vec::new();
let mut idx: usize = 0;
while idx < data.len() {
let len = data[idx ..].iter().take_while(|&&x| x == data[idx]).count();
let count = u8::try_from(len).unwrap_or_else(|err| {
panic!("Failed to convert {len} elements from {data:?}: {err:?}")
});
assert!(count < 10);
assert!(count > 0);
result.push(count);
result.push(data[idx]);
idx += len;
}
result
}
fn solve(data: &[u8], n: usize) -> Vec<u8> {
let mut current: Vec<u8> = data.to_vec();
for _ in 0 .. n {
let next = convert(¤t);
current = next;
}
current
}
fn solution(input: &str, n: usize) -> usize {
let data = parse(input);
solve(&data, n).iter().map(|n| n + b'0').len()
}
const DATA: &str = "1113222113";
fn main() {
let result_1 = solution(DATA, 40);
println!("Part 1: {result_1}");
let result_2 = solution(DATA, 50);
println!("Part 2: {result_2}");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_convert_1() {
assert_eq!(convert(&parse("111221")), parse("312211"));
}
#[test]
fn test_solve_1_real() {
assert_eq!(solution(DATA, 40), 252_594);
}
#[test]
fn test_solve_2_real() {
assert_eq!(solution(DATA, 50), 3_579_328);
}
}