-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
62 lines (54 loc) · 1.25 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
extern crate core;
pub fn fn1(s: &str) -> i32 {
s.chars().map(|c| match c {
'(' => 1,
')' => -1,
_ => 0,
}).fold(0, |sum, i| sum + i)
}
pub fn fn2(s: &str) -> i32 {
let mut n = 0;
for (i, c) in s.chars().enumerate() {
match c {
'(' => n += 1,
')' => n-=1,
_ => (),
}
if n == -1 {
return i as i32+1
}
}
0
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_fn1_unit() {
assert_eq!(fn1("(())"), 0);
assert_eq!(fn1("()()"), 0);
assert_eq!(fn1("((("), 3);
assert_eq!(fn1("(()(()("), 3);
assert_eq!(fn1("))((((("), 3);
assert_eq!(fn1("())"), -1);
assert_eq!(fn1("))("), -1);
assert_eq!(fn1(")))"), -3);
assert_eq!(fn1(")())())"), -3);
}
#[test]
fn test_fn1_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn1(s.as_str()), 280);
}
#[test]
fn test_fn2_unit() {
assert_eq!(fn2(")"), 1);
assert_eq!(fn2("()())"), 5);
}
#[test]
fn test_fn2_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn2(s.as_str()), 1797);
}
}