--- AOC Day 2: Dive! ---

Now, you need to figure out how to pilot this thing.

It seems like the submarine can take a series of commands like forward 1down 2, or up 3:

  • forward X increases the horizontal position by X units.
  • down X increases the depth by X units.
  • up X decreases the depth by X units.

Note that since you’re on a submarine, down and up affect your depth, and so they have the opposite result of what you might expect.

The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it’s going. For example:

forward 5 down 5 forward 8 up 3 down 8 forward 2

Your horizontal position and depth both start at 0. The steps above would then modify them as follows:

  • forward 5 adds 5 to your horizontal position, a total of 5.
  • down 5 adds 5 to your depth, resulting in a value of 5.
  • forward 8 adds 8 to your horizontal position, a total of 13.
  • up 3 decreases your depth by 3, resulting in a value of 2.
  • down 8 adds 8 to your depth, resulting in a value of 10.
  • forward 2 adds 2 to your horizontal position, a total of 15.

After following these instructions, you would have a horizontal position of 15 and a depth of 10. (Multiplying these together produces 150.)

Calculate the horizontal position and depth you would have after following the planned course. 

W__hat do you get if you multiply your final horizontal position by your final depth?

 

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

Don’t forget to submit your solutions at adventofcode.com**** and join the KX leaderboard using code 1526329-bb0915a5.

Today‘s problem solution uses projections to ingest the data, then a table to think through a solution to the second part. Finally we reduce the table solution to a simpler vector expression.

This post contains solutions!

The text file consists of course adjustments that affect horizontal position and depth.

forward 5 down 5 forward 8 up 3 down 8 forward 2

Part 1

Take the starting position and depth as 0 0.

q)forward:10*;down:01*;up:0-1* q)showc:valueeachread0`:test2.txt5005800-30820

The final position and depth are simply the sum of c and the answer to part 1 is their product.

q)prdsumc150

Part 2

Part 2 complicates the picture. The first column of c still describes forward movements. But we now need to calculate ‘aim’. Up and Down now adjust aim. Depth changes by the product of forward motion and aim.

A table can help us to think this through.

q)crs:{selectcmd:x,fwd,udfromflip`fwd`ud!flipvalueeachx}read0`:test2.txtq)updateaim:sumsudfrom`crs`crsq)updatedown:fwd*aimfrom`crs`crsq)showcrscmdfwdudaimdown---------------------------"forward 5"5000"down 5"0550"forward 8"80540"up 3"0-320"down 8"08100"forward 2"201020

Now we have the changes in horizontal and vertical position crs[fwddown] and can simply sum for the final position.

q)sumeachcrs[`fwd`down]1560

But the down column is no more than the product of the fwd column and the accumulated sums of the ud column. We can express the whole thing in terms of the fwd and udvectors.

q)`fwd:`udset'flipc / forward; up-downq)prdsumeach(fwd;fwd*sumsud)900

The repetition of fwd draws the eye. Isn’t (fwd;fwd*sums ud) just fwd multiplied by 1 and by sums ud?

q)prdsumfwd*1,'sumsud900

Or expressed as a function directly on the columns of c

q)prdsum{x*1,'sumsy}.flipc

That reduces our complete solution to

forward:10*;down:01*;up:0-1*c:valueeachread0`:day2.txta[`$"2-2"]:prdsumca[`$"2-2"]:prdsum{x*1,'sumsy}.flipc