-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.rs
43 lines (34 loc) · 1.1 KB
/
3.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
const INPUT: &str = include_str!("3.txt");
fn main() { one(); two(); }
fn one() {
let mut result = process_houses(0..INPUT.len());
result.sort();
result.dedup();
result = result.len();
println!("{result}");
}
// Takes an iterator of indices or the houses and returns a vector of
// coordinates after the respective amount of instructions.
fn process_houses<T: Iterator<Item = usize>>(indices: T) -> Vec<(i16, i16)> {
let mut houses = vec![(0, 0)];
for i in indices {
let (x_last, y_last) = *houses.last().unwrap();
houses.push(match INPUT.chars().nth(i).unwrap() {
'^' => (x_last, y_last + 1),
'v' => (x_last, y_last - 1),
'<' => (x_last - 1, y_last),
/* '>' */ _ => (x_last + 1, y_last)
});
}
houses
}
fn two() {
let mut all_houses = [
process_houses((0..INPUT.len()).filter(|n| n % 2 == 0)),
process_houses((0..INPUT.len()).filter(|n| n % 2 == 1))
].concat();
all_houses.sort();
all_houses.dedup();
let result = all_houses.len();
println!("{result}");
}