# \--- AOC Day 3: Binary Diagnostic ---

**URL:** <https://forum.kx.com/t/aoc-day-3-binary-diagnostic/12293>\
**Category:** Community Support\
**Tags:** advent-of-code\
**Created:** [December 3, 2021, 9:11am UTC](https://forum.kx.com/t/aoc-day-3-binary-diagnostic/12293 "2021-12-03T09:11:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laura2](https://avatars.discourse-cdn.com/v4/letter/l/76d3ee/32.png) [@Laura2](https://forum.kx.com/u/Laura2)\
**Post date:** [December 3, 2021, 9:11am UTC](https://forum.kx.com/t/aoc-day-3-binary-diagnostic/12293/1 "2021-12-03T09:11:00Z")

</div>

The submarine has been making some&nbsp;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&nbsp;_power consumption_.

You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the&nbsp;_gamma rate_&nbsp;and the&nbsp;_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&nbsp;_most common bit in the corresponding position_&nbsp;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&nbsp;`0`&nbsp;bits and seven&nbsp;`1`&nbsp;bits. Since the most common bit is&nbsp;`1`, the first bit of the gamma rate is&nbsp;`1`.

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

The most common value of the third, fourth, and fifth bits are&nbsp;`1`,&nbsp;`1`, and&nbsp;`0`, respectively, and so the final three bits of the gamma rate are&nbsp;`110`.

So, the gamma rate is the binary number&nbsp;`10110`, or&nbsp;`22`&nbsp;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&nbsp;`01001`, or&nbsp;`9`&nbsp;in decimal. Multiplying the gamma rate (`22`) by the epsilon rate (`9`) produces the power consumption,&nbsp;`198`.

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

&nbsp;

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

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

---

<div class="post-metadata">

**Author:** ![sujoy1](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@sujoy1](https://forum.kx.com/u/sujoy1)\
**Post date:** [December 3, 2021, 10:19am UTC](https://forum.kx.com/t/aoc-day-3-binary-diagnostic/12293/2 "2021-12-03T10:19:00Z")

</div>

&nbsp;

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

 

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![sujoy1](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@sujoy1](https://forum.kx.com/u/sujoy1)\
**Post date:** [December 3, 2021, 11:17am UTC](https://forum.kx.com/t/aoc-day-3-binary-diagnostic/12293/3 "2021-12-03T11:17:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SJT1](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@SJT1](https://forum.kx.com/u/SJT1)\
**Post date:** [December 15, 2021, 3:57pm UTC](https://forum.kx.com/t/aoc-day-3-binary-diagnostic/12293/4 "2021-12-15T15:57:00Z")

</div>

## 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&nbsp;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 of`dg`. 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.&nbsp;

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

```
