AoC of 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 String
s in:
enum Expr<'a> {
&'a str, isize),
Val(&'a str, &'a str, &'a str, &'a str),
Expr(&'a str, &'a str),
Assert(}
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> =
.split('-').map(|e|
s.parse().map_err(|err| mk_e(format!("'{}': {}", e, err)))
e.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:
-
Good mix of “hardware” simulations needs in a single problem.
-
A problem where falling sand is simulated.
-
The only problem I did not manage to solve in 24 hours. I don’t like my solution. But the problem statement is great!
-
A nice problem on unusual number base.
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!