2015/01
This commit is contained in:
4
2015/01/Cargo.lock
generated
Normal file
4
2015/01/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[root]
|
||||||
|
name = "01"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
6
2015/01/Cargo.toml
Normal file
6
2015/01/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "01"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
1
2015/01/input.txt
Normal file
1
2015/01/input.txt
Normal file
File diff suppressed because one or more lines are too long
80
2015/01/src/main.rs
Normal file
80
2015/01/src/main.rs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
use std::io::{self, Read};
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
io::stdin().read_to_string(&mut input).unwrap();
|
||||||
|
|
||||||
|
let count = process_1(&input);
|
||||||
|
|
||||||
|
println!("count={}", count);
|
||||||
|
|
||||||
|
if let Some(floor) = process_2(&input) {
|
||||||
|
println!("floor={}", floor + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_1(input: &str) -> i32 {
|
||||||
|
input
|
||||||
|
.chars()
|
||||||
|
.fold(0, |acc, x| {
|
||||||
|
match x {
|
||||||
|
'(' => acc + 1,
|
||||||
|
')' => acc - 1,
|
||||||
|
_ => acc,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_2(input: &str) -> Option<usize> {
|
||||||
|
input
|
||||||
|
.chars()
|
||||||
|
.scan(0, |acc, x| {
|
||||||
|
*acc += match x {
|
||||||
|
'(' => 1,
|
||||||
|
')' => -1,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(*acc)
|
||||||
|
})
|
||||||
|
.position(|acc| acc == -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{process_1, process_2};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_01() {
|
||||||
|
assert_eq!(0, process_1("(())"));
|
||||||
|
assert_eq!(0, process_1("()()"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_02() {
|
||||||
|
assert_eq!(3, process_1("((("));
|
||||||
|
assert_eq!(3, process_1("(()(()("));
|
||||||
|
assert_eq!(3, process_1("))((((("));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_03() {
|
||||||
|
assert_eq!(-1, process_1("())"));
|
||||||
|
assert_eq!(-1, process_1("))("));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_04() {
|
||||||
|
assert_eq!(-3, process_1(")))"));
|
||||||
|
assert_eq!(-3, process_1(")())())"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example_05() {
|
||||||
|
assert_eq!(Some(1 - 1), process_2(")"));
|
||||||
|
assert_eq!(Some(5 - 1), process_2("()())"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user