AoC of 2022

December 25, 2022

This year I again took part in AoC: https://adventofcode.com/2022. I published my solutions at https://github.com/trofi/AoC/tree/main/2022.

As usual problems were appearing once a day at 5AM from Dec 1 to Dec 25. I did not get up that early. Instead my personal goal was to solve them within 24 hours of publish time and get the source code within 4KB each. I failed a few of them.

This year I again took rust (without cargo) to get some practice in it. I think it went a bit smoother this time.

rust niceties: lifetime tracking and error handling

I was more conscious of decreasing amount of data copying around. Instead of slapping .clone() all over the place. rust is great at tracking dangling references with lifetimes. For example in Day 16: Proboscidea Volcanium I encoded search space as a vector and a set of &str “pointers” instead of copying Strings in:

enum Expr<'a> {
  Val(&'a str, isize),
  Expr(&'a str, &'a str, &'a str, &'a str),
  Assert(&'a str, &'a str),
}

fn parse<'a>(i: &'a str) -> Vec<Expr<'a>> {
  ...

I even attempted using a bit of error handling in 1-2 problems instead of panic!()-ing on unexpected data:

impl FromStr for Assignment {
  type Err = E;
  fn from_str(s: &str) -> Result<Self, Self::Err> {
    let numbers: Result<Vec<usize>, Self::Err> =
      s.split('-').map(|e|
        e.parse().map_err(|err| mk_e(format!("'{}': {}", e, err)))
      ).collect();
    match numbers?.as_slice() {
      &[a, b] => Ok(Assignment{from: a, to: b}),
      _       => Err(mk_e(format!("'{}' does not match N-M format", s)))
    }
  }
}

Here Iter<Result<...>> constructs Result<Vec<...>> (instead of typical Vec<Result<...>>!). It’s a handy way to pop errors out from iterator based loops. I’m still very clumzy at constructing new errors. But at least I did not lose them here.

Funniest problems

The solutions to most problems are technically very boring. But some problem statements are great: a bit of ray casting, 2d and 3d physics simulation and even CPU+CRT simulation!

Here is my list of fun problems I remembered:

I did not include a bunch of other problems like robot production and valve handling as they looked very similar to me. I solved them all with A* search.

Grepping through the solutions visited keyword happens 5 times. I would say it is too many similar search-in-graph problems in a single year. But maybe it’s just me who used search too frequently without an attempt at finding better solution?

Have fun!