my solutions to advent of code
aoc advent-of-code

did day 1 in rust why not

aylac.top bdb95867 c10cc95b

verified
Changed files
+51
2015
+7
2015/1/rust/Cargo.lock
···
+
# This file is automatically @generated by Cargo.
+
# It is not intended for manual editing.
+
version = 4
+
+
[[package]]
+
name = "rust"
+
version = "0.1.0"
+6
2015/1/rust/Cargo.toml
···
+
[package]
+
name = "rust"
+
version = "0.1.0"
+
edition = "2024"
+
+
[dependencies]
+38
2015/1/rust/src/main.rs
···
+
use std::{fs, ops::ControlFlow};
+
+
fn main() {
+
let input: String = match fs::read_to_string("../input.txt") {
+
Ok(input) => input,
+
_ => panic!("invalid input!!!"),
+
};
+
let input = input.trim();
+
+
let part1 = input.chars().fold(0, |floor, v| {
+
(match v {
+
'(' => 1,
+
')' => -1,
+
_ => 0,
+
}) + floor
+
});
+
+
println!("{}", part1.to_string());
+
+
let part2 = input.chars().try_fold((0, 1), |state, v| {
+
let (floor, step) = state;
+
let floor = (match v {
+
'(' => 1,
+
')' => -1,
+
_ => 0,
+
}) + floor;
+
match floor {
+
-1 => ControlFlow::Break(step),
+
_ => ControlFlow::Continue((floor, step + 1)),
+
}
+
});
+
let part2 = match part2 {
+
ControlFlow::Break(part2) => part2,
+
_ => panic!("something bad happened"),
+
};
+
+
println!("{}", part2.to_string());
+
}