gobanos / cargo-aoc

452 stars 48 forks source link

Store correct answers so later refactoring doesn't accidently break solutions. #48

Closed tocklime closed 4 years ago

tocklime commented 4 years ago

I quite often get the correct solution with very messy code, then tidy up. A few days later I may refactor multiple solutions together to tidy up further.

It would be nice to store the 'correct' answer in the #[aoc(day1,part1)] line, or maybe a 2nd annotation, which then gets assert_eq!ed in the surrounding code.

That way, once I know the correct answer, I could store it in code and be sure my solutions are all still correct.

Of course I could manually put in the asserts, but that might be verbose?

Alternatively, if there were a version of assert_eq! which returned one of its arguments, that could be quite succinct...

(also, this solution wouldn't work great for the 'graphical' problems where the answer is letters made from a grid of # and space characters)

tocklime commented 4 years ago

...I've just found the contracts crate, which can solve this problem nicely with a line like #[post(ret == 1234)]