Everybody Codes is now in its
third year, and I've been doing it again.
All of my code is on Codeberg.
I like sets. And they make this much easier.
As usual in Rust, when I have short strings used as keys I convert
them to integers (using base-36) for convenience. (To get them out
again for debugging I used the radix_fmt crate but that isn't needed
to solve the actual problems.)
Part 1 is straightforward: map from the first item to a set of each of
the others. So "BRDS:FLWR,SLTT,YSTS" produces the hash entry:
BRDS → Set(FLWR, SLTT, YSTS)
Then walk the tree (I use a DFS purely to avoid pulling in a
VecDeque and because it makes no difference to this whether I go
depth or breadth first) and get a list of everything that's
referenced, then delete the original.
Part 2 is almost the same, but the mapping is reversed. So from that
same line "BRDS:FLWR,SLTT,YSTS" produces:
FLWR → Set(BRDS)
SLTT → Set(BRDS)
YSTS → Set(BRDS)
and then adding "FLWR:SLTT,YSTS" gives an aggregate of:
FLWR → Set(BRDS)
SLTT → Set(BRDS, FLWR)
YSTS → Set(BRDS, FLWR)
The rest of the code is identical.
Part 3 starts with part 2, but I have three separate map-walks to
produce the sets of items using each of the three nulls.
Then I intersect each pair of sets, and union that intersection into
the output set, thus getting everything that uses two nulls (and
deduplicating any three-nulls items automatically as needed).
All of this can be done without a native set type, but it's tedious.
Parts 1 and 2 were fairly straightforward: FIFO queue. (The only
difference between 1 and 2 is the incoming energy.)
In part 3 the timing was more important, and my "queue" ended up being
a hashmap of targets, each with a vec of incoming impulses. So the round
ended up being:
-
process the incoming queue, adding energy and putting new impulse
entries on a new outgoing queue.
-
scan each cloud looking for explosions. If one is found, generate
more impulses on that same outgoing queue, and add that cloud ID to
the deletion list.
-
delete each exploded cloud from the hash of clouds, and from the
outgoing queue.
-
exit if the outgoing queue is empty, otherwise loop again.
The final version took a while to run so I could probably do it more
efficiently, but I took a chance with the maximum found by the 12th ID
(in random order, hash keys after all) when about 20 IDs had been
calculated, and that turned out to be the answer. (If it hadn't been,
I'd have let the code run to completion — as I did — and put in the
final value.)
Definitely one where I could have done better by building a proper
hex-grid data structure. But I didn't. Too bad.
I did assume I'd be looking at different directions of light, and that
was indeed the case. So I split out the shade calculator into its own
function. Not that it did very much yet.
Part 2 was the biggest slog. An enum for directions is all very well,
but the horizontal passes (east/west) need to start at the right point
and then jump by two in X. Probably some of the code I put in to
achieve this is unnecessary; the important bit is the "look along the
row until you find the first thing that exists, then step two from
each one after that".
In part 3 I had the realisation that I could treat a seed as a tree of
height -2, which grows each round whether lit or not. Then it was just
a matter of passing a mutable reference to the field all the way
down into the shade calculator, and tracking the count of new warriors
as well as energy. (There's commented-out code for displaying the
field too, as I used it in debugging.)
Some serious thinking needed this time.
A Plant structure with a grow function and an elements function
seems like the way to do this, and that served me well all through the
problem. The first part was mostly fiddling with the structure to get
the right results; what I ended up doing was storing a Counter of
"twigs" (segments with no divisions, stored by length) and a count of
"centres" (junction points). Then, when twigs equal or exceed the
growth limit:
- note the count of twigs, ct
- add centres equal to ct
- remove these twigs
- add ct×2 twigs of half the length
- add ct twigs of length 1
The number of elements is the number of centres, plus the total
length of all the twigs.
For part 2, I just need a different elements function, and a bit of
inspection reveals that the number of leaves equals one more than the
number of centres.
For part 3, I looked around for a purely mathematical solution, but
while I found some interesting correlations with the growth=3 layout I
wasn't able to generalise it. So instead I built in a skip: if the
longest twig is length A, and a split happens when it hits length
B, then I can skip forward by B-A frames, increment all twig
lengths by that amount, and then resolve the growth as above.
I borrow "Coord" and "Dir" classes from previous years.
For the first part, I establish field, a HashSet of valid spaces to
move into, and ghosts, a Vec of the Ghost type, which contains a
position and a direction. (A Ghost starting space is necessarily in
the field.)
Then for each cycle I perform two operations:
(1) For each ghost, test whether the position in front of it exists;
if so move it, if not turn it and score a collision.
(2) Sort the ghosts into a HashMap keyed by their current position
(and containing Vecs of their directions, since that's all I need to
know). For each entry, if there's one ghost, just make a new one; if
there are more, score collisions equal to the number, reverse the
directions if there's an even number of ghosts involved, and write out
the new ones.
In part 2 I was deceived by the statement All the movement and
collision rules from Part I remained exactly the same. The number of
collision events that happen in an N-ghost intersection is now 1
rather than N. But my test data passed by the old rules, so that took
a bit of digging.
For part 3, well, it ended up looking quite like yesterday's problem
with the need to skip forward to significant events; in this case if a
list of ghost B matches a list A from an earlier cycle, I can look
through them to see how many more cycles I'll need to run before any
ghost hits its collision limit (and then simulate them more cheaply).
Things I learned here:
-
using std::iter::zip to run through two lists side by side rather
than just using an index value.
-
using std::hash to build a custom hasher on the Ghost structure,
so that it would give ghosts with equal position and direction the
same hash key even if the number of collisions were different.
-
rewriting the ghost to ghost collision detection logic from parts
1-2, so that it would preserve the order of the ghosts (so that I
could quickly compare two lists of them, can't use a hash as a hash
key).
And, in case the data get huge, I clear the cache of previously-seen
ghost positions whenever a ghost is removed.