-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2015-12-01.rs
86 lines (69 loc) · 1.94 KB
/
2015-12-01.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Day 1: Not Quite Lisp
use std::str::FromStr;
use aoc_ornaments::{Part, Solution};
#[derive(Debug, derive_more::Deref)]
struct Day(Vec<i32>);
impl Solution for Day {
type Output = i32;
fn part1(&mut self) -> miette::Result<Self::Output> {
Ok(self.iter().sum())
}
fn part2(&mut self) -> miette::Result<Self::Output> {
let output = self.iter()
.scan(0, |floor, &x| {
*floor += x;
Some(*floor)
})
.position(|floor| floor < 0)
.map(|pos| pos + 1)
.unwrap_or(0);
Ok(output as i32)
}
}
impl FromStr for Day {
type Err = miette::Error;
fn from_str(input: &str) -> miette::Result<Self> {
let parsed = input.chars().map(|c| {
match c {
'(' => 1,
')' => -1,
_ => 0,
}
}).collect();
Ok(Self(parsed))
}
}
fn main() -> miette::Result<()> {
let mut day = Day::from_str(include_str!("./inputs/2015-12-01.txt"))?;
let part1 = day.solve(Part::One)?;
let part2 = day.solve(Part::Two)?;
println!("Part 1: {}", part1);
println!("Part 2: {}", part2);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
#[case("(())", 0)]
#[case("()()", 0)]
#[case("(((", 3)]
#[case("(()(()(", 3)]
#[case("))(((((", 3)]
#[case("())", -1)]
#[case("))(", -1)]
#[case(")))", -3)]
#[case(")())())", -3)]
fn test_cases_part1(#[case] input: &str, #[case] expected: i32) {
let mut day = Day::from_str(input).unwrap();
assert_eq!(day.solve(Part::One).unwrap(), expected.to_string());
}
#[rstest]
#[case(")", 1)]
#[case("()())", 5)]
fn test_cases_part2(#[case] input: &str, #[case] expected: i32) {
let mut day = Day::from_str(input).unwrap();
assert_eq!(day.solve(Part::Two).unwrap(), expected.to_string());
}
}