-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.rs
127 lines (107 loc) · 2.55 KB
/
lib.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
extern crate core;
use std::collections::{HashMap, HashSet};
pub fn fn1(s: &str) -> String {
let mut pass = s.to_string();
loop {
pass = increment(&pass);
if is_valid(&pass) {
return pass;
}
}
}
fn is_valid(s: &str) -> bool {
req1(s) && req2(s) && req3(s)
}
fn req1(s: &str) -> bool {
for i in 0..s.len() - 2 {
let a = s.chars().nth(i).unwrap();
let b = s.chars().nth(i + 1).unwrap();
if next_char(a) == b {
let c = s.chars().nth(i + 2).unwrap();
if next_char(b) == c {
return true;
}
}
}
false
}
fn req2(s: &str) -> bool {
for c in s.chars().into_iter() {
match c {
'i' | 'o' | 'l' => return false,
_ => (),
}
}
true
}
fn req3(s: &str) -> bool {
let mut set = HashSet::new();
let mut i = 0;
let mut found_first = false;
while i < s.len() - 1 {
let a = s.chars().nth(i).unwrap();
let b = s.chars().nth(i + 1).unwrap();
if a == b {
let key = format!("{}{}", a, b);
if !set.contains(&key) {
set.insert(key);
if found_first {
return true;
}
found_first = true;
}
}
if i == s.len() - 2 {
return false;
}
if s.chars().nth(i + 2).unwrap() == a {
i += 2;
} else {
i += 1;
}
}
false
}
fn next_char(c: char) -> char {
std::char::from_u32(c as u32 + 1).unwrap_or(c)
}
fn increment(s: &str) -> String {
let mut string = s.to_string();
for i in (0..s.len()).rev() {
unsafe {
let mut bytes = string.as_bytes_mut();
let c = s.chars().nth(i).unwrap();
if c != 'z' {
bytes[i] = next_char(c) as u8;
return string;
} else {
bytes[i] = 'a' as u8;
}
}
}
string
}
pub fn fn2(input: &str) -> i32 {
let lines: Vec<_> = input.lines().collect();
1
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_increment() {
assert_eq!(increment("a"), "b");
assert_eq!(increment("xy"), "xz");
assert_eq!(increment("xz"), "ya");
assert_eq!(increment("azz"), "baa");
}
#[test]
fn test_fn1_input() {
assert_eq!(fn1("vzbxkghb"), "vzbxxyzz");
}
#[test]
fn test_fn2_input() {
assert_eq!(fn1("vzbxxyzz"), "vzcaabcc");
}
}