Advent of Code rust nuggets

28 points johnheitmann

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)?
  • How do you iterate from the integer x1 to x2 where it's possible that x1 > x2?
    • From u/SuperSmurfen (full solution): use num::signum to easily get the delta for iteration, e.g. let dx = (x2 - x1).signum().

Day 4

Day 4 solution thread: here

There's a thread about Day 4 in this subreddit too here.

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) using scan!("{} {}" <- line), but it looks like it has a bit more power for tougher stuff.