From 848495b886f2a0737f761a2b72b7912ac039bc96 Mon Sep 17 00:00:00 2001 From: Tristan Date: Mon, 6 Dec 2021 23:37:37 +0100 Subject: [PATCH] Add part one of day 3 --- aoc21 | 2 + data/day3/data.txt | 1000 +++++++++++++++++++++++++++++++++++++++++ data/day3/example.txt | 12 + data/day3/puzzle.md | 76 ++++ src/Day3.php | 66 +++ tests/Day3Test.php | 24 + 6 files changed, 1180 insertions(+) create mode 100644 data/day3/data.txt create mode 100644 data/day3/example.txt create mode 100644 data/day3/puzzle.md create mode 100644 src/Day3.php create mode 100644 tests/Day3Test.php diff --git a/aoc21 b/aoc21 index d6dce5c..2d8fcb3 100755 --- a/aoc21 +++ b/aoc21 @@ -4,6 +4,7 @@ require __DIR__.'/vendor/autoload.php'; use AdventOfCode21\Day1; use AdventOfCode21\Day2; +use AdventOfCode21\Day3; use AdventOfCode21\Puzzle; use Symfony\Component\Console\Application; @@ -12,5 +13,6 @@ $application = new Application(); $application->add(new Puzzle()); $application->add(new Day1()); $application->add(new Day2()); +$application->add(new Day3()); $application->run(); diff --git a/data/day3/data.txt b/data/day3/data.txt new file mode 100644 index 0000000..f398604 --- /dev/null +++ b/data/day3/data.txt @@ -0,0 +1,1000 @@ +010111111011 +010010101110 +011001001100 +001000001010 +111100101000 +111010101100 +000111101111 +010011010011 +100010111011 +101011000111 +100111101010 +101101101101 +110010110110 +100110011100 +001110011000 +011000101010 +001100111101 +100011101111 +100111011001 +011100101101 +111101000111 +111000101011 +001001000101 +010110011000 +110100100001 +010010010011 +100100100100 +011011001000 +111101011101 +101011110011 +110011101101 +001001000100 +100111101110 +101101101010 +111110101000 +111011011001 +111110101101 +110101010100 +011100110000 +000010111110 +111011111111 +010111111110 +101001110101 +001111010100 +001111110001 +000010000000 +010001101000 +100001001111 +101111000010 +000011110001 +110111101110 +000000100111 +010100000111 +011111100001 +100011110001 +101000001111 +101010111001 +001101100100 +001100111001 +001000011010 +001001111100 +011001011001 +101011001010 +100101101101 +000101011110 +101100110111 +011000010110 +110101000110 +011010100101 +110011100110 +111101000010 +111110101011 +110101100101 +101110101010 +100110001011 +000110101100 +010100001001 +010011001110 +101010010101 +101010000101 +100100001011 +010010100101 +011000111111 +001010000001 +000001100111 +010101101111 +101110000110 +100101001001 +010000101001 +001101011001 +011101111110 +011110011100 +110100110111 +000101011010 +100000010011 +011100111000 +001110101000 +001001001000 +110101011101 +000011001111 +101000011010 +100011110010 +100100010111 +110001010100 +111010100110 +101001010100 +110100111101 +111000010111 +010001111000 +000101010110 +010100000100 +001000110010 +100111001100 +111001000100 +011100001011 +100110001010 +000010100111 +001010111110 +101100110001 +010100100001 +011001110010 +010011100101 +011111010110 +111000010010 +011110110101 +110101101100 +101010110110 +000111000001 +001111111101 +110011101010 +011000110010 +101110101011 +110100001100 +001000011000 +110110110010 +101111000111 +000111111001 +111100110100 +010010011010 +010010111010 +000001101000 +000101010101 +110111110101 +111001110110 +110011001100 +011110011001 +000010110101 +011011000111 +010101010110 +001011010100 +100001111001 +011000111010 +011100010000 +110001011010 +110010110111 +001101010101 +000110011000 +000010011110 +101100111001 +100011001110 +100010000000 +001111010001 +011100010011 +010001000010 +110010010111 +011011111101 +010011010100 +100001101001 +101100110110 +000000101011 +111000110111 +111101010110 +111110000000 +101010000111 +011010110110 +100110100111 +001011001011 +110011111000 +010111100110 +001100101010 +110001001000 +000100000111 +000001011100 +000100000000 +100010100000 +110001011011 +110110111101 +100110010001 +001101001110 +011111011000 +110010000100 +100011010110 +011001100000 +010101001110 +111100000001 +110100010111 +000001000011 +101100010011 +000100011111 +111000001001 +101100011100 +010110010001 +000110000010 +110000101111 +011010011100 +111000011110 +100110110001 +100000011100 +101010001011 +010111001100 +100111110111 +111100001011 +010100100100 +100111100101 +111100000111 +101001001101 +111001111000 +001110010100 +111010011001 +010100111111 +110111010011 +100100000001 +001101111111 +111101001100 +000111101010 +000100101001 +111011011101 +010001110111 +111010100001 +001101010100 +101010011100 +000011100100 +100101101010 +011111100000 +011011001110 +000101000111 +010010111111 +001100111011 +000001101001 +001110110001 +010011011110 +001011011000 +100000111010 +001000100101 +010001100011 +011001000111 +010110101010 +101000001001 +110101011000 +001100000101 +100110011001 +000011001001 +100110111001 +111101101001 +011011001001 +010001100111 +001100011100 +011111101111 +100100111010 +101111110011 +110111001110 +000111100000 +011110000101 +111110100010 +001011001101 +110100101011 +011100001101 +111001111100 +111110100110 +000010010100 +010111110010 +001110001101 +011111010011 +110110111010 +101001110000 +011110110011 +000111011000 +010111000110 +001011100010 +001110000010 +010111000011 +010011111110 +011000010000 +101111011110 +101010000110 +011100011101 +011101101100 +100110101001 +000000011001 +100101010010 +110011010101 +010100100111 +011101011100 +011001111001 +000001001000 +001000101011 +010110111101 +100110010010 +010010010110 +000000111001 +011111110101 +011111001000 +100100011111 +000001111000 +111101010010 +101010100100 +011010111101 +000001011011 +101111100001 +100011000000 +110011001010 +000100010111 +100000010000 +101111010110 +010010010001 +101011100001 +101001011100 +001111000011 +111000100010 +001101110110 +001110111011 +100001110010 +000001110001 +111100001000 +100010011101 +000011000110 +111100100110 +000111110111 +000100111001 +011001110001 +111101010001 +110110101100 +010001011001 +001101111110 +000101111010 +110100100110 +111011001110 +110011010100 +010000100011 +011000111001 +110001110001 +101000000001 +101011000101 +011010000110 +000010111000 +010011100011 +111101101110 +001111010010 +100011011100 +111110111010 +000010110010 +101000011100 +010011101011 +110001011000 +000000010011 +001101010010 +110111111011 +111101011100 +111001010011 +110111100101 +111000111001 +001110010010 +001011101101 +110100100111 +101011100000 +011000100101 +110101010010 +000110000111 +111111001000 +000011100110 +101101110011 +001110011111 +110000101110 +110011100101 +101110100101 +001000111000 +101111000011 +100111111011 +011010101111 +101101001000 +000101101110 +111101000011 +000011000010 +000101010010 +101001011101 +110011010011 +111010010001 +110100010001 +010100001010 +110111101010 +100111000101 +101001100010 +011010011011 +101011101101 +011001100001 +001000110111 +000100101000 +010000000111 +111111110000 +001011110110 +100010010001 +100110101111 +110110010000 +100110011011 +001100110101 +100011101100 +000011011001 +010110100001 +011110101111 +101011011000 +101110111001 +111000010000 +011100000010 +100110100101 +010101011111 +100000111001 +011000111100 +101100100111 +101011111000 +111101111101 +101000100011 +101000010101 +111001010010 +101110000111 +101010000001 +000100100111 +100001011101 +110010101010 +000000100100 +011101000101 +110010001101 +111011101110 +111111111110 +011100000111 +101111110010 +000101100110 +000110001011 +100100011000 +010001101111 +011110010000 +100011011111 +010101101010 +000011100000 +000101001110 +001001110001 +111110011001 +101110000000 +010001010010 +110001000110 +101011001110 +010001101001 +000000011011 +101001010011 +011010000100 +001100001100 +100010001001 +100000111111 +001000000000 +100110110100 +100111110000 +101001110001 +000110101101 +000111001010 +101100011011 +110001101000 +000111101110 +110110010011 +100010001000 +001010001100 +001011111110 +001110000111 +110010100000 +100001110101 +000110111101 +110010011000 +111110111011 +101000000011 +011010011110 +110000001111 +011000110100 +111111001101 +000010110100 +101100001100 +110000101011 +010000001110 +101001101000 +011000000100 +101101011000 +100111111100 +110001001010 +001010101100 +111010001110 +101110100110 +011110010001 +111011111010 +000001100110 +010001100101 +100101001111 +111001001011 +111101101010 +011011000001 +010111010101 +110001111110 +110100101110 +111101011011 +011000101011 +010100111100 +011000000111 +101000101001 +111000110001 +101000110001 +001111010011 +100000010100 +001001101101 +010101110001 +100110000101 +011000101101 +001110110010 +100111000001 +011001000001 +000101101000 +101000101110 +000100001110 +111110101111 +011110110001 +101001110010 +000000111010 +101111010010 +011110011010 +101000011111 +000100001100 +110001111001 +110000110101 +111010011100 +001000111011 +111110001111 +011011011000 +000110110001 +101001010010 +111010100011 +000001111100 +110010000001 +110101111011 +100011001001 +010011100001 +101101011111 +100000001010 +001011001000 +000011100010 +011110111000 +000110111011 +010111001011 +011100011000 +101010011011 +001001011110 +000110001010 +111011010101 +010101110010 +011100101001 +100000110010 +111100111001 +001011011101 +110101101001 +001000111110 +101011000011 +001110111111 +100101010110 +111011011110 +010111111001 +011001010001 +110101111111 +110100100101 +111111110001 +010011000101 +011101101010 +101111000100 +011101000100 +001110110110 +010011011000 +000011011010 +110000000110 +001100001101 +111001111001 +100001100101 +100100010011 +111011111000 +001001111110 +101101000000 +000001100001 +001100001011 +110011011111 +010101111110 +001110011100 +111111000100 +010011000010 +101100010000 +000010110111 +110100000101 +110100010010 +111001100101 +010000010001 +101100100110 +001111101100 +011100110110 +000000111101 +001000000100 +010101101101 +100101101000 +001001010101 +000000001111 +110111110100 +000010110110 +001000000010 +011000101000 +100101001101 +010110101110 +100010110001 +010111010000 +001111011001 +000011110100 +100100111000 +011011100110 +100100010110 +000001111101 +010111011011 +110001000101 +111100010001 +101001000111 +110100010000 +001010000111 +101001110111 +010010010010 +100010111100 +001101000011 +011001110011 +000100101111 +010110011101 +001001011010 +111011101001 +111111000000 +111110000111 +110000001011 +010000110010 +011100011001 +011001100011 +001101000110 +011001011111 +100111100000 +110111010100 +101101000001 +110011101000 +001010110101 +100101010001 +101000101100 +010110001000 +111011111011 +000010010001 +010111011010 +011101101011 +001000101110 +101010100111 +001011000111 +011011010110 +110111101111 +100001110001 +100100101111 +110000010011 +011110111011 +000110000100 +110000111111 +100110101100 +001101110101 +011100000100 +001100011110 +010111010010 +001110101101 +100110001110 +100100010000 +001000001101 +011101110011 +011001100110 +110101000011 +100101001010 +111111101111 +010011000100 +100111000111 +101100101100 +010110000100 +111100000100 +000000101001 +000011110010 +011011110111 +011101110001 +111110000001 +011000011001 +100001101111 +110000011010 +110110101010 +110100110000 +111111000011 +100011100010 +100100010010 +110100011000 +000110000110 +001101001000 +101011110111 +000011110111 +001001100100 +100010110011 +100111111000 +000001111010 +110001111101 +100011111111 +111001001101 +011111101011 +101111110111 +100111000011 +010010001010 +010101101011 +011011110000 +110111000000 +010011101111 +010001010101 +111110101010 +001010101010 +001100101101 +111011000111 +101001000001 +000101000100 +010011110010 +001110100010 +110111001000 +111011001101 +111110001010 +110000100001 +000000010101 +000010011001 +011100100111 +001101111010 +000001100100 +011111101100 +110100011111 +010110000000 +001010101001 +000010010011 +111100000110 +110101000101 +010110001011 +111001001000 +010110101111 +001000000001 +111100001101 +001000110110 +110001101001 +001110001011 +111101101011 +011101111100 +100011001011 +001000001001 +000001001100 +100100110111 +111111101101 +001110100000 +001001001001 +001111001100 +101100101111 +101100110010 +101000100001 +101000010110 +100000010101 +011010011000 +101101101110 +011101001100 +101011111100 +101110101000 +110010100100 +011111011110 +110010011101 +110100100011 +111111100100 +011011011101 +011100100001 +101011110000 +001100100011 +000111111000 +110010001010 +100001001001 +110010001000 +111010001001 +010110111111 +101011001101 +101010011110 +011011110001 +101010100011 +100000110111 +100011101001 +101110010011 +101011011011 +110010010000 +011110100011 +010010110110 +100110010111 +110100000110 +101011101011 +001100010100 +101101010000 +110110001001 +010010001101 +111011010110 +111100001110 +000101111001 +100001000001 +101111101001 +100101101110 +000011001101 +000001000110 +111110001011 +000010101111 +011101101101 +000000000110 +111111101010 +101001110110 +111011000100 +001011101111 +110010111010 +110011110001 +111100011111 +110110010001 +100101110000 +010001001011 +101100010100 +110100001010 +001011101001 +110110000001 +001101001001 +100000111100 +001101101010 +010000110011 +011111101110 +110001110011 +011110011000 +000010001010 +001110000100 +000110101111 +100011111000 +100101100010 +111111110010 +101100011001 +011100100000 +100001100011 +001010011110 +111100011010 +010110100010 +101110010010 +001011110010 +100010111000 +001100110010 +001001101001 +011111101101 +101100111100 +011010011101 +101011100100 +111000001000 +010111011100 +001010000000 +011110000110 +110101101110 +000111000101 +101001011010 +110010100010 +100101110010 +000101100100 +010100110010 +011001000010 +110010011010 +010100001011 +011001111110 +011000101001 +011101011011 +010010000111 +000001110111 +100010101001 +111100011101 +100010001011 +111100101100 +011111010101 +111100101110 +111101001110 +111000010110 +000000110110 +011110000001 +000111011110 +111010111010 +101010001101 +111110101100 +000111110010 +110000111101 +111101110101 +101010101100 +101110110010 +110110011010 +111110110001 +011101111011 +101011111011 +100111101101 +000110011001 +100101101011 +110101101101 +010111110100 +001111110010 +101010101000 +011001100100 +101011111111 +001001000001 +010100100110 +111100011100 +011100011010 +000011011110 +001011101010 +010010001110 +101101101111 +000101000010 +010111100010 +010010010000 +111000000101 +010001100010 +101100001111 +011110111100 +100011000010 +010100001111 +011100101110 +011000111110 +111101010000 +110100111000 +001010001111 +111100011011 +101000111110 +100000011011 +010110000001 +001111100111 +001111101111 +011001011100 +111001000011 +110011101111 +001101100011 +101100111111 +011111101010 +010000011110 +001111111111 +010001001001 +111010101000 +110100001001 +111111010111 +000101110100 +101101100000 +001101100001 +001111111000 +100111101001 +000110010001 +100011101011 +000010000101 +101010001100 +000111110011 +001010110001 +111110001110 +010011100010 +100011010000 +100001010010 +100011000101 +110011000100 +010000010000 +110100110011 +100011000111 +010111101010 +101011000010 +110000101100 +111100001111 +000110100001 +010010110001 +111000100110 +000110111110 +100101000000 +011101001010 +101111011011 +111011101111 +000000010100 +110011101110 +111001110111 +011111000111 +111111011100 +110010011111 +111111110111 +001110110011 +000010010010 +100100100001 +111010000101 +001001110011 +101001101111 +101110101110 +101000101111 diff --git a/data/day3/example.txt b/data/day3/example.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/data/day3/example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/data/day3/puzzle.md b/data/day3/puzzle.md new file mode 100644 index 0000000..e641ab6 --- /dev/null +++ b/data/day3/puzzle.md @@ -0,0 +1,76 @@ +# Day 3: Binary Diagnostic + +[https://adventofcode.com/2021/day/3](https://adventofcode.com/2021/day/3) + +## Description + +### Part One + +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 `1`, `1`, 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.) + +### Part Two + +Next, you should verify the _life support rating_, which can be determined by multiplying the _oxygen generator rating_ by the _CO2 scrubber rating_. + +Both the oxygen generator rating and the CO2 scrubber rating are values that can be found in your diagnostic report - finding them is the tricky part. Both values are located using a similar process that involves filtering out values until only one remains. Before searching for either rating value, start with the full list of binary numbers from your diagnostic report and _consider just the first bit_ of those numbers. Then: + +* Keep only numbers selected by the _bit criteria_ for the type of rating value for which you are searching. Discard numbers which do not match the bit criteria. +* If you only have one number left, stop; this is the rating value for which you are searching. +* Otherwise, repeat the process, considering the next bit to the right. + +The _bit criteria_ depends on which type of rating value you want to find: + +* To find _oxygen generator rating_, determine the _most common_ value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a _`1`_ in the position being considered. +* To find _CO2 scrubber rating_, determine the _least common_ value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a _`0`_ in the position being considered. + +For example, to determine the _oxygen generator rating_ value using the same example diagnostic report from above: + +* Start with all 12 numbers and consider only the first bit of each number. There are more `1` bits (7) than `0` bits (5), so keep only the 7 numbers with a `1` in the first position: `11110`, `10110`, `10111`, `10101`, `11100`, `10000`, and `11001`. +* Then, consider the second bit of the 7 remaining numbers: there are more `0` bits (4) than `1` bits (3), so keep only the 4 numbers with a `0` in the second position: `10110`, `10111`, `10101`, and `10000`. +* In the third position, three of the four numbers have a `1`, so keep those three: `10110`, `10111`, and `10101`. +* In the fourth position, two of the three numbers have a `1`, so keep those two: `10110` and `10111`. +* In the fifth position, there are an equal number of `0` bits and `1` bits (one each). So, to find the _oxygen generator rating_, keep the number with a `1` in that position: `10111`. +* As there is only one number left, stop; the _oxygen generator rating_ is `10111`, or _`23`_ in decimal. + +Then, to determine the _CO2 scrubber rating_ value from the same example above: + +* Start again with all 12 numbers and consider only the first bit of each number. There are fewer `0` bits (5) than `1` bits (7), so keep only the 5 numbers with a `0` in the first position: `00100`, `01111`, `00111`, `00010`, and `01010`. +* Then, consider the second bit of the 5 remaining numbers: there are fewer `1` bits (2) than `0` bits (3), so keep only the 2 numbers with a `1` in the second position: `01111` and `01010`. +* In the third position, there are an equal number of `0` bits and `1` bits (one each). So, to find the _CO2 scrubber rating_, keep the number with a `0` in that position: `01010`. +* As there is only one number left, stop; the _CO2 scrubber rating_ is `01010`, or _`10`_ in decimal. + +Finally, to find the life support rating, multiply the oxygen generator rating (`23`) by the CO2 scrubber rating (`10`) to get _`230`_. + +Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. _What is the life support rating of the submarine?_ (Be sure to represent your answer in decimal, not binary.) diff --git a/src/Day3.php b/src/Day3.php new file mode 100644 index 0000000..d1ea0de --- /dev/null +++ b/src/Day3.php @@ -0,0 +1,66 @@ + $value) { + $bits[$position][] = $value; + } + } + + $gammaRate = ''; + $epsilonRate = ''; + + foreach ($bits as $values) { + $zeros = array_filter($values, static fn ($value) => $value === '0'); + $ones = array_filter($values, static fn ($value) => $value === '1'); + + $gammaRate .= ($ones > $zeros) ? '1' : '0'; + $epsilonRate .= ($ones < $zeros) ? '1' : '0'; + } + + return (int) (bindec($gammaRate) * bindec($epsilonRate)); + } + + /** + * @param string[] $data + * @param int $position + * @param bool $highest + * + * @return string + * + * @psalm-return '0'|'1' + */ + private function valueAtPosition(array $data, int $position, bool $highest = true): string + { + $zeros = count(array_filter($data, static fn ($value) => $value[$position] === '0')); + $ones = count(array_filter($data, static fn ($value) => $value[$position] === '1')); + + if ($highest) { + return $zeros > $ones ? '0' : '1'; + } + + if ($zeros === $ones) { + return '0'; + } + + // If there are more zeros, return 1 (as that is the lowest). + return $zeros > $ones ? '1' : '0'; + } +} diff --git a/tests/Day3Test.php b/tests/Day3Test.php new file mode 100644 index 0000000..203beea --- /dev/null +++ b/tests/Day3Test.php @@ -0,0 +1,24 @@ +