blob: b2289f0cb9f2b00b41c8b0ac48269f81dd8bbee8 (
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
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
|
use std::io::{self, Read};
use std::str::FromStr;
struct Sequence {
sequence: String,
hypernet: bool,
}
impl Sequence {
fn has_abba(&self) -> bool {
self.sequence
.as_bytes()
.windows(4)
.any(|window| {
window[0] == window[3]
&& window[1] == window[2]
&& window[0] != window[1]
})
}
}
struct Ip(Vec<Sequence>);
impl Ip {
fn supports_tls(&self) -> bool {
let any_abbas = self.0
.iter()
.any(Sequence::has_abba);
let any_hypernet_abbas = self.0
.iter()
.filter(|sequence| sequence.hypernet)
.any(Sequence::has_abba);
any_abbas && !any_hypernet_abbas
}
}
impl FromStr for Ip {
type Err = ();
fn from_str(s: &str) -> Result<Ip, ()> {
let mut sequences = Vec::new();
let mut hypernet = false;
for sequence in s.split(|ch| ch == '[' || ch == ']') {
sequences.push(
Sequence {
sequence: sequence.to_owned(),
hypernet: hypernet
}
);
hypernet = !hypernet;
}
Ok(Ip(sequences))
}
}
fn solve(input: &str) -> usize {
input.lines()
.map(str::parse)
.map(Result::unwrap)
.filter(Ip::supports_tls)
.count()
}
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
println!("Part 1: {}", solve(&input));
}
#[test]
fn part1() {
let input = "
abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn
";
assert_eq!(2, solve(input.trim()));
}
|