summaryrefslogtreecommitdiffstats
path: root/2015/rs/code/ten/src/main.rs
blob: 848c2881e631ad588764770cb45de33131958b71 (plain) (blame)
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
use std::fmt::Write;

static INPUT: &str = "1113122113";

fn main() {
  let mut buf = INPUT.to_string();
  for _ in 0..40 {
    buf = part1(&buf);
  }
  println!("Final strlen @ 40: {}", buf.len());

  for _ in 0..10 {
    buf = part1(&buf);
  }
  println!("Final strlen @ 50: {}", buf.len());
}

fn part1(nums: &str) -> String {
  let mut buf = String::with_capacity(nums.len() * 2); // worst-case
  let mut chars = nums.chars();
  let mut last = chars.next().unwrap();
  let mut count = 1;
  for char in chars {
    if char == last {
      count += 1;
    } else {
      let _ = write!(&mut buf, "{count}{last}");
      last = char;
      count = 1;
    }
  }
  let _ = write!(&mut buf, "{count}{last}");
  buf
}