summaryrefslogtreecommitdiffstats
path: root/2015/rs/code/five/src/main.rs
blob: 3c4872cfe449c64a7a7a1ec62badf7f572415aba (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
static INPUT: &str = include_str!("input.txt");

fn main() {
  let valueified = INPUT.split('\n').filter(|s| !s.is_empty());

  let nice_count = valueified.clone().filter(|s| {
    let mut s2 = s.chars();
    let mut last_char = s2.next().unwrap();

    let mut double_flag = false;
    let mut abcdpqxy_flag = false;
    for c in s2 {
      if c == last_char { double_flag = true; }
      if [('a','b'),('c','d'),('p','q'),('x','y')].contains(&(last_char, c)) { abcdpqxy_flag = true; }
      if double_flag && abcdpqxy_flag { break; }

      last_char = c;
    }

    s.chars().filter(|c| "aeiou".contains(*c)).count() >= 3 && double_flag && !abcdpqxy_flag
  }).count();
  println!("Nice word count: {nice_count}");

  let nice_count = valueified.filter(|s| {
    let mut s = s.chars();
    let mut llc = s.next().unwrap();
    let mut lc = s.next().unwrap();

    let mut dupeslist = Vec::with_capacity(32);
    dupeslist.push((llc, lc));
    let mut pushed = true;

    let mut flag1 = false;
    let mut flag2 = false;

    for c in s {
      if !flag1 && (!pushed || llc != lc || lc != c) && dupeslist.contains(&(lc, c)) { flag1 = true; }
      if !flag1 && !dupeslist.contains(&(lc, c)) { dupeslist.push((lc, c)); pushed = true; } else { pushed = false; }

      if c == llc { flag2 = true; }

      if flag1 && flag2 { break; }

      llc = lc;
      lc = c;
    }

    flag1 && flag2
  }).count();

  println!("Nice word(v2) count: {nice_count}");
}