Skip to content

Commit

Permalink
a line integral for day 18 is slightly faster than using shoelace
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 19, 2023
1 parent 9989ac2 commit 3a18899
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 15](./src/bin/15.rs) | `20.4µs` | `85.9µs` |
| [Day 16](./src/bin/16.rs) | `58.3µs` | `1.7ms` |
| [Day 17](./src/bin/17.rs) | `1.6ms` | `3.7ms` |
| [Day 18](./src/bin/18.rs) | `2.4µs` | `2.5µs` |
| [Day 19](./src/bin/19.rs) | `167.6µs` | `156.8µs` |
| [Day 18](./src/bin/18.rs) | `2.2µs` | `2.4µs` |
| [Day 19](./src/bin/19.rs) | `167.2µs` | `155.1µs` |

**Total: 14.40ms**
<!--- benchmarking table --->
Expand Down
16 changes: 14 additions & 2 deletions src/bin/18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ fn solve_part1_fast(input: &str) -> i64 {
b'U' => Point::new(prev.x, prev.y - distance),
_ => unreachable!(),
};
area += match input[start] {
b'R' => -next.y * distance,
b'D' => next.x * distance,
b'L' => next.y * distance,
b'U' => -next.x * distance,
_ => unreachable!(),
};
index += 15;
area += prev.x * next.y - prev.y * next.x;
border_points += distance;
prev = next;
}
Expand Down Expand Up @@ -59,8 +65,14 @@ fn solve_part2_fast(input: &str) -> i64 {
b'3' => Point::new(prev.x, prev.y - distance),
_ => unreachable!(),
};
area += match input[index + 5] {
b'0' => -next.y * distance,
b'1' => next.x * distance,
b'2' => next.y * distance,
b'3' => -next.x * distance,
_ => unreachable!(),
};
index += 14;
area += prev.x * next.y - prev.y * next.x;
border_points += distance;
prev = next;
}
Expand Down

0 comments on commit 3a18899

Please sign in to comment.