2015 day 1 (redoing lost challenges)

This commit is contained in:
dece 2020-12-27 19:32:47 +01:00
parent 56ec1d2ec7
commit 351fc11bed
6 changed files with 65 additions and 0 deletions

1
2015/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

5
2015/Cargo.lock generated Normal file
View 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
View 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
View 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
View 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
View file

@ -0,0 +1 @@
pub mod input;