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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use errors::*;

pub fn solve(input: &str) -> Result<String> {
  Ok(input.lines()
    .map(|x| IP7::new(x))
    .filter(IP7::supports_ssl)
    .count()
    .to_string())
}

pub struct IP7 {
  supernets: Vec<String>,
  hypernets: Vec<String>,
}

impl IP7 {
  fn new(s: &str) -> Self {
    let mut current = String::new();
    let mut supernets = Vec::new();
    let mut hypernets = Vec::new();

    for c in s.chars() {
      match c {
        '[' => {
          supernets.push(current);
          current = String::new();
        }
        ']' => {
          hypernets.push(current);
          current = String::new();
        }
        c => current.push(c),
      }
    }
    supernets.push(current);

    IP7 {
      supernets: supernets,
      hypernets: hypernets,
    }
  }

  pub fn supports_tls(&self) -> bool {
    self.supernets.iter().any(|x| Self::abba(x)) &&
    self.hypernets.iter().all(|x| !Self::abba(x))
  }

  fn supports_ssl(&self) -> bool {
    let abas: Vec<String> =
      self.supernets.iter().flat_map(|x| {
        Self::abas(x).iter().map(|aba| {
          let mut c = aba.chars();
          let a = c.next().unwrap();
          let b = c.next().unwrap();
          format!("{}{}{}", b, a, b)
        }).collect::<Vec<_>>()
      }).collect();
    self.hypernets.iter().any(|x| abas.iter().any(|y| x.contains(y)))
  }

  fn abba(s: &str) -> bool {
    let v: Vec<_> = s.chars().collect();
    v.windows(4).any(|w| (w[0] != w[1]) && (w[0] == w[3]) && (w[1] == w[2]))
  }

  fn abas(s: &str) -> Vec<String> {
    let v: Vec<_> = s.chars().collect();
    v.windows(3)
      .filter(|w| (w[0] != w[1]) && (w[0] == w[2]))
      .map(|w| w.iter().cloned().collect())
      .collect()
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_new() {
    let ip = IP7::new("abba[mnop]qrst");
    assert_eq!(ip.supernets, vec!["abba".to_string(), "qrst".to_string()]);
    assert_eq!(ip.hypernets, vec!["mnop".to_string()]);
  }

  #[test]
  fn test_abba() {
    assert!(IP7::abba("abba"));
    assert!(!IP7::abba("abcd"));
  }

  #[test]
  fn test_supports_tls() {
    assert_eq!(IP7::new("abba[mnop]qrst").supports_tls(), true);
    assert_eq!(IP7::new("abcd[bddb]xyyx").supports_tls(), false);
    assert_eq!(IP7::new("aaaa[qwer]tyui").supports_tls(), false);
    assert_eq!(IP7::new("ioxxoj[asdfgh]zxcvbn").supports_tls(), true);
  }

  #[test]
  fn test_abas() {
    assert_eq!(IP7::abas("zazbz"),
               vec!["zaz".to_string(), "zbz".to_string()]);
  }

  #[test]
  fn test_supports_ssl() {
    assert!(IP7::new("aba[bab]xyz").supports_ssl());
    assert!(!IP7::new("xyx[xyx]xyx").supports_ssl());
    assert!(IP7::new("aaa[kek]eke").supports_ssl());
    assert!(IP7::new("zazbz[bzb]cdb").supports_ssl());
  }
}