2015 day 1 (redoing lost challenges)
This commit is contained in:
parent
56ec1d2ec7
commit
351fc11bed
1
2015/.gitignore
vendored
Normal file
1
2015/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
target/
|
5
2015/Cargo.lock
generated
Normal file
5
2015/Cargo.lock
generated
Normal file
|
@ -0,0 +1,5 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "aoc2015"
|
||||
version = "0.1.0"
|
10
2015/Cargo.toml
Normal file
10
2015/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "aoc2015"
|
||||
version = "0.1.0"
|
||||
authors = ["dece <shgck@pistache.land>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "aoc"
|
||||
|
||||
[dependencies]
|
31
2015/src/bin/day1.rs
Normal file
31
2015/src/bin/day1.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use aoc::input;
|
||||
|
||||
fn main() {
|
||||
let lines = input::read_lines();
|
||||
let line = lines[0].to_owned();
|
||||
|
||||
// Part 1
|
||||
let mut floor = 0;
|
||||
for c in line.chars() {
|
||||
match c {
|
||||
'(' => floor += 1,
|
||||
')' => floor -= 1,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
println!("Floor: {}", floor);
|
||||
|
||||
// Part 2
|
||||
floor = 0;
|
||||
for (i, c) in line.chars().enumerate() {
|
||||
match c {
|
||||
'(' => floor += 1,
|
||||
')' => floor -= 1,
|
||||
_ => {}
|
||||
}
|
||||
if floor == -1 {
|
||||
println!("Entered -1 at {}.", i + 1);
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
17
2015/src/input.rs
Normal file
17
2015/src/input.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use std::io;
|
||||
|
||||
pub fn read_lines() -> Vec<String> {
|
||||
let mut lines = vec!();
|
||||
loop {
|
||||
let mut text = String::new();
|
||||
let read_count = io::stdin().read_line(&mut text).unwrap();
|
||||
if read_count == 0 {
|
||||
break
|
||||
}
|
||||
if let Some(stripped) = text.strip_suffix("\n") {
|
||||
text = stripped.to_string()
|
||||
}
|
||||
lines.push(text);
|
||||
}
|
||||
lines
|
||||
}
|
1
2015/src/lib.rs
Normal file
1
2015/src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod input;
|
Loading…
Reference in a new issue