-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
175 lines (143 loc) · 3.78 KB
/
mod.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::utils::ParseError;
use anyhow::{Context, Error, Result};
use std::{collections::HashSet, str::FromStr};
type Coords = (isize, isize);
#[derive(Debug)]
pub enum Direction {
Up,
Left,
Down,
Right,
}
#[derive(Debug)]
pub struct Operation {
direction: Direction,
distance: isize,
}
impl FromStr for Operation {
type Err = Error;
fn from_str(s: &str) -> Result<Operation> {
let mut split = s.split(' ');
let dir = split.next().context("Parsing direction failed")?;
let direction = match dir {
"L" => Direction::Left,
"D" => Direction::Down,
"R" => Direction::Right,
"U" => Direction::Up,
_ => Err(ParseError::new("Cannot parse direction"))?,
};
let distance = isize::from_str(split.next().context("Parsing distance failed")?)?;
Ok(Operation {
direction,
distance,
})
}
}
#[aoc_generator(day09)]
pub fn input_generator(input: &str) -> Result<Vec<Operation>> {
input
.lines()
.filter(|s| !s.is_empty())
.map(|s| s.trim())
.map(Operation::from_str)
.collect::<Result<Vec<_>, Error>>()
}
fn move_head(op: &Operation, p: &Coords) -> Coords {
let d = 1;
match op.direction {
Direction::Up => (p.0, p.1 - d),
Direction::Left => (p.0 - d, p.1),
Direction::Down => (p.0, d + p.1),
Direction::Right => (p.0 + d, p.1),
}
}
fn close_enough(a: &Coords, b: &Coords) -> bool {
(a.0 - b.0).abs() <= 1 && (a.1 - b.1).abs() <= 1
}
fn update_member(head: &Coords, member: Coords) -> Coords {
if close_enough(head, &member) {
return member;
}
let diff = (head.0 - member.0, head.1 - member.1);
(member.0 + diff.0.signum(), member.1 + diff.1.signum())
}
fn update_tail(head: &Coords, mut tail: Vec<Coords>) -> Vec<Coords> {
tail[0] = update_member(head, tail[0]);
for i in 1..tail.len() {
tail[i] = update_member(&tail[i - 1], tail[i]);
}
tail
}
fn simulate(ops: &Vec<Operation>, mut rope: Vec<Coords>) -> Option<usize> {
let mut visited: HashSet<Coords> = HashSet::new();
let mut head = (0, 0);
visited.insert(*rope.last()?);
for op in ops {
for _ in 0..op.distance {
head = move_head(op, &head);
rope = update_tail(&head, rope);
visited.insert(*rope.last()?);
}
}
Some(visited.len())
}
#[aoc(day09, part1)]
pub fn solve_part1(input: &Vec<Operation>) -> Result<usize> {
let rope = vec![(0, 0)];
simulate(input, rope).context("Simulation failed")
}
#[aoc(day09, part2)]
pub fn solve_part2(input: &Vec<Operation>) -> Result<usize> {
let rope = vec![(0, 0); 9];
simulate(input, rope).context("Simulation failed")
}
#[cfg(test)]
mod test {
use super::*;
fn sample1() -> &'static str {
"R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2"
}
fn input1() -> Result<Vec<Operation>> {
input_generator(sample1())
}
#[test]
fn part1_sample1() -> Result<()> {
let data = input1()?;
Ok(assert_eq!(13, solve_part1(&data)?))
}
#[test]
fn part2_sample1() -> Result<()> {
let data = input1()?;
Ok(assert_eq!(1, solve_part2(&data)?))
}
fn sample2() -> &'static str {
"R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20"
}
fn input2() -> Result<Vec<Operation>> {
input_generator(sample2())
}
#[test]
fn part1_sample2() -> Result<()> {
let data = input2()?;
Ok(assert_eq!(88, solve_part1(&data)?))
}
#[test]
fn part2_sample2() -> Result<()> {
let data = input2()?;
Ok(assert_eq!(36, solve_part2(&data)?))
}
}