-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_09.rs
59 lines (45 loc) · 1.34 KB
/
day_09.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
use std::vec;
use hashbrown::HashSet;
use common::{solution, Answer};
use nd_vec::vector;
type Point = nd_vec::Vec2<i32>;
solution!("Rope Bridge", 9);
fn part_a(input: &str) -> Answer {
process(input, 1).into()
}
fn part_b(input: &str) -> Answer {
process(input, 9).into()
}
fn process(raw: &str, count: usize) -> usize {
let orders = raw.lines().map(from_line).collect::<Vec<_>>();
let mut tail_pios = HashSet::new();
let mut knots = vec![vector!(0, 0); count + 1];
tail_pios.insert(*knots.last().unwrap());
for (dir, count) in orders {
for _ in 0..count {
knots[0] += dir;
for i in 1..knots.len() {
let diff = knots[i - 1] - knots[i];
if diff.abs().max_component() <= 1 {
continue;
}
knots[i] += diff.signum();
}
tail_pios.insert(*knots.last().unwrap());
}
}
tail_pios.len()
}
// Direction, count
fn from_line(imp: &str) -> (Point, u32) {
let (direction, count) = imp.split_once(' ').unwrap();
let count = count.parse::<i32>().unwrap();
let out = match direction {
"R" => vector!(1, 0),
"L" => vector!(-1, 0),
"U" => vector!(0, -1),
"D" => vector!(0, 1),
_ => panic!("Invalid direction"),
};
(out, count as u32)
}