More advent of coding! These last three days have been interesting, and the main reason it’s taken so long to solve them is finding time to do them.
Day 8
Another day, another input that is interpreted as a map.
I once again used the coords module I wrote for an earlier day.
The story is that there are some antennas and you need to find
spots where the antennas resonate with each other.
What that amounts to is using a bit of vector arithmetic on the
coordinates of the antennas.
I implemented some arithmetic operations for my coords struct,
but because of how things work, I need arithmetic to work with values and references.
This means I need to implement addition for two values and for a value and a reference.
That is, I need c + d to work, and also c + &d.
(And for completion’s sake I should probably also do two references, and maybe a reference and a value).
And then the same again for subtraction, multiplication and so on.
Maybe there’s a trick to make it less cumbersome to handle these cases, but I got it working
so I’m happy enough for now.
Day 9
Day 9 was a fun one. I had to create a data structure to hold some data, and parse some data encoded in a bit of a weird way. Then I had to defragment the data structure. This was cool because it felt closer to a programming task that really does need solving. It’s a simple memory management system! That’s a real problem! My solution is probably not efficient, but it’s not terrible.
I don’t really have much to say about this one, but I do remember that I did most of the work for it in Rome Fiumicino airport on my way back from watching Ireland beat Italy in the Men’s Six Nations.
Day 10
And yet another use of the coords module.
I had to refector it to turn a string of characters into
a hashmap from coords to digits (instead of chars),
but that turned out to be straightforward.
The puzzle then involved counting paths through the
map where paths have to go up by 1 on each step.
I had a bit of a fight with the rust type checker on this one.
Now, the type checker was right, but I still think it was being pedantic.
So the issue is that my solution involved flat_map repeatedly.
I had to declare the variable outside of the loop and repeatedly assign to it so that
the final value would be available when the loop was finished.
That meant that each assignment to the variable had to be of the same type.
A type such as FlatMap includes as part of its type declaration,
the closure you pass it.
So the type is different from stage to stage.
Now I guess, in my actual use case, I could define the function in the flat_map
somewhere else, and then it would be the same everywhere, but that won’t help because there’s another issue:
The type changes from Iter to FlatMap<Iter...> to FlatMap<FlatMap<Iter...
as you keep applying it.
It’s being evaluated lazily, I guess.
let mut v = vec![1,2,3,4].iter();
let x = v.flat_map(|x| [x]);
v = v.flat_map(|x| [x]);
Assigning to x works fine, but assigning to v again causes a type error for the above reasons.
So the solution is to do what I only recently learned is something that you should try to avoid:
collect into a vector on each iteration.
The vectors all have the same type, and they don’t involve the closure any more.
I had another classic Advent of Code experience on this one: my orginal incorrect solution for part 1 turned out to be precisely the solution for part 2. There is all of four minutes between the commit where I’d finished part 1 and the commit where I finished part 2.