Advent of Code rust nuggets
Advent of Code is a daily set of programming puzzles. After each puzzle is released some solvers post their solutions in the r/adventofcode subreddit. At the start of the month the puzzles are somewhat simple, but even so the solutions tend to look very different, and I learn some cool rust expressions nearly every time. I thought I'd list out some nuggets here for folks who aren't following the event directly. To set expectations: these won't be solutions to the daily problem, just very tiny bits of rust code that used a library or technique that I thought was interesting.
Day 5
Day 5 solution thread: here
- How do you parse pair-of-pairs syntax like "12,3 -> 8,3" (only the integers vary)?
- From u/someleaf (full solution): use Itertools::collect_tuple in your split -> parse -> collect chain.
- How do you iterate from the integer x1 to x2 where it's possible that x1 > x2?
- From u/SuperSmurfen (full solution): use
num::signumto easily get the delta for iteration, e.g.let dx = (x2 - x1).signum().
- From u/SuperSmurfen (full solution): use
Day 4
Day 4 solution thread: here
There's a thread about Day 4 in this subreddit too here.
- How do you represent a 5x5 bingo board where you'll need to do basic operations on rows and columns?
- From u/ritobanrc (full solution): use nalgebra::Matrix5 and you can use
column_iter()androw_iterator()on your board. - From u/rossmacarthur (full solution): use vectrix::Matrix<T, 5, 5> for similar row/col access convenience.
- From u/ritobanrc (full solution): use nalgebra::Matrix5 and you can use
Day 2
Day 2 solution thread: here
- How do you quick 'n dirty parse simple an ad hoc text format? This comes up nearly every day in AoC. If I do another writeup like this later on I'll find a bunch of other styles. Here's one new to me:
- From u/IceSentry (full solution): use serde_scan::scan! to do ad hoc parsing. In this case it was parsing something like "command 123" into
(String, i32)usingscan!("{} {}" <- line), but it looks like it has a bit more power for tougher stuff.
- From u/IceSentry (full solution): use serde_scan::scan! to do ad hoc parsing. In this case it was parsing something like "command 123" into