add until day05-1
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1 @@
|
||||
()())
|
||||
File diff suppressed because one or more lines are too long
@@ -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