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.
Day 1
This implementation use a BinaryHeap.
Day 2
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
.
Day 3
This implementation use HashSets and the let else
new (at the time of writing) feature.
Day 4
Using the somethat recent TryFrom<&[T]> for [T; N]
implementation. Also using function as argument to mutualize part 1 and part 2.
Day 5
impl FromStr for Move
to use line.parse()
in the code.
Day 6
A very simple implementation (but not optimal) thanks to windows and iterators.
Day 7
Elegant and simple recursion with a closure as visitor. Also, readable parsing using pattern matching on a slice.
Day 8
Genericity by using iterators as function argument.
Day 9
Using RangeInclusive::contains and pattern matching on Ordering.
Day 10
Nothing really special. Part 2 must be "decoded" by eye.
Day 11
Some closure wrapped in Arc<dyn Fn>
.
Day 12
A very compact BFS implementation thanks to VecDeque, Extend and returning impl Iterator
.
Day 13
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
.
Day 14
A small macro to mutualize some code with break
and continue
.
Day 15
Using RangeInclusive as intervals, and regex for parsing.