-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
177 lines (148 loc) · 4.08 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use crate::split_password::SplitPassword;
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: &'static str) -> usize {
data.lines().filter(|l| valid_password(l, false)).count()
}
fn part_two(data: &'static str) -> usize {
data.lines().filter(|l| valid_password(l, true)).count()
}
mod word {
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Word {
data: [u8; 8],
}
impl From<&[u8]> for Word {
fn from(v: &[u8]) -> Self {
let mut data = [0; 8];
for (o, i) in data.iter_mut().zip(v.iter()) {
*o = *i;
}
Self { data }
}
}
impl Word {
pub const fn new() -> Self {
Self { data: [0; 8] }
}
pub fn sort(&mut self) {
self.data.sort_unstable_by_key(|&v| std::cmp::Reverse(v));
}
}
}
mod split_password {
use crate::word::Word;
pub struct SplitPassword {
data: [Word; 15],
length: usize,
}
impl SplitPassword {
pub const fn new() -> Self {
Self {
data: [Word::new(); 15],
length: 0,
}
}
pub fn push(&mut self, new: &'static [u8], sorted: bool) {
let mut new: Word = new.into();
if sorted {
new.sort();
}
self.data[self.length] = new;
self.length += 1;
}
pub fn pop(&mut self) -> Option<Word> {
if self.length == 0 {
return None;
}
self.length -= 1;
Some(self.data[self.length])
}
}
impl Default for SplitPassword {
fn default() -> Self {
Self::new()
}
}
pub struct Iter<'a> {
source: &'a SplitPassword,
position: usize,
}
impl<'a> SplitPassword {
const fn iter(&'a self) -> Iter<'a> {
Iter {
source: self,
position: 0,
}
}
}
impl<'a> IntoIterator for &'a SplitPassword {
type IntoIter = Iter<'a>;
type Item = Word;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> Iterator for Iter<'a> {
type Item = Word;
fn next(&mut self) -> Option<Self::Item> {
if self.position >= self.source.length {
return None;
}
self.position += 1;
Some(self.source.data[self.position - 1])
}
}
}
fn split(input: &'static [u8], sorted: bool) -> SplitPassword {
let mut out = SplitPassword::new();
let mut remainder = input;
let split_next = |d: &'static [u8]| {
d.iter()
.position(|&x| x == b' ')
.map(|pos| (&d[..pos], &d[pos + 1..]))
};
while let Some((a, b)) = split_next(remainder) {
out.push(a, sorted);
remainder = b;
}
out.push(remainder, sorted);
out
}
fn repeated_parts(mut data: SplitPassword) -> bool {
while let Some(next) = data.pop() {
for other in &data {
if next == other {
return true;
}
}
}
false
}
fn valid_password(data: &'static str, sorted: bool) -> bool {
let data_slice = data.trim().as_bytes();
let split_data = split(data_slice, sorted);
!repeated_parts(split_data)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
assert!(valid_password("aa bb cc dd ee", false));
assert!(!valid_password("aa bb cc dd aa", false));
assert!(valid_password("aa bb cc dd aaa", false));
}
#[test]
fn two() {
assert!(valid_password("abcde fghij", true));
assert!(!valid_password("abcde xyz ecdab", true));
assert!(valid_password("a ab abc abd abf abj", true));
assert!(valid_password("iiii oiii ooii oooi oooo", true));
assert!(!valid_password("oiii ioii iioi iiio", true));
}
}