-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.rs
More file actions
189 lines (172 loc) · 6.01 KB
/
18.rs
File metadata and controls
189 lines (172 loc) · 6.01 KB
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
advent_of_code::solution!(18);
enum Angle {
HautGauche,
HautDroite,
BasGauche,
BasDroite,
}
struct MazeContructor {
sommets: Vec<(i64, i64)>,
last_x: i64,
last_y: i64,
current_angle: Angle,
}
impl MazeContructor {
pub fn new() -> Self {
Self {
sommets: vec![(0, 0)],
last_x: 0,
last_y: 0,
current_angle: Angle::HautGauche,
}
}
fn add_trou(&mut self, direction: &str, distance: i64) {
match direction {
"R" => {
match self.current_angle {
Angle::HautGauche => {
self.last_y += distance + 1;
}
Angle::HautDroite => {
self.last_y += distance;
}
Angle::BasDroite => {
self.sommets.pop();
self.last_x -= 1;
self.sommets.push((self.last_x, self.last_y));
self.last_y += distance;
}
Angle::BasGauche => {
panic!("Invalid direction");
}
}
self.current_angle = Angle::HautDroite;
self.sommets.push((self.last_x, self.last_y));
}
"L" => {
match self.current_angle {
Angle::HautGauche => {
self.sommets.pop();
self.last_x += 1;
self.sommets.push((self.last_x, self.last_y));
self.last_y -= distance;
}
Angle::HautDroite => {
panic!("Invalid direction");
}
Angle::BasDroite => {
self.last_y -= distance + 1;
}
Angle::BasGauche => {
self.last_y -= distance;
}
}
self.current_angle = Angle::BasGauche;
self.sommets.push((self.last_x, self.last_y));
}
"U" => {
match self.current_angle {
Angle::HautGauche => {
self.last_x -= distance;
}
Angle::HautDroite => {
self.sommets.pop();
self.last_y -= 1;
self.sommets.push((self.last_x, self.last_y));
self.last_x -= distance;
}
Angle::BasDroite => {
panic!("Invalid direction");
}
Angle::BasGauche => {
self.last_x -= distance + 1;
}
}
self.current_angle = Angle::HautGauche;
self.sommets.push((self.last_x, self.last_y));
}
"D" => {
match self.current_angle {
Angle::HautGauche => {
panic!("Invalid direction");
}
Angle::HautDroite => {
self.last_x += distance + 1;
}
Angle::BasDroite => {
self.last_x += distance;
}
Angle::BasGauche => {
self.sommets.pop();
self.last_y += 1;
self.sommets.push((self.last_x, self.last_y));
self.last_x += distance;
}
}
self.current_angle = Angle::BasDroite;
self.sommets.push((self.last_x, self.last_y));
}
_ => panic!("Invalid direction"),
}
}
fn read_input_part1(&mut self, input: &str) {
for line in input.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() == 3 {
let distance = parts[1].parse::<i64>().unwrap();
self.add_trou(parts[0], distance);
}
}
}
fn read_input_part2(&mut self, input: &str) {
for line in input.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
let color_code = &parts[2][2..7]; // Get the 5 characters after #
let distance = i64::from_str_radix(color_code, 16).unwrap(); // Convert from base 16 to base 10
let direction = parts[2].chars().nth(7).unwrap(); // Get the 6th character
match direction {
'0' => { self.add_trou("R", distance); }
'1' => { self.add_trou("D", distance); }
'2' => { self.add_trou("L", distance); }
'3' => { self.add_trou("U", distance); }
_ => panic!("Invalid direction"),
}
}
}
fn count(&self) -> f64 {
let len = self.sommets.len();
let mut sum = 0_f64;
for i in 0..len {
let (x1, y1) = self.sommets[i];
let (x2, y2) = self.sommets[(i + 1) % len];
sum += ((x1 * y2) - (x2 * y1)) as f64;
}
sum.abs() / 2.0
}
}
pub fn part_one(input: &str) -> Option<u64> {
let mut maze_constructor = MazeContructor::new();
maze_constructor.read_input_part1(input);
let maze = maze_constructor.count();
Some(maze as u64)
}
pub fn part_two(input: &str) -> Option<u64> {
let mut maze_constructor = MazeContructor::new();
maze_constructor.read_input_part2(input);
let maze = maze_constructor.count();
Some(maze as u64)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(62));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(952408144115));
}
}