-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlib.rs
84 lines (79 loc) · 1.82 KB
/
lib.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
use aoclib::model::coord::Coord;
use aoclib::model::coord::Direction;
use std::collections::HashSet;
use std::convert::TryFrom;
pub struct PartOne;
pub struct PartTwo;
impl aoclib::Solvable<&str, usize> for PartOne {
fn solve(input: &str) -> aoclib::Solution<usize> {
let mut set = HashSet::<Coord<i8>>::new();
let mut pos = Coord::new(0, 0);
for d in input.chars().filter_map(|c| Direction::try_from(c).ok()) {
pos += d.value();
set.insert(pos);
}
Ok(set.len())
}
}
impl aoclib::Solvable<&str, usize> for PartTwo {
fn solve(input: &str) -> aoclib::Solution<usize> {
let mut set = HashSet::<Coord<i32>>::new();
let mut s_pos = Coord::new(0, 0);
let mut r_pos = Coord::new(0, 0);
let mut is_r = true;
for d in input.chars().filter_map(|c| Direction::try_from(c).ok()) {
let pos = if is_r { &mut r_pos } else { &mut s_pos };
*pos += d.value();
is_r = !is_r;
set.insert(*pos);
}
Ok(set.len())
}
}
// For some reason these solutions are slower by ~30%
/*
impl aoclib::Solvable<&str, usize> for PartOne {
fn solve(input: &str) -> aoclib::Solution<usize> {
Ok(input
.chars()
.filter_map(|c| Direction::try_from(c).ok())
.fold(
(HashSet::<Coord<i8>>::new(), Coord::new(0, 0)),
|mut acc, d| {
acc.1 += d.value();
acc.0.insert(acc.1);
acc
},
)
.0
.len())
}
}
impl aoclib::Solvable<&str, usize> for PartTwo {
fn solve(input: &str) -> aoclib::Solution<usize> {
Ok(input
.chars()
.filter_map(|c| Direction::try_from(c).ok())
.fold(
(
HashSet::<Coord<i32>>::new(),
Coord::new(0, 0),
Coord::new(0, 0),
true,
),
|mut acc, d| {
if acc.3 {
acc.2 += d.value();
} else {
acc.1 += d.value();
}
acc.3 = !acc.3;
acc.0.insert(if acc.3 { acc.2 } else { acc.1 });
acc
},
)
.0
.len())
}
}
*/