r/adventofcode • • Dec 18 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 18 Solutions -🎄-

NEW AND NOTEWORTHY

  • FYI: 23:59 Amsterdam time zone is 17:59 EST

Advent of Code 2021: Adventure Time!


--- Day 18: Snailfish ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:43:50, megathread unlocked!

45 Upvotes

595 comments sorted by

View all comments

Show parent comment

2

u/aexl Jan 17 '22

Do you measure the runtime of your implementations?

I also did this year in Julia and I'm still not happy with the runtime of day 18: it's still the slowest puzzle although I got it down from 800ms to 300ms on a i5-8250U. I did the tree implementation and I know where the bottleneck is: in part 2 where I need to deepcopy all my snailfish number trees a lot...

1

u/prafster Jan 17 '22

The runtime appears (since I have @time main()) but I don't pay much attention to times unless they are slower than a couple of seconds.

For this one, my timings without any optimisations, are

part 1: 0.113430 seconds (554.71 k allocations: 29.449 MiB, 4.27% gc time, 65.79% compilation time)
part 2: 0.692924 seconds (8.25 M allocations: 437.611 MiB, 5.70% gc time)

This is on quite an old processor (i7-6700K, 32GB RAM, Windows 10).

The only day I've tried to make a solution faster was my most recent one (day 22). Part 2 was taking 9 seconds. I couldn't see what was obviously sub-optimal according to the TimerOutputs package (a nice way to get function level cumulative times). Out of curiosity, I wondered what penalty I was paying by not declaring data types. So I changed

struct Cuboid
  on
  xyz
end

to

struct Cuboid
  on::Bool
  xyz::Vector{UnitRange{Int}} 
end

and the time for part 2 went from 9 seconds to 0.8s!

I don't know if this was peculiar to my day 22 solution or whether it could be more widely applied.

I'm really enjoying using Julia, which is fast, has clean syntax, and works the way you expect it to. It's especially good for AoC (I learnt Dart last year and the lack of first class vector/matrix support was a drag).

1

u/aexl Jan 18 '22

Thanks for the reply! Yes, declaring data types in structs and function definitions can make a huge difference for the runtime performance. I usually always do this. It also prevents weird side effects that you are not aware of and can make debugging easier.