/aoc2022

https://adventofcode.com/2022/

Primary LanguageRustDo What The F*ck You Want To Public LicenseWTFPL

Advent of Code 2022

This repository contains my solutions for Advent of Code 2022.

Goal is readable, simple and mostly clean.

Each day is solved in it's dedicated file in the src/bin directory.

Only a few well-known dependencies are used:

  • anyhow for easy and simple error handling, in all the files.
  • regex is used in a few files for easy parsing.
  • serde and serde_json are used in day 13 for easy parsing.

About my writing of these files

I am an experienced rust developer. I use rust since 2014 (so before rust 1.0). You may know me for structopt or keyberon. I like to use iterators, the ? operator and prefer (a bit too much) short names to comments.

I have solved these problems by doing some "dirty" things (as .clone() abuse, copy and paste, unreadable mess, damn slow algorithm running during lunch). Then I have cleaned them, and sometime improved them. They all run in less than 2 seconds in release on my computer.

All these programs should solve any problem from the official site, except day 22 (the cube folding is hardcoded for my instance).

Days

In this section, I make a few remarks on the different days. I will suppose you have already read the instructions on the official site.

This implementation use a BinaryHeap.

This implementation use a lot rust "plain enum", and implement the TryFrom on them. It also externalize the preprocessing of the input in a function returning an (somethat) impl Iterator.

This implementation use HashSets and the let else new (at the time of writing) feature.

Using the somethat recent TryFrom<&[T]> for [T; N] implementation. Also using function as argument to mutualize part 1 and part 2.

impl FromStr for Move to use line.parse() in the code.

A very simple implementation (but not optimal) thanks to windows and iterators.

Elegant and simple recursion with a closure as visitor. Also, readable parsing using pattern matching on a slice.

Genericity by using iterators as function argument.

Using RangeInclusive::contains and pattern matching on Ordering.

Nothing really special. Part 2 must be "decoded" by eye.

Some closure wrapped in Arc<dyn Fn>.

A very compact BFS implementation thanks to VecDeque, Extend and returning impl Iterator.

The parsing is done using serde_json and serde with #[serde(untagged)]. If you want a hand-written parser, you can search in the history.

The custom comparison method is implemented as Ord. It is done simply and without allocation thanks to std::slice::from_ref and the Ord implementation of a slice.

A small macro to mutualize some code with break and continue.

Using RangeInclusive as intervals, and regex for parsing.