2020 day12 performance #85
Conversation
maneatingape
left a comment
There was a problem hiding this comment.
Nice, I get a roughly 3x speedup.
We can re-use existing parsing utility methods to get the same effect with less code, while keeping the core idea.
| _ => unreachable!(), | ||
| } | ||
| pub fn parse(input: &str) -> Input { | ||
| let mut inp = input.to_string(); |
There was a problem hiding this comment.
parse.rs does exactly this byte by byte parsing, exposed as 2 neat typed iterators (one for unsigned number and one for signed) so that you can write:
use crate::util::parse::*;
let amounts = input.input.signed();
Since we also need the direction command use the nifty methods on Iterator to compose 2 iterators into a single iterator over a 2-tuple, e.g.
let first = input.bytes().filter(u8::is_ascii_uppercase);
let second = input.iter_signed();
We can then use this (without an intermediate vec) in your core loop that combines part one and part two, e.g. something like:
let mut part_one = ORIGIN;
let mut part_two = ORIGIN;
let mut direction = RIGHT;
let mut waypoint = Point::new(10, -1);
for (command, amount) in first.zip(second) {
match command {
b'N' => {
part_one.y -= amount;
waypoint.y -= amount;
}
...
| } | ||
|
|
||
| fn rotate(point: Point, amount: i32) -> Point { | ||
| match amount.rem_euclid(360) { |
There was a problem hiding this comment.
Can omit the rem_euclid if the class from L reflect the rotation e.g. direction = rotate(direction, 360 - amount);
Description
2020 / day12 8x speedup from a custom single-pass parser
Please ignore the spurious day06 commit, I don't know how to get rid of it!
Type of change
Checklist
using the same naming conventions. Code should be portable, avoiding any
architecture-specific intrinsics.
cargo testcargo fmt -- `find . -name "*.rs"`cargo clippy --all-targets --all-featuresFormatting and linting also can be executed by running
just(if installed) on the command line at the project root.