From 351fc11bedb5854f8a37fd5cf6a0cd973d5b457c Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 27 Dec 2020 19:32:47 +0100 Subject: [PATCH] 2015 day 1 (redoing lost challenges) --- 2015/.gitignore | 1 + 2015/Cargo.lock | 5 +++++ 2015/Cargo.toml | 10 ++++++++++ 2015/src/bin/day1.rs | 31 +++++++++++++++++++++++++++++++ 2015/src/input.rs | 17 +++++++++++++++++ 2015/src/lib.rs | 1 + 6 files changed, 65 insertions(+) create mode 100644 2015/.gitignore create mode 100644 2015/Cargo.lock create mode 100644 2015/Cargo.toml create mode 100644 2015/src/bin/day1.rs create mode 100644 2015/src/input.rs create mode 100644 2015/src/lib.rs diff --git a/2015/.gitignore b/2015/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/2015/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2015/Cargo.lock b/2015/Cargo.lock new file mode 100644 index 0000000..6a45c5d --- /dev/null +++ b/2015/Cargo.lock @@ -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" diff --git a/2015/Cargo.toml b/2015/Cargo.toml new file mode 100644 index 0000000..894e7fa --- /dev/null +++ b/2015/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "aoc2015" +version = "0.1.0" +authors = ["dece "] +edition = "2018" + +[lib] +name = "aoc" + +[dependencies] diff --git a/2015/src/bin/day1.rs b/2015/src/bin/day1.rs new file mode 100644 index 0000000..35d770f --- /dev/null +++ b/2015/src/bin/day1.rs @@ -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 + } + } +} diff --git a/2015/src/input.rs b/2015/src/input.rs new file mode 100644 index 0000000..e0e02d1 --- /dev/null +++ b/2015/src/input.rs @@ -0,0 +1,17 @@ +use std::io; + +pub fn read_lines() -> Vec { + 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 +} diff --git a/2015/src/lib.rs b/2015/src/lib.rs new file mode 100644 index 0000000..7839bc5 --- /dev/null +++ b/2015/src/lib.rs @@ -0,0 +1 @@ +pub mod input;