2015: day01: finish

This commit is contained in:
Antoine Martin 2019-12-01 21:16:15 +01:00
parent dd4e610324
commit c420ed9e8b
7 changed files with 133 additions and 0 deletions

4
Cargo.lock generated
View file

@ -1,5 +1,9 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aoc2015"
version = "0.1.0"
[[package]]
name = "aoc2018"
version = "0.1.0"

View file

@ -1,5 +1,6 @@
[workspace]
members = [
"aoc2015",
"aoc2018",
"aoc2019",
]

9
aoc2015/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "aoc2015"
version = "0.1.0"
authors = ["Antoine Martin <antoine97.martin@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1
aoc2015/input/day01.txt Normal file

File diff suppressed because one or more lines are too long

81
aoc2015/src/day01.rs Normal file
View file

@ -0,0 +1,81 @@
use std::error::Error;
use super::err;
use super::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
Ok(())
}
fn part1(input: &str) -> Result<i64> {
Ok(input
.chars()
.filter_map(|c| match c {
'(' => Some(1),
')' => Some(-1),
_ => None,
})
.sum())
}
fn part2(input: &str) -> Result<usize> {
let mut sum = 0;
let mut res = 0;
for (i, c) in input.chars().enumerate() {
match c {
'(' => sum += 1,
')' => sum -= 1,
_ => return Err(err!("unidentified character in input: {}", c)),
};
if sum < 0 {
res = i + 1;
break;
}
}
match res {
0 => Err(err!("never reached the basement...")),
_ => Ok(res),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_provided() {
assert_eq!(part1("(())").unwrap(), 0);
assert_eq!(part1("()()").unwrap(), 0);
assert_eq!(part1("(((").unwrap(), 3);
assert_eq!(part1("(()(()(").unwrap(), 3);
assert_eq!(part1("))(((((").unwrap(), 3);
assert_eq!(part1("())").unwrap(), -1);
assert_eq!(part1("))(").unwrap(), -1);
assert_eq!(part1(")))").unwrap(), -3);
assert_eq!(part1(")())())").unwrap(), -3);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 74);
}
#[test]
fn part2_provided() {
assert_eq!(part2(")").unwrap(), 1);
assert_eq!(part2("()())").unwrap(), 5);
}
#[test]
fn part2_real() {
assert_eq!(part2(INPUT).unwrap(), 1795);
}
}

8
aoc2015/src/lib.rs Normal file
View file

@ -0,0 +1,8 @@
pub mod day01;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[macro_export]
macro_rules! err {
($($string:expr),+) => (Box::<dyn Error>::from(format!($($string),+)))
}

29
aoc2015/src/main.rs Normal file
View file

@ -0,0 +1,29 @@
use std::env;
use aoc2015::day01;
use aoc2015::Result;
fn main() -> Result<()> {
let days: &[fn() -> Result<()>] = &[day01::run];
let mut args = env::args();
args.next();
match args.next() {
Some(arg) => {
let day: usize = arg.parse().expect("Please provide a day number");
days[day - 1]().expect("error running day specified");
}
None => {
for (i, day) in days.iter().enumerate() {
let i = i + 1;
println!("day{}: ", i);
day().unwrap_or_else(|e| panic!("error running day {}: {}", i, e));
println!();
}
}
}
Ok(())
}