add until day05-1
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#[cfg(debug_assertions)]
|
||||
const FILENAME: &str = "example.txt";
|
||||
#[cfg(not(debug_assertions))]
|
||||
const FILENAME: &str = "input.txt";
|
||||
|
||||
fn main() {
|
||||
let data = std::io::read_to_string(std::fs::File::open(FILENAME).unwrap()).unwrap();
|
||||
let s = data.trim();
|
||||
|
||||
part_1(s);
|
||||
part_2(s);
|
||||
}
|
||||
|
||||
fn part_1(data: &str) {
|
||||
let result: i64 = data.chars().map(|c| if c == '(' { 1 } else { -1 }).sum();
|
||||
println!("{result}");
|
||||
}
|
||||
|
||||
fn part_2(data: &str) {
|
||||
let mut current_floor: i64 = 0;
|
||||
for (i, c) in data.chars().enumerate() {
|
||||
if c == '(' {
|
||||
current_floor += 1;
|
||||
} else {
|
||||
current_floor -= 1;
|
||||
}
|
||||
if current_floor == -1 {
|
||||
println!("{}", i + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user