-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
148 lines (132 loc) · 3.34 KB
/
main.rs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
data.lines().filter(|&l| is_nice_1(l.as_bytes())).count()
}
fn part_two(data: &str) -> usize {
data.lines().filter(|&l| is_nice_2(l.as_bytes())).count()
}
fn is_nice_1(input: &[u8]) -> bool {
three_vowels(input) && double_letter(input) && no_bad_sustrings(input)
}
fn is_nice_2(input: &[u8]) -> bool {
non_overlapping_pair(input) && seperated_repeated_letter(input)
}
fn three_vowels(input: &[u8]) -> bool {
let mut count: u8 = 0;
for charachter in input {
match charachter {
b'a' | b'e' | b'i' | b'o' | b'u' => {
if count == 2 {
return true;
}
count += 1;
}
_ => (),
}
}
false
}
fn double_letter(input: &[u8]) -> bool {
let mut last: u8 = 0;
for charachter in input {
if charachter == &last {
return true;
}
last = *charachter;
}
false
}
const fn no_bad_sustrings(mut input: &[u8]) -> bool {
while let Some((charachter, rest)) = input.split_first() {
if rest.is_empty() {
return true;
}
match charachter {
b'a' => {
if rest[0] == b'b' {
return false;
}
}
b'c' => {
if rest[0] == b'd' {
return false;
}
}
b'p' => {
if rest[0] == b'q' {
return false;
}
}
b'x' => {
if rest[0] == b'y' {
return false;
}
}
_ => (),
}
input = rest;
}
true
}
fn non_overlapping_pair(mut input: &[u8]) -> bool {
/*input
.windows(2)
.dedup()
.sorted_unstable()
.dedup_with_count()
.any(|(count, _)| count > 1)*/
if input.len() < 4 {
return false;
}
while let Some((head, rest)) = input.split_first() {
if rest.is_empty() {
return false;
}
if matching_pair(*head, rest[0], &rest[1..]) {
return true;
}
input = rest;
}
false
}
const fn matching_pair(char_1: u8, char_2: u8, mut search: &[u8]) -> bool {
while let Some((charachter, rest)) = search.split_first() {
if rest.is_empty() {
return false;
}
if *charachter == char_1 && rest[0] == char_2 {
return true;
}
search = rest;
}
false
}
fn seperated_repeated_letter(input: &[u8]) -> bool {
input.windows(3).any(|slice| slice[0] == slice[2])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(2, part_one(data));
}
#[test]
fn overlap_test() {
assert!(non_overlapping_pair(b"xyxy"));
assert!(non_overlapping_pair(b"aabcdefgaa"));
assert!(!non_overlapping_pair(b"aaa"));
assert!(non_overlapping_pair(b"aaaa"));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(0, part_two(data));
}
}