-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.rs
197 lines (163 loc) · 4.59 KB
/
lib.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
extern crate core;
use std::collections::HashSet;
pub fn rope1(input: &str) -> i32 {
let lines: Vec<_> = input.lines().collect();
let mut visited = HashSet::new();
let mut h = Position { x: 0, y: 0 };
let mut t = Position { x: 0, y: 0 };
visited.insert((0, 0));
lines.iter().for_each(|line| {
let m = get_move(line);
for _ in 0..m.x.abs() {
h.x += if m.x > 0 { 1 } else { -1 };
t = tail_position(&h, &t);
visited.insert((t.x, t.y));
}
for _ in 0..m.y.abs() {
h.y += if m.y > 0 { 1 } else { -1 };
t = tail_position(&h, &t);
visited.insert((t.x, t.y));
}
});
visited.len() as i32
}
#[derive(Eq, PartialEq)]
struct Position {
x: i32,
y: i32,
}
fn get_move(m: &str) -> Position {
let s: Vec<&str> = m.split(' ').collect();
let v: i32 = s[1].parse().unwrap();
match s[0] {
"R" => Position { x: v, y: 0 },
"L" => Position { x: -v, y: 0 },
"U" => Position { x: 0, y: v },
"D" => Position { x: 0, y: -v },
_ => Position { x: 0, y: 0 },
}
}
fn tail_position(h: &Position, t: &Position) -> Position {
// Same
if h.x == t.x && h.y == t.y {
return Position { x: h.x, y: h.y };
}
// Head is right
if h.y == t.y && h.x > t.x {
return Position { x: h.x - 1, y: t.y };
}
// Head is left
if h.y == t.y && h.x < t.x {
return Position { x: h.x + 1, y: t.y };
}
// Head is up
if h.x == t.x && h.y > t.y {
return Position { x: h.x, y: h.y - 1 };
}
// Head is down
if h.x == t.x && h.y < t.y {
return Position { x: h.x, y: h.y + 1 };
}
if is_touching(h, t) {
return Position { x: t.x, y: t.y };
}
// Diagonally: up right
if h.x > t.x && h.y > t.y {
return Position {
x: t.x + 1,
y: t.y + 1,
};
}
// Diagonally: up left
if h.x < t.x && h.y > t.y {
return Position {
x: t.x - 1,
y: t.y + 1,
};
}
// Diagonally: bottom right
if h.x > t.x && h.y < t.y {
return Position {
x: t.x + 1,
y: t.y - 1,
};
}
// Diagonally: bottom left
if h.x < t.x && h.y < t.y {
return Position {
x: t.x - 1,
y: t.y - 1,
};
}
Position { x: 0, y: 0 }
}
fn is_touching(h: &Position, t: &Position) -> bool {
(h.x - t.x).abs() == 1 && (h.y - t.y).abs() == 1
}
pub fn rope2(input: &str) -> i32 {
let lines: Vec<_> = input.lines().collect();
let mut visited = HashSet::new();
let mut h = Position { x: 0, y: 0 };
let mut knots = vec![];
for _ in 0..9 {
knots.push(Position { x: 0, y: 0 })
}
visited.insert((0, 0));
lines.iter().for_each(|line| {
let m = get_move(line);
for _ in 0..m.x.abs() {
h.x += if m.x > 0 { 1 } else { -1 };
for i in 0..9 {
let mut before = &h;
if i > 0 {
before = &knots.get(i - 1).unwrap();
}
let res = tail_position(before, &knots.get(i).unwrap());
let mut elem = &mut knots[i];
elem.x = res.x;
elem.y = res.y;
}
visited.insert((knots.get(8).unwrap().x, knots.get(8).unwrap().y));
}
for _ in 0..m.y.abs() {
h.y += if m.y > 0 { 1 } else { -1 };
for i in 0..9 {
let mut before = &h;
if i > 0 {
before = &knots.get(i - 1).unwrap();
}
let res = tail_position(before, &knots.get(i).unwrap());
let mut elem = &mut knots[i];
elem.x = res.x;
elem.y = res.y;
}
visited.insert((knots.get(8).unwrap().x, knots.get(8).unwrap().y));
}
});
visited.len() as i32
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_rope1_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(rope1(s.as_str()), 13);
}
#[test]
fn test_rope1_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(rope1(s.as_str()), 5245);
}
#[test]
fn test_rope2_unit() {
let s = fs::read_to_string("larger_test.txt").unwrap();
assert_eq!(rope2(s.as_str()), 36);
}
#[test]
fn test_rope2_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(rope2(s.as_str()), 2717);
}
}