-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday09.rs
57 lines (48 loc) · 1.29 KB
/
day09.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
use crate::util::hash::*;
use crate::util::iter::*;
use crate::util::parse::*;
use crate::util::point::*;
type Input = (Point, u32);
pub fn parse(input: &str) -> Vec<Input> {
input
.split_ascii_whitespace()
.chunk::<2>()
.map(|[d, n]| {
let point = Point::from_string(d);
let amount = from(n);
(point, amount)
})
.collect()
}
pub fn part1(input: &[Input]) -> usize {
simulate(input, 2)
}
pub fn part2(input: &[Input]) -> usize {
simulate(input, 10)
}
fn simulate(input: &[Input], size: usize) -> usize {
let mut rope: Vec<Point> = vec![ORIGIN; size];
let mut tail: FastSet<Point> = FastSetBuilder::with_capacity(5_000);
for (step, amount) in input {
for _ in 0..*amount {
rope[0] += *step;
for i in 1..size {
if apart(rope[i - 1], rope[i]) {
let next = delta(rope[i - 1], rope[i]);
rope[i] += next;
}
}
tail.insert(rope[size - 1]);
}
}
tail.len()
}
fn apart(a: Point, b: Point) -> bool {
(a.x - b.x).abs() > 1 || (a.y - b.y).abs() > 1
}
fn delta(a: Point, b: Point) -> Point {
Point {
x: (a.x - b.x).signum(),
y: (a.y - b.y).signum(),
}
}