--- AOC Day 3: Binary Diagnostic ---

The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case.

The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the power consumption.

You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.

Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers in the diagnostic report. For example, given the following diagnostic report:

00100 11110 10110 10111 10101 01111 00111 11100 10000 11001 00010 01010

Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.

The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.

The most common value of the third, fourth, and fifth bits are 11, and 0, respectively, and so the final three bits of the gamma rate are 110.

So, the gamma rate is the binary number 10110, or 22 in decimal.

The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198.

Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.)

 

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.

 

prd (2 sv’)“J”$string (“01”;“10”)@:{(>).(sum’)“10”=:x} each flip read0`:aoc3.txt

 

 

prd {(2 sv’)“I”$string{?[1~count x;x;x where x[;y]=z(>=).(sum’)“10”=:(flip x)[y]]}[;;y]/ [x;til count first x]}[l;] each (“01”;“10”)

Part 2

Day 3: Binary Diagnostic

This post contains solutions!

Ingestion here is a treat. Comparing the file lines to "1" gets us a boolean matrix.

q)showdg:"1"=read0`:day3.txt / diagnostic110011110101b110011100010b010100011010b011001100000b010011011101b..

Part 1

Finding the most common bit is just comparing the the sum of dg to half its count.

q)sum[dg]>=count[dg]%2 / gamma bits001110101101b

And the least-common bit is a slight variation.

q)sum[dg]<count[dg]%2 / epsilon bits110001010010b

which suggests finding both with a projection.

q)(>=;<){.[x](sumy;count[y]%2)}\:dg001110101101b110001010010b

Above, a comparison operator is passed to the lambda as its left argument. Within the lambda, the Apply operator . uses x to compare sum y and count[y]%2. But we can do that a little more neatly.

{.[x]1 .5*(sum;count)@\:y}

The Each Left map iterator applies the lambda between the two comparison operators and the diagnostic matrix.

All that remains is to encode these two binary numbers as decimals and get their product. The complete solution:

q)prd2sv'(>=;<){.[x]1 .5*(sum;count)@\:y}\:"1"=read0`:day3.txt2967914

Part 2

To find the oxygen generator rating we need to filter the rows of dg by an aggregation (most-common bit) of its first column, and so on. Start with a filter based on the aggregator we already defined.

fltr:{where y=.[x]1 .5*(sum;count)@\:y)}

This finds which items of its right argument match the most-common or least-common bit according its left argument.

q)fltr[\>=]101010b  
0 2 4

As another way of working across the columns of dg we’ll flip it and iterate through the items (rows). As each iteration will use the result of the previous iteration, we need an Accumulator iterator. We’ll use the Scan iterator to filter the rows of flip dg until we find the next iteration would leave no rows. Initially, all the rows are ‘in’: til count dg.

(tilcountdg){$[counti:fltr[>=] y x;x i;x]}/flipdg

Here we can see the structure of the iteration. The initial state til count dg is a list of all the rows ofdg. The lambda being iterated tests the first column of dg and finds which rows pass the test. Only the rows listed in the left argument are tested, so eliminated rows stay eliminated. We stop just before we eliminate the last row/s. 

We can use the Scan iterator to watch the row indexes being filtered.

q)(tilcountdg) {$[counti:fltr[>=]y x;x i;x]}\flip"1"=read0`:test3.txt1234789234823423,3q)dg310111b

That is the oxygen generator rating for the test data. Switching the comparison operator will give us the CO2 scrubber rating. So to be tidy, let’s name the lambda and make the comparison operator its third argument.

q)dg:"1"=read0`:test3.txt
q)analyze:{$[counti:fltr[z]y x;x i;x]}q)(til count dg) analyze[;;\>=]\ flip dg1 2 3 4 7 8 9  
234823423,3q)(tilcountdg) analyze[;;<]\flip dg0561011511,11,11,11q)dg1101010b

So the rating (O2 or CO2) is a function of the diagnostic matrix and a comparison operator. Let’s write it that way.

rating:{first(til count y)analyze[;;x]\flip y}

And there’s the CO2 scrubber rating for the test data. On to the puzzle data.

q)fltr:{where y=.[x]1 .5*(sum;count)@\:y)}
q)analyze:{$[counti:fltr[z]y x;x i;x]} q)rating:{first(til count y)analyze[;;x]\flip y}
q)prd 2 sv'dg(>=;<)rating\:dg:"1"=read0`:day3.txt7041258