diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-04-12 04:58:02 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-04-12 04:58:02 +0200 |
| commit | 01fe0c7d69cfc15c44bf8da9e7bc12c4b4ced264 (patch) | |
| tree | e40648725f24912be8bac01385eb47219b39dce2 /2022/Rust/src/day9.rs | |
| parent | 1e8e58033cefcaf138c72085b1a5bffb8cd958c9 (diff) | |
| download | aoc-01fe0c7d69cfc15c44bf8da9e7bc12c4b4ced264.tar.xz aoc-01fe0c7d69cfc15c44bf8da9e7bc12c4b4ced264.zip | |
Day11 done in rust
Diffstat (limited to '2022/Rust/src/day9.rs')
| -rw-r--r-- | 2022/Rust/src/day9.rs | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/2022/Rust/src/day9.rs b/2022/Rust/src/day9.rs index b32c910..fbc28d9 100644 --- a/2022/Rust/src/day9.rs +++ b/2022/Rust/src/day9.rs @@ -81,51 +81,32 @@ impl Simulation { } } + fn update_head_helper(&mut self, amount: &usize, tail_idx: usize, pos_increment: (i32, i32)) { + for _ in 1..=*amount { + self.head_pos.0 += pos_increment.0; + self.head_pos.1 += pos_increment.1; + for i in 0..9 { + if !self.is_tail_one_block_away(i) { + self.update_tail(&self.get_direction(i), i); + self.unique_pos.insert(self.tail_pos[tail_idx]); + } + } + } + } + fn update_head(&mut self, command: &Command, tail_idx: usize) { match command { Command::Up(amount) => { - for _ in 1..=*amount { - self.head_pos.1 += 1; - for i in 0..9 { - if !self.is_tail_one_block_away(i) { - self.update_tail(&self.get_direction(i), i); - self.unique_pos.insert(self.tail_pos[tail_idx]); - } - } - } + self.update_head_helper(amount, tail_idx, (0, 1)); } Command::Down(amount) => { - for _ in 1..=*amount { - self.head_pos.1 -= 1; - for i in 0..9 { - if !self.is_tail_one_block_away(i) { - self.update_tail(&self.get_direction(i), i); - self.unique_pos.insert(self.tail_pos[tail_idx]); - } - } - } + self.update_head_helper(amount, tail_idx, (0, -1)); } Command::Right(amount) => { - for _ in 1..=*amount { - self.head_pos.0 += 1; - for i in 0..9 { - if !self.is_tail_one_block_away(i) { - self.update_tail(&self.get_direction(i), i); - self.unique_pos.insert(self.tail_pos[tail_idx]); - } - } - } + self.update_head_helper(amount, tail_idx, (1, 0)); } Command::Left(amount) => { - for _ in 1..=*amount { - self.head_pos.0 -= 1; - for i in 0..9 { - if !self.is_tail_one_block_away(i) { - self.update_tail(&self.get_direction(i), i); - self.unique_pos.insert(self.tail_pos[tail_idx]); - } - } - } + self.update_head_helper(amount, tail_idx, (-1, 0)); } } } |
