-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-9.rs
139 lines (126 loc) · 3.17 KB
/
day-9.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::{collections::HashSet, error::Error};
const INPUT: &str = include_str!("day-9.txt");
#[cfg(test)]
const INPUT_EX: &str = "R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2";
#[cfg(test)]
const INPUT_EX2: &str = "R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20";
aoc_2022::aoc! {
struct Day9 {
motions: Vec<Motion>,
}
self(input) {
Ok(Self {
motions: input.lines().map(|line| {
let Some((m, n)) = line.split_once(' ') else {
return Err("invalid input".into());
};
let n: isize = n.parse()?;
Ok(match m {
"U" => Motion::Vertical(-n),
"D" => Motion::Vertical(n),
"L" => Motion::Horizontal(-n),
"R" => Motion::Horizontal(n),
_ => return Err("unknown motion".into()),
})
}).collect::<Result<_, Box<dyn Error>>>()?
})
}
part1 usize {
let mut rope = Rope::new(1);
for &m in self.motions.iter() {
rope.motion(m);
}
Ok(rope.tail_history.len())
}
part2 usize {
let mut rope = Rope::new(9);
for &m in self.motions.iter() {
rope.motion(m);
}
Ok(rope.tail_history.len())
}
input = INPUT;
test day9_ex(INPUT_EX, 13, 1);
test day9_ex2(INPUT_EX2,, 36);
test day9(INPUT, 6563, 2653);
}
struct Rope {
head: (isize, isize),
tail: Vec<(isize, isize)>,
tail_history: HashSet<(isize, isize)>,
}
impl Rope {
fn new(len: usize) -> Self {
let tail = vec![(0, 0); len];
let mut tail_history = HashSet::new();
tail_history.insert((0, 0));
Self {
head: (0, 0),
tail,
tail_history,
}
}
fn motion(&mut self, motion: Motion) {
match motion {
Motion::Horizontal(n) => {
if n < 0 {
for _ in 0..-n {
self.step(-1, 0);
}
} else {
for _ in 0..n {
self.step(1, 0);
}
}
}
Motion::Vertical(n) => {
if n < 0 {
for _ in 0..-n {
self.step(0, -1);
}
} else {
for _ in 0..n {
self.step(0, 1);
}
}
}
}
}
fn step(&mut self, dx: isize, dy: isize) {
self.head.0 += dx;
self.head.1 += dy;
let mut prev = self.head;
for tail in self.tail.iter_mut() {
let dx = prev.0 - tail.0;
let dy = prev.1 - tail.1;
if dx.abs() > 1 || dy.abs() > 1 {
let dx = dx.clamp(-1, 1);
let dy = dy.clamp(-1, 1);
*tail = (tail.0 + dx, tail.1 + dy);
prev = *tail;
} else {
break;
}
}
self.tail_history.insert(*self.tail.last().unwrap());
}
}
#[derive(Clone, Copy)]
enum Motion {
Horizontal(isize),
Vertical(isize),
}