-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigging_plan.rs
75 lines (65 loc) · 1.88 KB
/
digging_plan.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
use crate::instruction::{Direction, Instruction, InstructionErr};
use std::str::FromStr;
pub(crate) struct DigPlan {
pub(crate) set: std::rc::Rc<[Instruction]>,
}
impl DigPlan {
pub(crate) fn iter(&self) -> impl Iterator<Item = &Instruction> + '_ {
self.set.iter()
}
pub fn _is_clockwise(&self) -> bool {
let mut last: Option<Direction> = None;
self.set
.iter()
.map(|i| {
let out = last
.map(|ld| if i.dir.is_clockwise(ld) { 1 } else { -1 })
.unwrap_or(0);
last = Some(i.dir);
out
})
.sum::<isize>()
.is_positive()
}
}
impl FromStr for DigPlan {
type Err = InstructionErr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut set = vec![];
for line in s.lines() {
set.push(line.parse::<Instruction>()?);
}
Ok(DigPlan { set: set.into() })
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::lagoon::test::load_plan;
#[test]
fn test_digplan_parse() {
let inp = std::fs::read_to_string("./src/bin/day18/sample.txt").expect("Ops!");
let plan = match inp.parse::<DigPlan>() {
Ok(set) => set,
Err(e) => panic!("{}", e),
};
let mut iter = plan.iter();
inp.lines()
.for_each(|line| {
let out = &format!("{:?}",iter.next().unwrap());
println!("{line} => {out}");
assert_eq!(line,out);
});
}
#[test]
fn test_digplan_is_clockwise() {
let plan: DigPlan = match load_plan(None) {
Ok(p) => p,
Err(e) => panic!("{}", e),
};
match plan._is_clockwise() {
true => println!("Clockwise"),
false => println!("Counter Clockwise"),
}
}
}