-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd07.rs
More file actions
114 lines (91 loc) · 2.7 KB
/
d07.rs
File metadata and controls
114 lines (91 loc) · 2.7 KB
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
114
use std::collections::HashMap;
use crate::Day;
pub struct Day07 {}
impl Day for Day07 {
fn year(&self) -> u16 {
2016
}
fn day(&self) -> u8 {
7
}
fn part_one(&self) -> String {
let input = self.read_default_input();
let mut count = 0;
for ip in input.lines() {
if support_tls(ip) {
count += 1;
}
}
count.to_string()
}
fn part_two(&self) -> String {
let input = self.read_default_input();
let mut count = 0;
for ip in input.lines() {
if support_ssl(ip) {
count += 1;
}
}
count.to_string()
}
}
fn support_tls(ip: &str) -> bool {
let mut contains_inside_brackets = false;
let mut contains_outside_brackets = false;
let mut current_inside_brackets = false;
let chars: Vec<_> = ip.chars().collect();
for (i, c) in chars.iter().enumerate() {
if *c == '[' {
current_inside_brackets = true;
continue;
} else if *c == ']' {
current_inside_brackets = false;
continue;
}
if i + 3 >= ip.len() {
break;
}
if (chars[i + 3] == *c && chars[i + 1] == chars[i + 2]) && chars[i + 1] != *c {
match current_inside_brackets {
true => contains_inside_brackets = true,
false => contains_outside_brackets = true,
}
}
}
contains_outside_brackets && !contains_inside_brackets
}
fn support_ssl(ip: &str) -> bool {
let mut aba_registry: HashMap<String, bool> = HashMap::new();
let mut bab_registry: HashMap<String, bool> = HashMap::new();
let mut current_inside_brackets = false;
let chars: Vec<_> = ip.chars().collect();
for (i, c) in chars.iter().enumerate() {
if *c == '[' {
current_inside_brackets = true;
continue;
} else if *c == ']' {
current_inside_brackets = false;
continue;
}
if i + 2 >= ip.len() {
break;
}
if chars[i + 2] == *c && chars[i + 1] != *c {
let found = String::from_iter(&chars[i..=i + 2]);
if current_inside_brackets {
bab_registry.insert(found, true);
} else {
aba_registry.insert(found, true);
}
}
}
for (key, _) in aba_registry {
let key_chars: Vec<_> = key.chars().collect();
let bab = String::from_iter(vec![key_chars[1], key_chars[0], key_chars[1]]);
match bab_registry.get(bab.as_str()) {
Some(_) => return true,
None => continue,
}
}
false
}