-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.rs
40 lines (32 loc) · 922 Bytes
/
part1.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
// adventofcode - day 1
// part 1
use std::io::prelude::*;
use std::fs::File;
fn main() {
let directions = import_data();
let mut floor = 0i32;
// we iterate over every character and increment/decrement whenever we hit
// a ( or ), respectively.
// any other character will just be ignored
for token in directions.chars(){
floor += match token {
'(' => 1,
')' => -1,
_ => 0
};
}
println!("Deliver it to floor number {}", floor);
}
// This function simply imports the data set from a file called input.txt
fn import_data() -> String {
let mut file = match File::open("../../inputs/01.txt") {
Ok(f) => f,
Err(e) => panic!("file error: {}", e),
};
let mut data = String::new();
match file.read_to_string(&mut data){
Ok(_) => {},
Err(e) => panic!("file error: {}", e),
};
data
}