Showing results for 
Search instead for 
Did you mean: 

--- AOC Day 1: Sonar Sweep ---

Community Manager Community Manager
Community Manager

You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!

Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars.

Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.

For example, suppose you had the following report:


This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.

The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something.

To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows:

199 (N/A - no previous measurement)
200 (increased)
208 (increased)
210 (increased)
200 (decreased)
207 (increased)
240 (increased)
269 (increased)
260 (decreased)
263 (increased)

In this example, there are 7 measurements that are larger than the previous measurement.

How many measurements are larger than the previous measurement?


All credit for the above puzzle goes to Eric Wastl and Advent of Code.

Don't forget to submit your solutions at and join the KX leaderboard using code 1526329-bb0915a5.


Valued Contributor

The birth of the cool

This year’s Advent of Code has raised the excitement level in the vector dojo because the puzzles are particularly susceptible to vector solutions. 

We nod to code golfers who seek the shortest solutions, and to others who pursue the fastest. But in the dojo we seek the most ‘vector-ish’ solutions. That’s admittedly a bit vague, because it is at bottom an aesthetic criterion – often pronounced “cool”.

Feel free to chime in with comments and alternatives: what follows is unlikely to be the last word in cool.


Spoilers follow

Day 1: Sonar Sweep

Every puzzle has the same first step: ingesting the data. We‘re proud of how easy it is to convert each day’s text file into a tractable q data structure.

q)show d:"J"$read0 `:day1.txt
148 167 168 169 182 188 193 209 195 206 214 219 225 219 211 215 216 195 200 1..

How many of these depth measurements are larger than the previous measurement?

The Each Prior iterator applied to the Greater Than operator > derives a function >': that tells us exactly that. We use it with a zero left argument for the first comparison.


We’re not interested in the first comparison, so we discard it and count the remaining hits.

q)sum 1_ 0>':d

Because we are not interested in the first comparison, the 0 could be any integer. Perhaps better to get rid of it entirely, and apply >': as a unary, using either bracket or prefix syntax.

q)sum 1_ >':[d] / bracket
q)sum 1_ (>':)d / prefix

Without the 0 left argument to >':, q has its own rules for what to compare the first item of d to. We don’t care, but you can read about these defaults.

We see above that the derived function >': is variadic: we can apply it as either a unary or a binary. Applying it as a unary means we could instead use the prior keyword.

q)sum 1 _ (>) prior d

That is better q style, and perhaps the coolest way to write the solution.

Note the parens in (>), which give it noun syntax. That is, the parser reads it as the left argument of prior rather than trying to apply it. With this as a model, part 2 looks simple. We want the 3-point moving sums of d, of which we shall ignore the first two.

q)a:()!"j"$() / answers
q)a[`$"1-1"]:sum 1 _ (>) prior d
q)a[`$"1-2"]:sum 1 _ (>) prior 2 _ 3 msum d
q)show a
1-1| 1400
1-2| 1429

Oleg Finkelshteyn has a simpler (and faster) solution that baffles me.


Factoring out the repeated constant, I prefer this as

q)sum .[>] (1 neg\3)_\:d

but still cannot see why comparing d[i] to d[i-3] gives the same result as comparing the moving sums. Help, anyone?

New Contributor III
L:149 163 165 160 179 184 186 199 207 210 / first 10 depths 
sum L[0 1 2] < sum L[1 2 3] / compare first 2 moving sums
L[0] + sum L[1 2] < L[3] + sum L[1 2] / factor out repeated sums
L[0] < L[3] / simplified comparison