-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.rs
47 lines (36 loc) · 1.13 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
44
45
46
47
const INPUT: &str = include_str!("3.txt");
fn main() { one(); two(); }
fn one() {
let mut houses = process_houses(0..INPUT.len());
houses.sort();
houses.dedup();
let result = houses.len();
print!("3.1: {result}\t\t");
}
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!("3.2: {result}");
}
// Takes an iterator of indices and returns a vector of coordinates after the respective
// amount of instructions
fn process_houses<T: IntoIterator<Item = usize>>(indices: T) -> Vec<(i32, i32)> {
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
}