Compare commits
72 Commits
a5229f5209
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 29be797deb | |||
| 52de1b4d9e | |||
| 2681d92bee | |||
| 3f5437e383 | |||
| 9576bc490f | |||
| 0ea4309c3e | |||
| 732c455dfe | |||
| 310668d4a6 | |||
| aae16d3c46 | |||
| c80d4e7ac5 | |||
| a81d7b914c | |||
| c02a7c3934 | |||
| 71ebdf8f65 | |||
| 959b52cda2 | |||
| 7def74b81e | |||
| 0050e6dd76 | |||
| 7e1e023caa | |||
| a7363a8fa3 | |||
| ca9e753968 | |||
| 014d2947fe | |||
| 66e344a5e5 | |||
| f137feefe4 | |||
| 3de570f27f | |||
| a6a56cb319 | |||
| 26e385cdd2 | |||
| 73681dd50b | |||
| a81a510d6b | |||
| 9b8178438a | |||
| 6a57417826 | |||
| e022a7db44 | |||
| 90f2e88b1f | |||
| 14855dcc2d | |||
| 54b83966ed | |||
| cadeaf1bba | |||
| ce0b73fce1 | |||
| 86665d264a | |||
| d16807f80d | |||
| c365cc5b64 | |||
| cbd7e79917 | |||
| 7da0981cc5 | |||
| 1255dab24f | |||
| a3becde292 | |||
| d353c3555e | |||
| a860842fcf | |||
| 6136c92fd6 | |||
| 9aaf38ea2c | |||
| 48a72554bb | |||
| 77b158aacf | |||
| 56ee86026d | |||
| ae2666fb02 | |||
| 739cb2a318 | |||
| 03633c432f | |||
| 87e8ab9c6a | |||
| 0b08b34c9a | |||
| 824f4eff67 | |||
| 5b693a6ac1 | |||
| d2d338ab16 | |||
| 801e4747a2 | |||
| f1a4989196 | |||
| e0a1022a55 | |||
| 932ec446b2 | |||
| fc9e44a73e | |||
| a70226032b | |||
| 5995599d6d | |||
| 0842b5d196 | |||
| ae8d3f0d97 | |||
| cceb733667 | |||
| 577725fd54 | |||
| 7928680a02 | |||
| d45e0ef80e | |||
| 2642aa39f1 | |||
| 0473bf6da2 |
@@ -5,6 +5,14 @@ Personal stats
|
||||
|
||||
--------Part 1-------- --------Part 2--------
|
||||
Day Time Rank Score Time Rank Score
|
||||
16 01:03:24 1899 0 02:53:04 4403 0
|
||||
15 11:35:53 19304 0 12:02:27 15520 0
|
||||
14 00:49:36 7350 0 00:50:14 2214 0
|
||||
13 00:30:05 3269 0 00:37:02 2884 0
|
||||
12 06:56:08 16817 0 16:08:35 26326 0
|
||||
11 04:38:59 13748 0 04:42:36 13469 0
|
||||
10 00:13:01 2516 0 00:28:59 3447 0
|
||||
9 00:21:43 5101 0 13:07:23 29623 0
|
||||
8 00:17:48 5001 0 00:51:53 1540 0
|
||||
7 02:50:14 21325 0 03:04:41 20312 0
|
||||
6 00:11:27 3758 0 00:27:45 3202 0
|
||||
|
||||
91
day-01/README.md
Normal file
91
day-01/README.md
Normal file
@@ -0,0 +1,91 @@
|
||||
# Day 1: Sonar Sweep
|
||||
|
||||
[https://adventofcode.com/2021/day/1](https://adventofcode.com/2021/day/1)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
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:
|
||||
|
||||
199
|
||||
200
|
||||
208
|
||||
210
|
||||
200
|
||||
207
|
||||
240
|
||||
269
|
||||
260
|
||||
263
|
||||
|
||||
|
||||
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 <span title="Does this premise seem fishy to you?">carried into deeper water</span> 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?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Considering every single measurement isn't as useful as you expected: there's just too much noise in the data.
|
||||
|
||||
Instead, consider sums of a _three-measurement sliding window_. Again considering the above example:
|
||||
|
||||
199 A
|
||||
200 A B
|
||||
208 A B C
|
||||
210 B C D
|
||||
200 E C D
|
||||
207 E F D
|
||||
240 E F G
|
||||
269 F G H
|
||||
260 G H
|
||||
263 H
|
||||
|
||||
|
||||
Start by comparing the first and second three-measurement windows. The measurements in the first window are marked `A` (`199`, `200`, `208`); their sum is `199 + 200 + 208 = 607`. The second window is marked `B` (`200`, `208`, `210`); its sum is `618`. The sum of measurements in the second window is larger than the sum of the first, so this first comparison _increased_.
|
||||
|
||||
Your goal now is to count _the number of times the sum of measurements in this sliding window increases_ from the previous sum. So, compare `A` with `B`, then compare `B` with `C`, then `C` with `D`, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum.
|
||||
|
||||
In the above example, the sum of each three-measurement window is as follows:
|
||||
|
||||
A: 607 (N/A - no previous sum)
|
||||
B: 618 (increased)
|
||||
C: 618 (no change)
|
||||
D: 617 (decreased)
|
||||
E: 647 (increased)
|
||||
F: 716 (increased)
|
||||
G: 769 (increased)
|
||||
H: 792 (increased)
|
||||
|
||||
|
||||
In this example, there are _`5`_ sums that are larger than the previous sum.
|
||||
|
||||
Consider sums of a three-measurement sliding window. _How many sums are larger than the previous sum?_
|
||||
67
day-02/README.md
Normal file
67
day-02/README.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# Day 2: Dive!
|
||||
|
||||
[https://adventofcode.com/2021/day/2](https://adventofcode.com/2021/day/2)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
Now, you need to figure out how to <span title="Tank, I need a pilot program for a B212 helicopter.">pilot this thing</span>.
|
||||
|
||||
It seems like the submarine can take a series of commands like `forward 1`, `down 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. _What do you get if you multiply your final horizontal position by your final depth?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.
|
||||
|
||||
In addition to horizontal position and depth, you'll also need to track a third value, _aim_, which also starts at `0`. The commands also mean something entirely different than you first thought:
|
||||
|
||||
* `down X` _increases_ your aim by `X` units.
|
||||
* `up X` _decreases_ your aim by `X` units.
|
||||
* `forward X` does two things:
|
||||
* It increases your horizontal position by `X` units.
|
||||
* It increases your depth by your aim _multiplied by_ `X`.
|
||||
|
||||
Again note that since you're on a submarine, `down` and `up` do the opposite of what you might expect: "down" means aiming in the positive direction.
|
||||
|
||||
Now, the above example does something different:
|
||||
|
||||
* `forward 5` adds `5` to your horizontal position, a total of `5`. Because your aim is `0`, your depth does not change.
|
||||
* `down 5` adds `5` to your aim, resulting in a value of `5`.
|
||||
* `forward 8` adds `8` to your horizontal position, a total of `13`. Because your aim is `5`, your depth increases by `8*5=40`.
|
||||
* `up 3` decreases your aim by `3`, resulting in a value of `2`.
|
||||
* `down 8` adds `8` to your aim, resulting in a value of `10`.
|
||||
* `forward 2` adds `2` to your horizontal position, a total of `15`. Because your aim is `10`, your depth increases by `2*10=20` to a total of `60`.
|
||||
|
||||
After following these new instructions, you would have a horizontal position of `15` and a depth of `60`. (Multiplying these produces _`900`_.)
|
||||
|
||||
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. _What do you get if you multiply your final horizontal position by your final depth?_
|
||||
76
day-03/README.md
Normal file
76
day-03/README.md
Normal file
@@ -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 <span title="Turns out oceans are heavy.">odd creaking noises</span>, 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.)
|
||||
79
day-04/README.md
Normal file
79
day-04/README.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# Day 4: Giant Squid
|
||||
|
||||
[https://adventofcode.com/2021/day/4](https://adventofcode.com/2021/day/4)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You're already almost 1.5km (almost a mile) below the surface of the ocean, already so deep that you can't see any sunlight. What you _can_ see, however, is a giant squid that has attached itself to the outside of your submarine.
|
||||
|
||||
Maybe it wants to play [bingo](https://en.wikipedia.org/wiki/Bingo_(American_version))?
|
||||
|
||||
Bingo is played on a set of boards each consisting of a 5x5 grid of numbers. Numbers are chosen at random, and the chosen number is _marked_ on all boards on which it appears. (Numbers may not appear on all boards.) If all numbers in any row or any column of a board are marked, that board _wins_. (Diagonals don't count.)
|
||||
|
||||
The submarine has a _bingo subsystem_ to help passengers (currently, you and the giant squid) pass the time. It automatically generates a random order in which to draw numbers and a random set of boards (your puzzle input). For example:
|
||||
|
||||
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
||||
|
||||
22 13 17 11 0
|
||||
8 2 23 4 24
|
||||
21 9 14 16 7
|
||||
6 10 3 18 5
|
||||
1 12 20 15 19
|
||||
|
||||
3 15 0 2 22
|
||||
9 18 13 17 5
|
||||
19 8 7 25 23
|
||||
20 11 10 24 4
|
||||
14 21 16 12 6
|
||||
|
||||
14 21 17 24 4
|
||||
10 16 15 9 19
|
||||
18 8 23 26 20
|
||||
22 11 13 6 5
|
||||
2 0 12 3 7
|
||||
|
||||
|
||||
After the first five numbers are drawn (`7`, `4`, `9`, `5`, and `11`), there are no winners, but the boards are marked as follows (shown here adjacent to each other to save space):
|
||||
|
||||
22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
|
||||
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
|
||||
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
|
||||
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
|
||||
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
|
||||
|
||||
|
||||
After the next six numbers are drawn (`17`, `23`, `2`, `0`, `14`, and `21`), there are still no winners:
|
||||
|
||||
22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
|
||||
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
|
||||
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
|
||||
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
|
||||
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
|
||||
|
||||
|
||||
Finally, `24` is drawn:
|
||||
|
||||
22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
|
||||
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
|
||||
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
|
||||
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
|
||||
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
|
||||
|
||||
|
||||
At this point, the third board _wins_ because it has at least one complete row or column of marked numbers (in this case, the entire top row is marked: _`14 21 17 24 4`_).
|
||||
|
||||
The _score_ of the winning board can now be calculated. Start by finding the _sum of all unmarked numbers_ on that board; in this case, the sum is `188`. Then, multiply that sum by _the number that was just called_ when the board won, `24`, to get the final score, `188 * 24 = 4512`.
|
||||
|
||||
To guarantee victory against the giant squid, figure out which board will win first. _What will your final score be if you choose that board?_
|
||||
|
||||
### Part Two
|
||||
|
||||
On the other hand, it might be wise to try a different strategy: <span title="That's 'cuz a submarine don't pull things' antennas out of their sockets when they lose. Giant squid are known to do that.">let the giant squid win</span>.
|
||||
|
||||
You aren't sure how many bingo boards a giant squid could play at once, so rather than waste time counting its arms, the safe thing to do is to _figure out which board will win last_ and choose that one. That way, no matter which boards it picks, it will win for sure.
|
||||
|
||||
In the above example, the second board is the last to win, which happens after `13` is eventually called and its middle column is completely marked. If you were to keep playing until this point, the second board would have a sum of unmarked numbers equal to `148` for a final score of `148 * 13 = 1924`.
|
||||
|
||||
Figure out which board will win last. _Once it wins, what would its final score be?_
|
||||
77
day-05/README.md
Normal file
77
day-05/README.md
Normal file
@@ -0,0 +1,77 @@
|
||||
# Day 5: Hydrothermal Venture
|
||||
|
||||
[https://adventofcode.com/2021/day/5](https://adventofcode.com/2021/day/5)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You come across a field of [hydrothermal vents](https://en.wikipedia.org/wiki/Hydrothermal_vent) on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.
|
||||
|
||||
They tend to form in _lines_; the submarine helpfully produces a list of nearby <span title="Maybe they're Bresenham vents.">lines of vents</span> (your puzzle input) for you to review. For example:
|
||||
|
||||
0,9 -> 5,9
|
||||
8,0 -> 0,8
|
||||
9,4 -> 3,4
|
||||
2,2 -> 2,1
|
||||
7,0 -> 7,4
|
||||
6,4 -> 2,0
|
||||
0,9 -> 2,9
|
||||
3,4 -> 1,4
|
||||
0,0 -> 8,8
|
||||
5,5 -> 8,2
|
||||
|
||||
|
||||
Each line of vents is given as a line segment in the format `x1,y1 -> x2,y2` where `x1`,`y1` are the coordinates of one end the line segment and `x2`,`y2` are the coordinates of the other end. These line segments include the points at both ends. In other words:
|
||||
|
||||
* An entry like `1,1 -> 1,3` covers points `1,1`, `1,2`, and `1,3`.
|
||||
* An entry like `9,7 -> 7,7` covers points `9,7`, `8,7`, and `7,7`.
|
||||
|
||||
For now, _only consider horizontal and vertical lines_: lines where either `x1 = x2` or `y1 = y2`.
|
||||
|
||||
So, the horizontal and vertical lines from the above list would produce the following diagram:
|
||||
|
||||
.......1..
|
||||
..1....1..
|
||||
..1....1..
|
||||
.......1..
|
||||
.112111211
|
||||
..........
|
||||
..........
|
||||
..........
|
||||
..........
|
||||
222111....
|
||||
|
||||
|
||||
In this diagram, the top left corner is `0,0` and the bottom right corner is `9,9`. Each position is shown as _the number of lines which cover that point_ or `.` if no line covers that point. The top-left pair of `1`s, for example, comes from `2,2 -> 2,1`; the very bottom row is formed by the overlapping lines `0,9 -> 5,9` and `0,9 -> 2,9`.
|
||||
|
||||
To avoid the most dangerous areas, you need to determine _the number of points where at least two lines overlap_. In the above example, this is anywhere in the diagram with a `2` or larger - a total of _`5`_ points.
|
||||
|
||||
Consider only horizontal and vertical lines. _At how many points do at least two lines overlap?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider _diagonal lines_.
|
||||
|
||||
Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:
|
||||
|
||||
* An entry like `1,1 -> 3,3` covers points `1,1`, `2,2`, and `3,3`.
|
||||
* An entry like `9,7 -> 7,9` covers points `9,7`, `8,8`, and `7,9`.
|
||||
|
||||
Considering all lines from the above example would now produce the following diagram:
|
||||
|
||||
1.1....11.
|
||||
.111...2..
|
||||
..2.1.111.
|
||||
...1.2.2..
|
||||
.112313211
|
||||
...1.2....
|
||||
..1...1...
|
||||
.1.....1..
|
||||
1.......1.
|
||||
222111....
|
||||
|
||||
|
||||
You still need to determine _the number of points where at least two lines overlap_. In the above example, this is still anywhere in the diagram with a `2` or larger - now a total of _`12`_ points.
|
||||
|
||||
Consider all of the lines. _At how many points do at least two lines overlap?_
|
||||
68
day-06/README.md
Normal file
68
day-06/README.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# Day 6: Lanternfish
|
||||
|
||||
[https://adventofcode.com/2021/day/6](https://adventofcode.com/2021/day/6)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
The sea floor is getting steeper. Maybe the sleigh keys got carried this way?
|
||||
|
||||
A massive school of glowing [lanternfish](https://en.wikipedia.org/wiki/Lanternfish) swims past. They must spawn quickly to reach such large numbers - maybe _exponentially_ quickly? You should model their growth rate to be sure.
|
||||
|
||||
Although you know nothing about this specific species of lanternfish, you make some guesses about their attributes. Surely, <span title="I heard you like lanternfish.">each lanternfish creates a new lanternfish</span> once every _7_ days.
|
||||
|
||||
However, this process isn't necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents _the number of days until it creates a new lanternfish_.
|
||||
|
||||
Furthermore, you reason, a _new_ lanternfish would surely need slightly longer before it's capable of producing more lanternfish: two more days for its first cycle.
|
||||
|
||||
So, suppose you have a lanternfish with an internal timer value of `3`:
|
||||
|
||||
* After one day, its internal timer would become `2`.
|
||||
* After another day, its internal timer would become `1`.
|
||||
* After another day, its internal timer would become `0`.
|
||||
* After another day, its internal timer would reset to `6`, and it would create a _new_ lanternfish with an internal timer of `8`.
|
||||
* After another day, the first lanternfish would have an internal timer of `5`, and the second lanternfish would have an internal timer of `7`.
|
||||
|
||||
A lanternfish that creates a new fish resets its timer to `6`, _not `7`_ (because `0` is included as a valid timer value). The new lanternfish starts with an internal timer of `8` and does not start counting down until the next day.
|
||||
|
||||
Realizing what you're trying to do, the submarine automatically produces a list of the ages of several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the following list:
|
||||
|
||||
3,4,3,1,2
|
||||
|
||||
This list means that the first fish has an internal timer of `3`, the second fish has an internal timer of `4`, and so on until the fifth fish, which has an internal timer of `2`. Simulating these fish over several days would proceed as follows:
|
||||
|
||||
Initial state: 3,4,3,1,2
|
||||
After 1 day: 2,3,2,0,1
|
||||
After 2 days: 1,2,1,6,0,8
|
||||
After 3 days: 0,1,0,5,6,7,8
|
||||
After 4 days: 6,0,6,4,5,6,7,8,8
|
||||
After 5 days: 5,6,5,3,4,5,6,7,7,8
|
||||
After 6 days: 4,5,4,2,3,4,5,6,6,7
|
||||
After 7 days: 3,4,3,1,2,3,4,5,5,6
|
||||
After 8 days: 2,3,2,0,1,2,3,4,4,5
|
||||
After 9 days: 1,2,1,6,0,1,2,3,3,4,8
|
||||
After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8
|
||||
After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8
|
||||
After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8
|
||||
After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8
|
||||
After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8
|
||||
After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7
|
||||
After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8
|
||||
After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8
|
||||
After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8
|
||||
|
||||
|
||||
Each day, a `0` becomes a `6` and adds a new `8` to the end of the list, while each other number decreases by 1 if it was present at the start of the day.
|
||||
|
||||
In this example, after 18 days, there are a total of `26` fish. After 80 days, there would be a total of _`5934`_.
|
||||
|
||||
Find a way to simulate lanternfish. _How many lanternfish would there be after 80 days?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean?
|
||||
|
||||
After 256 days in the example above, there would be a total of _`26984457539`_ lanternfish!
|
||||
|
||||
_How many lanternfish would there be after 256 days?_
|
||||
63
day-07/README.md
Normal file
63
day-07/README.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Day 7: The Treachery of Whales
|
||||
|
||||
[https://adventofcode.com/2021/day/7](https://adventofcode.com/2021/day/7)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
A giant [whale](https://en.wikipedia.org/wiki/Sperm_whale) has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run!
|
||||
|
||||
Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a _massive underground cave system_ just beyond where they're aiming!
|
||||
|
||||
The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?
|
||||
|
||||
There's one major catch - crab submarines can only move horizontally.
|
||||
|
||||
You quickly make a list of _the horizontal position of each crab_ (your puzzle input). Crab submarines have limited fuel, so you need to find a way to make all of their horizontal positions match while requiring them to spend as little fuel as possible.
|
||||
|
||||
For example, consider the following horizontal positions:
|
||||
|
||||
16,1,2,0,4,2,7,1,2,14
|
||||
|
||||
This means there's a crab with horizontal position `16`, a crab with horizontal position `1`, and so on.
|
||||
|
||||
Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any horizontal position to align them all on, but the one that costs the least fuel is horizontal position `2`:
|
||||
|
||||
* Move from `16` to `2`: `14` fuel
|
||||
* Move from `1` to `2`: `1` fuel
|
||||
* Move from `2` to `2`: `0` fuel
|
||||
* Move from `0` to `2`: `2` fuel
|
||||
* Move from `4` to `2`: `2` fuel
|
||||
* Move from `2` to `2`: `0` fuel
|
||||
* Move from `7` to `2`: `5` fuel
|
||||
* Move from `1` to `2`: `1` fuel
|
||||
* Move from `2` to `2`: `0` fuel
|
||||
* Move from `14` to `2`: `12` fuel
|
||||
|
||||
This costs a total of _`37`_ fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position `1` (`41` fuel), position `3` (`39` fuel), or position `10` (`71` fuel).
|
||||
|
||||
Determine the horizontal position that the crabs can align to using the least fuel possible. _How much fuel must they spend to align to that position?_
|
||||
|
||||
### Part Two
|
||||
|
||||
The crabs don't seem interested in your proposed solution. Perhaps you misunderstand crab engineering?
|
||||
|
||||
As it turns out, crab submarine engines <span title="This appears to be due to the modial interaction of magneto-reluctance and capacitive duractance.">don't burn fuel at a constant rate</span>. Instead, each change of 1 step in horizontal position costs 1 more unit of fuel than the last: the first step costs `1`, the second step costs `2`, the third step costs `3`, and so on.
|
||||
|
||||
As each crab moves, moving further becomes more expensive. This changes the best horizontal position to align them all on; in the example above, this becomes `5`:
|
||||
|
||||
* Move from `16` to `5`: `66` fuel
|
||||
* Move from `1` to `5`: `10` fuel
|
||||
* Move from `2` to `5`: `6` fuel
|
||||
* Move from `0` to `5`: `15` fuel
|
||||
* Move from `4` to `5`: `1` fuel
|
||||
* Move from `2` to `5`: `6` fuel
|
||||
* Move from `7` to `5`: `3` fuel
|
||||
* Move from `1` to `5`: `10` fuel
|
||||
* Move from `2` to `5`: `6` fuel
|
||||
* Move from `14` to `5`: `45` fuel
|
||||
|
||||
This costs a total of _`168`_ fuel. This is the new cheapest possible outcome; the old alignment position (`2`) now costs `206` fuel instead.
|
||||
|
||||
Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! _How much fuel must they spend to align to that position?_
|
||||
136
day-08/README.md
Normal file
136
day-08/README.md
Normal file
@@ -0,0 +1,136 @@
|
||||
# Day 8: Seven Segment Search
|
||||
|
||||
[https://adventofcode.com/2021/day/8](https://adventofcode.com/2021/day/8)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it. Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to press on.
|
||||
|
||||
As your submarine slowly makes its way through the cave system, you notice that the four-digit [seven-segment displays](https://en.wikipedia.org/wiki/Seven-segment_display) in your submarine are malfunctioning; <span title="Yes, just the four-digit seven-segment ones. Whole batch must have been faulty.">they must have been damaged</span> during the escape. You'll be in a lot of trouble without them, so you'd better figure out what's wrong.
|
||||
|
||||
Each digit of a seven-segment display is rendered by turning on or off any of seven segments named `a` through `g`:
|
||||
|
||||
0: 1: 2: 3: 4:
|
||||
aaaa .... aaaa aaaa ....
|
||||
b c . c . c . c b c
|
||||
b c . c . c . c b c
|
||||
.... .... dddd dddd dddd
|
||||
e f . f e . . f . f
|
||||
e f . f e . . f . f
|
||||
gggg .... gggg gggg ....
|
||||
|
||||
5: 6: 7: 8: 9:
|
||||
aaaa aaaa aaaa aaaa aaaa
|
||||
b . b . . c b c b c
|
||||
b . b . . c b c b c
|
||||
dddd dddd .... dddd dddd
|
||||
. f e f . f e f . f
|
||||
. f e f . f e f . f
|
||||
gggg gggg .... gggg gggg
|
||||
|
||||
|
||||
So, to render a `1`, only segments `c` and `f` would be turned on; the rest would be off. To render a `7`, only segments `a`, `c`, and `f` would be turned on.
|
||||
|
||||
The problem is that the signals which control the segments have been mixed up on each display. The submarine is still trying to display numbers by producing output on signal wires `a` through `g`, but those wires are connected to segments _randomly_. Worse, the wire/segment connections are mixed up separately for each four-digit display! (All of the digits _within_ a display use the same connections, though.)
|
||||
|
||||
So, you might know that only signal wires `b` and `g` are turned on, but that doesn't mean _segments_ `b` and `g` are turned on: the only digit that uses two segments is `1`, so it must mean segments `c` and `f` are meant to be on. With just that information, you still can't tell which wire (`b`/`g`) goes to which segment (`c`/`f`). For that, you'll need to collect more information.
|
||||
|
||||
For each display, you watch the changing signals for a while, make a note of _all ten unique signal patterns_ you see, and then write down a single _four digit output value_ (your puzzle input). Using the signal patterns, you should be able to work out which pattern corresponds to which digit.
|
||||
|
||||
For example, here is what you might see in a single entry in your notes:
|
||||
|
||||
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
|
||||
cdfeb fcadb cdfeb cdbaf
|
||||
|
||||
(The entry is wrapped here to two lines so it fits; in your notes, it will all be on a single line.)
|
||||
|
||||
Each entry consists of ten _unique signal patterns_, a `|` delimiter, and finally the _four digit output value_. Within an entry, the same wire/segment connections are used (but you don't know what the connections actually are). The unique signal patterns correspond to the ten different ways the submarine tries to render a digit using the current wire/segment connections. Because `7` is the only digit that uses three segments, `dab` in the above example means that to render a `7`, signal lines `d`, `a`, and `b` are on. Because `4` is the only digit that uses four segments, `eafb` means that to render a `4`, signal lines `e`, `a`, `f`, and `b` are on.
|
||||
|
||||
Using this information, you should be able to work out which combination of signal wires corresponds to each of the ten digits. Then, you can decode the four digit output value. Unfortunately, in the above example, all of the digits in the output value (`cdfeb fcadb cdfeb cdbaf`) use five segments and are more difficult to deduce.
|
||||
|
||||
For now, _focus on the easy digits_. Consider this larger example:
|
||||
|
||||
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |
|
||||
fdgacbe cefdb cefbgd gcbe
|
||||
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |
|
||||
fcgedb cgb dgebacf gc
|
||||
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |
|
||||
cg cg fdcagb cbg
|
||||
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |
|
||||
efabcd cedba gadfec cb
|
||||
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |
|
||||
gecf egdcabf bgf bfgea
|
||||
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |
|
||||
gebdcfa ecba ca fadegcb
|
||||
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |
|
||||
cefg dcbef fcge gbcadfe
|
||||
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |
|
||||
ed bcgafe cdgba cbgef
|
||||
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |
|
||||
gbdfcae bgc cg cgb
|
||||
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |
|
||||
fgae cfgab fg bagce
|
||||
|
||||
|
||||
Because the digits `1`, `4`, `7`, and `8` each use a unique number of segments, you should be able to tell which combinations of signals correspond to those digits. Counting _only digits in the output values_ (the part after `|` on each line), in the above example, there are _`26`_ instances of digits that use a unique number of segments (highlighted above).
|
||||
|
||||
_In the output values, how many times do digits `1`, `4`, `7`, or `8` appear?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Through a little deduction, you should now be able to determine the remaining digits. Consider again the first example above:
|
||||
|
||||
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
|
||||
cdfeb fcadb cdfeb cdbaf
|
||||
|
||||
After some careful analysis, the mapping between signal wires and segments only make sense in the following configuration:
|
||||
|
||||
dddd
|
||||
e a
|
||||
e a
|
||||
ffff
|
||||
g b
|
||||
g b
|
||||
cccc
|
||||
|
||||
|
||||
So, the unique signal patterns would correspond to the following digits:
|
||||
|
||||
* `acedgfb`: `8`
|
||||
* `cdfbe`: `5`
|
||||
* `gcdfa`: `2`
|
||||
* `fbcad`: `3`
|
||||
* `dab`: `7`
|
||||
* `cefabd`: `9`
|
||||
* `cdfgeb`: `6`
|
||||
* `eafb`: `4`
|
||||
* `cagedb`: `0`
|
||||
* `ab`: `1`
|
||||
|
||||
Then, the four digits of the output value can be decoded:
|
||||
|
||||
* `cdfeb`: _`5`_
|
||||
* `fcadb`: _`3`_
|
||||
* `cdfeb`: _`5`_
|
||||
* `cdbaf`: _`3`_
|
||||
|
||||
Therefore, the output value for this entry is _`5353`_.
|
||||
|
||||
Following this same process for each entry in the second, larger example above, the output value of each entry can be determined:
|
||||
|
||||
* `fdgacbe cefdb cefbgd gcbe`: `8394`
|
||||
* `fcgedb cgb dgebacf gc`: `9781`
|
||||
* `cg cg fdcagb cbg`: `1197`
|
||||
* `efabcd cedba gadfec cb`: `9361`
|
||||
* `gecf egdcabf bgf bfgea`: `4873`
|
||||
* `gebdcfa ecba ca fadegcb`: `8418`
|
||||
* `cefg dcbef fcge gbcadfe`: `4548`
|
||||
* `ed bcgafe cdgba cbgef`: `1625`
|
||||
* `gbdfcae bgc cg cgb`: `8717`
|
||||
* `fgae cfgab fg bagce`: `4315`
|
||||
|
||||
Adding all of the output values in this larger example produces _`61229`_.
|
||||
|
||||
For each entry, determine all of the wire/segment connections and decode the four-digit output values. _What do you get if you add up all of the output values?_
|
||||
78
day-09/README.md
Normal file
78
day-09/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# Day 9: Smoke Basin
|
||||
|
||||
[https://adventofcode.com/2021/day/9](https://adventofcode.com/2021/day/9)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
These caves seem to be [lava tubes](https://en.wikipedia.org/wiki/Lava_tube). Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly <span title="This was originally going to be a puzzle about watersheds, but we're already under water.">settles like rain</span>.
|
||||
|
||||
If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).
|
||||
|
||||
Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
|
||||
Each number corresponds to the height of a particular location, where `9` is the highest and `0` is the lowest a location can be.
|
||||
|
||||
Your first goal is to find the _low points_ - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)
|
||||
|
||||
In the above example, there are _four_ low points, all highlighted: two are in the first row (a `1` and a `0`), one is in the third row (a `5`), and one is in the bottom row (also a `5`). All other locations on the heightmap have some lower adjacent location, and so are not low points.
|
||||
|
||||
The _risk level_ of a low point is _1 plus its height_. In the above example, the risk levels of the low points are `2`, `1`, `6`, and `6`. The sum of the risk levels of all low points in the heightmap is therefore _`15`_.
|
||||
|
||||
Find all of the low points on your heightmap. _What is the sum of the risk levels of all low points on your heightmap?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Next, you need to find the largest basins so you know what areas are most important to avoid.
|
||||
|
||||
A _basin_ is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height `9` do not count as being in any basin, and all other locations will always be part of exactly one basin.
|
||||
|
||||
The _size_ of a basin is the number of locations within the basin, including the low point. The example above has four basins.
|
||||
|
||||
The top-left basin, size `3`:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
|
||||
The top-right basin, size `9`:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
|
||||
The middle basin, size `14`:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
|
||||
The bottom-right basin, size `9`:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
|
||||
Find the three largest basins and multiply their sizes together. In the above example, this is `9 * 14 * 9 = 1134`.
|
||||
|
||||
_What do you get if you multiply together the sizes of the three largest basins?_
|
||||
106
day-10/README.md
Normal file
106
day-10/README.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# Day 10: Syntax Scoring
|
||||
|
||||
[https://adventofcode.com/2021/day/10](https://adventofcode.com/2021/day/10)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You ask the submarine to determine the best route out of the deep-sea cave, but it only replies:
|
||||
|
||||
Syntax error in navigation subsystem on line: all of them
|
||||
|
||||
_All of them?!_ The damage is worse than you thought. You bring up a copy of the navigation subsystem (your puzzle input).
|
||||
|
||||
The navigation subsystem syntax is made of several lines containing _chunks_. There are one or more chunks on each line, and chunks contain zero or more other chunks. Adjacent chunks are not separated by any delimiter; if one chunk stops, the next chunk (if any) can immediately start. Every chunk must _open_ and _close_ with one of four legal pairs of matching characters:
|
||||
|
||||
* If a chunk opens with `(`, it must close with `)`.
|
||||
* If a chunk opens with `[`, it must close with `]`.
|
||||
* If a chunk opens with `{`, it must close with `}`.
|
||||
* If a chunk opens with `<`, it must close with `>`.
|
||||
|
||||
So, `()` is a legal chunk that contains no other chunks, as is `[]`. More complex but valid chunks include `([])`, `{()()()}`, `<([{}])>`, `[<>({}){}[([])<>]]`, and even `(((((((((())))))))))`.
|
||||
|
||||
Some lines are _incomplete_, but others are _corrupted_. Find and discard the corrupted lines first.
|
||||
|
||||
A corrupted line is one where a chunk _closes with the wrong character_ - that is, where the characters it opens and closes with do not form one of the four legal pairs listed above.
|
||||
|
||||
Examples of corrupted chunks include `(]`, `{()()()>`, `(((()))}`, and `<([]){()}[{}])`. Such a chunk can appear anywhere within a line, and its presence causes the whole line to be considered corrupted.
|
||||
|
||||
For example, consider the following navigation subsystem:
|
||||
|
||||
[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]
|
||||
|
||||
|
||||
Some of the lines aren't corrupted, just incomplete; you can ignore these lines for now. The remaining five lines are corrupted:
|
||||
|
||||
* `{([(<{}[<>[]}>{[]{[(<()>` - Expected `]`, but found `}` instead.
|
||||
* `[[<[([]))<([[{}[[()]]]` - Expected `]`, but found `)` instead.
|
||||
* `[{[{({}]{}}([{[{{{}}([]` - Expected `)`, but found `]` instead.
|
||||
* `[<(<(<(<{}))><([]([]()` - Expected `>`, but found `)` instead.
|
||||
* `<{([([[(<>()){}]>(<<{{` - Expected `]`, but found `>` instead.
|
||||
|
||||
Stop at the first incorrect closing character on each corrupted line.
|
||||
|
||||
Did you know that syntax checkers actually have contests to see who can get the high score for syntax errors in a file? It's true! To calculate the syntax error score for a line, take the _first illegal character_ on the line and look it up in the following table:
|
||||
|
||||
* `)`: `3` points.
|
||||
* `]`: `57` points.
|
||||
* `}`: `1197` points.
|
||||
* `>`: `25137` points.
|
||||
|
||||
In the above example, an illegal `)` was found twice (`2*3 = 6` points), an illegal `]` was found once (_`57`_ points), an illegal `}` was found once (_`1197`_ points), and an illegal `>` was found once (_`25137`_ points). So, the total syntax error score for this file is `6+57+1197+25137 = 26397` points!
|
||||
|
||||
Find the first illegal character in each corrupted line of the navigation subsystem. _What is the total syntax error score for those errors?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Now, discard the corrupted lines. The remaining lines are _incomplete_.
|
||||
|
||||
Incomplete lines don't have any incorrect characters - instead, they're missing some closing characters at the end of the line. To repair the navigation subsystem, you just need to figure out _the sequence of closing characters_ that complete all open chunks in the line.
|
||||
|
||||
You can only use closing characters (`)`, `]`, `}`, or `>`), and you must add them in the correct order so that only legal pairs are formed and all chunks end up closed.
|
||||
|
||||
In the example above, there are five incomplete lines:
|
||||
|
||||
* `[({(<(())[]>[[{[]{<()<>>` - Complete by adding `}}]])})]`.
|
||||
* `[(()[<>])]({[<{<<[]>>(` - Complete by adding `)}>]})`.
|
||||
* `(((({<>}<{<{<>}{[]{[]{}` - Complete by adding `}}>}>))))`.
|
||||
* `{<[[]]>}<{[{[{[]{()[[[]` - Complete by adding `]]}}]}]}>`.
|
||||
* `<{([{{}}[<[[[<>{}]]]>[]]` - Complete by adding `])}>`.
|
||||
|
||||
Did you know that autocomplete tools _also_ have contests? It's true! The score is determined by considering the completion string character-by-character. Start with a total score of `0`. Then, for each character, multiply the total score by 5 and then increase the total score by the point value given for the character in the following table:
|
||||
|
||||
* `)`: `1` point.
|
||||
* `]`: `2` points.
|
||||
* `}`: `3` points.
|
||||
* `>`: `4` points.
|
||||
|
||||
So, the last completion string above - `])}>` - would be scored as follows:
|
||||
|
||||
* Start with a total score of `0`.
|
||||
* Multiply the total score by 5 to get `0`, then add the value of `]` (2) to get a new total score of `2`.
|
||||
* Multiply the total score by 5 to get `10`, then add the value of `)` (1) to get a new total score of `11`.
|
||||
* Multiply the total score by 5 to get `55`, then add the value of `}` (3) to get a new total score of `58`.
|
||||
* Multiply the total score by 5 to get `290`, then add the value of `>` (4) to get a new total score of `294`.
|
||||
|
||||
The five lines' completion strings have total scores as follows:
|
||||
|
||||
* `}}]])})]` - `288957` total points.
|
||||
* `)}>]})` - `5566` total points.
|
||||
* `}}>}>))))` - `1480781` total points.
|
||||
* `]]}}]}]}>` - `995444` total points.
|
||||
* `])}>` - `294` total points.
|
||||
|
||||
Autocomplete tools are an odd bunch: the winner is found by _sorting_ all of the scores and then taking the _middle_ score. (There will always be an odd number of scores to consider.) In this example, the middle score is _`288957`_ because there are the same number of scores smaller and larger than it.
|
||||
|
||||
Find the completion string for each incomplete line, score the completion strings, and sort the scores. _What is the middle score?_
|
||||
354
day-11/README.md
Normal file
354
day-11/README.md
Normal file
@@ -0,0 +1,354 @@
|
||||
# Day 11: Dumbo Octopus
|
||||
|
||||
[https://adventofcode.com/2021/day/11](https://adventofcode.com/2021/day/11)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You enter a large cavern full of rare bioluminescent [dumbo octopuses](https://www.youtube.com/watch?v=eih-VSaS2g0)! They seem to not like the Christmas lights on your submarine, so you turn them off for now.
|
||||
|
||||
There are 100 <span title="I know it's weird; I grew up saying 'octopi' too.">octopuses</span> arranged neatly in a 10 by 10 grid. Each octopus slowly gains _energy_ over time and _flashes_ brightly for a moment when its energy is full. Although your lights are off, maybe you could navigate through the cave without disturbing the octopuses if you could predict when the flashes of light will happen.
|
||||
|
||||
Each octopus has an _energy level_ - your submarine can remotely measure the energy level of each octopus (your puzzle input). For example:
|
||||
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
||||
|
||||
|
||||
The energy level of each octopus is a value between `0` and `9`. Here, the top-left octopus has an energy level of `5`, the bottom-right one has an energy level of `6`, and so on.
|
||||
|
||||
You can model the energy levels and flashes of light in _steps_. During a single step, the following occurs:
|
||||
|
||||
* First, the energy level of each octopus increases by `1`.
|
||||
* Then, any octopus with an energy level greater than `9` _flashes_. This increases the energy level of all adjacent octopuses by `1`, including octopuses that are diagonally adjacent. If this causes an octopus to have an energy level greater than `9`, it _also flashes_. This process continues as long as new octopuses keep having their energy level increased beyond `9`. (An octopus can only flash _at most once per step_.)
|
||||
* Finally, any octopus that flashed during this step has its energy level set to `0`, as it used all of its energy to flash.
|
||||
|
||||
Adjacent flashes can cause an octopus to flash on a step even if it begins that step with very little energy. Consider the middle octopus with `1` energy in this situation:
|
||||
|
||||
Before any steps:
|
||||
11111
|
||||
19991
|
||||
19191
|
||||
19991
|
||||
11111
|
||||
|
||||
After step 1:
|
||||
34543
|
||||
40004
|
||||
50005
|
||||
40004
|
||||
34543
|
||||
|
||||
After step 2:
|
||||
45654
|
||||
51115
|
||||
61116
|
||||
51115
|
||||
45654
|
||||
|
||||
|
||||
An octopus is _highlighted_ when it flashed during the given step.
|
||||
|
||||
Here is how the larger example above progresses:
|
||||
|
||||
Before any steps:
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
||||
|
||||
After step 1:
|
||||
6594254334
|
||||
3856965822
|
||||
6375667284
|
||||
7252447257
|
||||
7468496589
|
||||
5278635756
|
||||
3287952832
|
||||
7993992245
|
||||
5957959665
|
||||
6394862637
|
||||
|
||||
After step 2:
|
||||
8807476555
|
||||
5089087054
|
||||
8597889608
|
||||
8485769600
|
||||
8700908800
|
||||
6600088989
|
||||
6800005943
|
||||
0000007456
|
||||
9000000876
|
||||
8700006848
|
||||
|
||||
After step 3:
|
||||
0050900866
|
||||
8500800575
|
||||
9900000039
|
||||
9700000041
|
||||
9935080063
|
||||
7712300000
|
||||
7911250009
|
||||
2211130000
|
||||
0421125000
|
||||
0021119000
|
||||
|
||||
After step 4:
|
||||
2263031977
|
||||
0923031697
|
||||
0032221150
|
||||
0041111163
|
||||
0076191174
|
||||
0053411122
|
||||
0042361120
|
||||
5532241122
|
||||
1532247211
|
||||
1132230211
|
||||
|
||||
After step 5:
|
||||
4484144000
|
||||
2044144000
|
||||
2253333493
|
||||
1152333274
|
||||
1187303285
|
||||
1164633233
|
||||
1153472231
|
||||
6643352233
|
||||
2643358322
|
||||
2243341322
|
||||
|
||||
After step 6:
|
||||
5595255111
|
||||
3155255222
|
||||
3364444605
|
||||
2263444496
|
||||
2298414396
|
||||
2275744344
|
||||
2264583342
|
||||
7754463344
|
||||
3754469433
|
||||
3354452433
|
||||
|
||||
After step 7:
|
||||
6707366222
|
||||
4377366333
|
||||
4475555827
|
||||
3496655709
|
||||
3500625609
|
||||
3509955566
|
||||
3486694453
|
||||
8865585555
|
||||
4865580644
|
||||
4465574644
|
||||
|
||||
After step 8:
|
||||
7818477333
|
||||
5488477444
|
||||
5697666949
|
||||
4608766830
|
||||
4734946730
|
||||
4740097688
|
||||
6900007564
|
||||
0000009666
|
||||
8000004755
|
||||
6800007755
|
||||
|
||||
After step 9:
|
||||
9060000644
|
||||
7800000976
|
||||
6900000080
|
||||
5840000082
|
||||
5858000093
|
||||
6962400000
|
||||
8021250009
|
||||
2221130009
|
||||
9111128097
|
||||
7911119976
|
||||
|
||||
After step 10:
|
||||
0481112976
|
||||
0031112009
|
||||
0041112504
|
||||
0081111406
|
||||
0099111306
|
||||
0093511233
|
||||
0442361130
|
||||
5532252350
|
||||
0532250600
|
||||
0032240000
|
||||
|
||||
|
||||
After step 10, there have been a total of `204` flashes. Fast forwarding, here is the same configuration every 10 steps:
|
||||
|
||||
After step 20:
|
||||
3936556452
|
||||
5686556806
|
||||
4496555690
|
||||
4448655580
|
||||
4456865570
|
||||
5680086577
|
||||
7000009896
|
||||
0000000344
|
||||
6000000364
|
||||
4600009543
|
||||
|
||||
After step 30:
|
||||
0643334118
|
||||
4253334611
|
||||
3374333458
|
||||
2225333337
|
||||
2229333338
|
||||
2276733333
|
||||
2754574565
|
||||
5544458511
|
||||
9444447111
|
||||
7944446119
|
||||
|
||||
After step 40:
|
||||
6211111981
|
||||
0421111119
|
||||
0042111115
|
||||
0003111115
|
||||
0003111116
|
||||
0065611111
|
||||
0532351111
|
||||
3322234597
|
||||
2222222976
|
||||
2222222762
|
||||
|
||||
After step 50:
|
||||
9655556447
|
||||
4865556805
|
||||
4486555690
|
||||
4458655580
|
||||
4574865570
|
||||
5700086566
|
||||
6000009887
|
||||
8000000533
|
||||
6800000633
|
||||
5680000538
|
||||
|
||||
After step 60:
|
||||
2533334200
|
||||
2743334640
|
||||
2264333458
|
||||
2225333337
|
||||
2225333338
|
||||
2287833333
|
||||
3854573455
|
||||
1854458611
|
||||
1175447111
|
||||
1115446111
|
||||
|
||||
After step 70:
|
||||
8211111164
|
||||
0421111166
|
||||
0042111114
|
||||
0004211115
|
||||
0000211116
|
||||
0065611111
|
||||
0532351111
|
||||
7322235117
|
||||
5722223475
|
||||
4572222754
|
||||
|
||||
After step 80:
|
||||
1755555697
|
||||
5965555609
|
||||
4486555680
|
||||
4458655580
|
||||
4570865570
|
||||
5700086566
|
||||
7000008666
|
||||
0000000990
|
||||
0000000800
|
||||
0000000000
|
||||
|
||||
After step 90:
|
||||
7433333522
|
||||
2643333522
|
||||
2264333458
|
||||
2226433337
|
||||
2222433338
|
||||
2287833333
|
||||
2854573333
|
||||
4854458333
|
||||
3387779333
|
||||
3333333333
|
||||
|
||||
After step 100:
|
||||
0397666866
|
||||
0749766918
|
||||
0053976933
|
||||
0004297822
|
||||
0004229892
|
||||
0053222877
|
||||
0532222966
|
||||
9322228966
|
||||
7922286866
|
||||
6789998766
|
||||
|
||||
|
||||
After 100 steps, there have been a total of _`1656`_ flashes.
|
||||
|
||||
Given the starting energy levels of the dumbo octopuses in your cavern, simulate 100 steps. _How many total flashes are there after 100 steps?_
|
||||
|
||||
### Part Two
|
||||
|
||||
It seems like the individual flashes aren't bright enough to navigate. However, you might have a better option: the flashes seem to be _synchronizing_!
|
||||
|
||||
In the example above, the first time all octopuses flash simultaneously is step _`195`_:
|
||||
|
||||
After step 193:
|
||||
5877777777
|
||||
8877777777
|
||||
7777777777
|
||||
7777777777
|
||||
7777777777
|
||||
7777777777
|
||||
7777777777
|
||||
7777777777
|
||||
7777777777
|
||||
7777777777
|
||||
|
||||
After step 194:
|
||||
6988888888
|
||||
9988888888
|
||||
8888888888
|
||||
8888888888
|
||||
8888888888
|
||||
8888888888
|
||||
8888888888
|
||||
8888888888
|
||||
8888888888
|
||||
8888888888
|
||||
|
||||
After step 195:
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
0000000000
|
||||
|
||||
|
||||
If you can calculate the exact moments when the octopuses will all flash simultaneously, you should be able to navigate through the cavern. _What is the first step during which all octopuses flash?_
|
||||
160
day-12/README.md
Normal file
160
day-12/README.md
Normal file
@@ -0,0 +1,160 @@
|
||||
# Day 12: Passage Pathing
|
||||
|
||||
[https://adventofcode.com/2021/day/12](https://adventofcode.com/2021/day/12)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
With your <span title="Sublime.">submarine's subterranean subsystems subsisting suboptimally</span>, the only way you're getting out of this cave anytime soon is by finding a path yourself. Not just _a_ path - the only way to know if you've found the _best_ path is to find _all_ of them.
|
||||
|
||||
Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example:
|
||||
|
||||
start-A
|
||||
start-b
|
||||
A-c
|
||||
A-b
|
||||
b-d
|
||||
A-end
|
||||
b-end
|
||||
|
||||
|
||||
This is a list of how all of the caves are connected. You start in the cave named `start`, and your destination is the cave named `end`. An entry like `b-d` means that cave `b` is connected to cave `d` - that is, you can move between them.
|
||||
|
||||
So, the above cave system looks roughly like this:
|
||||
|
||||
start
|
||||
/ \
|
||||
c--A-----b--d
|
||||
\ /
|
||||
end
|
||||
|
||||
|
||||
Your goal is to find the number of distinct _paths_ that start at `start`, end at `end`, and don't visit small caves more than once. There are two types of caves: _big_ caves (written in uppercase, like `A`) and _small_ caves (written in lowercase, like `b`). It would be a waste of time to visit any small cave more than once, but big caves are large enough that it might be worth visiting them multiple times. So, all paths you find should _visit small caves at most once_, and can _visit big caves any number of times_.
|
||||
|
||||
Given these rules, there are _`10`_ paths through this example cave system:
|
||||
|
||||
start,A,b,A,c,A,end
|
||||
start,A,b,A,end
|
||||
start,A,b,end
|
||||
start,A,c,A,b,A,end
|
||||
start,A,c,A,b,end
|
||||
start,A,c,A,end
|
||||
start,A,end
|
||||
start,b,A,c,A,end
|
||||
start,b,A,end
|
||||
start,b,end
|
||||
|
||||
|
||||
(Each line in the above list corresponds to a single path; the caves visited by that path are listed in the order they are visited and separated by commas.)
|
||||
|
||||
Note that in this cave system, cave `d` is never visited by any path: to do so, cave `b` would need to be visited twice (once on the way to cave `d` and a second time when returning from cave `d`), and since cave `b` is small, this is not allowed.
|
||||
|
||||
Here is a slightly larger example:
|
||||
|
||||
dc-end
|
||||
HN-start
|
||||
start-kj
|
||||
dc-start
|
||||
dc-HN
|
||||
LN-dc
|
||||
HN-end
|
||||
kj-sa
|
||||
kj-HN
|
||||
kj-dc
|
||||
|
||||
|
||||
The `19` paths through it are as follows:
|
||||
|
||||
start,HN,dc,HN,end
|
||||
start,HN,dc,HN,kj,HN,end
|
||||
start,HN,dc,end
|
||||
start,HN,dc,kj,HN,end
|
||||
start,HN,end
|
||||
start,HN,kj,HN,dc,HN,end
|
||||
start,HN,kj,HN,dc,end
|
||||
start,HN,kj,HN,end
|
||||
start,HN,kj,dc,HN,end
|
||||
start,HN,kj,dc,end
|
||||
start,dc,HN,end
|
||||
start,dc,HN,kj,HN,end
|
||||
start,dc,end
|
||||
start,dc,kj,HN,end
|
||||
start,kj,HN,dc,HN,end
|
||||
start,kj,HN,dc,end
|
||||
start,kj,HN,end
|
||||
start,kj,dc,HN,end
|
||||
start,kj,dc,end
|
||||
|
||||
|
||||
Finally, this even larger example has `226` paths through it:
|
||||
|
||||
fs-end
|
||||
he-DX
|
||||
fs-he
|
||||
start-DX
|
||||
pj-DX
|
||||
end-zg
|
||||
zg-sl
|
||||
zg-pj
|
||||
pj-he
|
||||
RW-he
|
||||
fs-DX
|
||||
pj-RW
|
||||
zg-RW
|
||||
start-pj
|
||||
he-WI
|
||||
zg-he
|
||||
pj-fs
|
||||
start-RW
|
||||
|
||||
|
||||
_How many paths through this cave system are there that visit small caves at most once?_
|
||||
|
||||
### Part Two
|
||||
|
||||
After reviewing the available paths, you realize you might have time to visit a single small cave _twice_. Specifically, big caves can be visited any number of times, a single small cave can be visited at most twice, and the remaining small caves can be visited at most once. However, the caves named `start` and `end` can only be visited _exactly once each_: once you leave the `start` cave, you may not return to it, and once you reach the `end` cave, the path must end immediately.
|
||||
|
||||
Now, the `36` possible paths through the first example above are:
|
||||
|
||||
start,A,b,A,b,A,c,A,end
|
||||
start,A,b,A,b,A,end
|
||||
start,A,b,A,b,end
|
||||
start,A,b,A,c,A,b,A,end
|
||||
start,A,b,A,c,A,b,end
|
||||
start,A,b,A,c,A,c,A,end
|
||||
start,A,b,A,c,A,end
|
||||
start,A,b,A,end
|
||||
start,A,b,d,b,A,c,A,end
|
||||
start,A,b,d,b,A,end
|
||||
start,A,b,d,b,end
|
||||
start,A,b,end
|
||||
start,A,c,A,b,A,b,A,end
|
||||
start,A,c,A,b,A,b,end
|
||||
start,A,c,A,b,A,c,A,end
|
||||
start,A,c,A,b,A,end
|
||||
start,A,c,A,b,d,b,A,end
|
||||
start,A,c,A,b,d,b,end
|
||||
start,A,c,A,b,end
|
||||
start,A,c,A,c,A,b,A,end
|
||||
start,A,c,A,c,A,b,end
|
||||
start,A,c,A,c,A,end
|
||||
start,A,c,A,end
|
||||
start,A,end
|
||||
start,b,A,b,A,c,A,end
|
||||
start,b,A,b,A,end
|
||||
start,b,A,b,end
|
||||
start,b,A,c,A,b,A,end
|
||||
start,b,A,c,A,b,end
|
||||
start,b,A,c,A,c,A,end
|
||||
start,b,A,c,A,end
|
||||
start,b,A,end
|
||||
start,b,d,b,A,c,A,end
|
||||
start,b,d,b,A,end
|
||||
start,b,d,b,end
|
||||
start,b,end
|
||||
|
||||
|
||||
The slightly larger example above now has `103` paths through it, and the even larger example now has `3509` paths through it.
|
||||
|
||||
Given these new rules, _how many paths through this cave system are there?_
|
||||
69
day-12/day-12.py
Normal file
69
day-12/day-12.py
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def process_node(node, edges, visited):
|
||||
num_paths = 0
|
||||
if node == "end":
|
||||
return 1
|
||||
if node.islower():
|
||||
visited.add(node)
|
||||
neighbors = [n for e in edges if node in e for n in e if n != node]
|
||||
for n in neighbors:
|
||||
if n not in visited:
|
||||
num_paths += process_node(n, edges, visited)
|
||||
if node in visited:
|
||||
visited.remove(node)
|
||||
return num_paths
|
||||
|
||||
|
||||
def process_node_twice(node, edges, visited, visited_twice):
|
||||
num_paths = 0
|
||||
if node == "end":
|
||||
return 1
|
||||
if node.islower():
|
||||
if node in visited and visited_twice:
|
||||
return num_paths
|
||||
elif node in visited and node != "start":
|
||||
visited_twice = node
|
||||
else:
|
||||
visited.add(node)
|
||||
neighbors = [n for e in edges if node in e for n in e if n != node]
|
||||
for n in neighbors:
|
||||
if n not in visited or visited_twice != node and n != "start":
|
||||
num_paths += process_node_twice(n, edges, visited, visited_twice)
|
||||
if visited_twice == node:
|
||||
visited_twice = None
|
||||
elif node in visited:
|
||||
visited.remove(node)
|
||||
return num_paths
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
edges = set()
|
||||
for line in input:
|
||||
n_start, n_end = line.strip().split('-')
|
||||
e = (n_start, n_end)
|
||||
edges.add(e)
|
||||
result = process_node("start", edges, set())
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
edges = set()
|
||||
for line in input:
|
||||
n_start, n_end = line.strip().split('-')
|
||||
e = (n_start, n_end)
|
||||
edges.add(e)
|
||||
result = process_node_twice("start", edges, set(), None)
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
24
day-12/input.txt
Normal file
24
day-12/input.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
pf-pk
|
||||
ZQ-iz
|
||||
iz-NY
|
||||
ZQ-end
|
||||
pf-gx
|
||||
pk-ZQ
|
||||
ZQ-dc
|
||||
NY-start
|
||||
NY-pf
|
||||
NY-gx
|
||||
ag-ZQ
|
||||
pf-start
|
||||
start-gx
|
||||
BN-ag
|
||||
iz-pf
|
||||
ag-FD
|
||||
pk-NY
|
||||
gx-pk
|
||||
end-BN
|
||||
ag-pf
|
||||
iz-pk
|
||||
pk-ag
|
||||
iz-end
|
||||
iz-BN
|
||||
129
day-13/README.md
Normal file
129
day-13/README.md
Normal file
@@ -0,0 +1,129 @@
|
||||
# Day 13: Transparent Origami
|
||||
|
||||
[https://adventofcode.com/2021/day/13](https://adventofcode.com/2021/day/13)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You reach another volcanically active part of the cave. It would be nice if you could do some kind of thermal imaging so you could tell ahead of time which caves are too hot to safely enter.
|
||||
|
||||
Fortunately, the submarine seems to be equipped with a thermal camera! When you activate it, you are greeted with:
|
||||
|
||||
Congratulations on your purchase! To activate this infrared thermal imaging
|
||||
camera system, please enter the code found on page 1 of the manual.
|
||||
|
||||
|
||||
Apparently, the Elves have never used this feature. To your surprise, you manage to find the manual; as you go to open it, page 1 falls out. It's a large sheet of [transparent paper](https://en.wikipedia.org/wiki/Transparency_(projection))! The transparent paper is marked with random dots and includes instructions on how to fold it up (your puzzle input). For example:
|
||||
|
||||
6,10
|
||||
0,14
|
||||
9,10
|
||||
0,3
|
||||
10,4
|
||||
4,11
|
||||
6,0
|
||||
6,12
|
||||
4,1
|
||||
0,13
|
||||
10,12
|
||||
3,4
|
||||
3,0
|
||||
8,4
|
||||
1,10
|
||||
2,14
|
||||
8,10
|
||||
9,0
|
||||
|
||||
fold along y=7
|
||||
fold along x=5
|
||||
|
||||
|
||||
The first section is a list of dots on the transparent paper. `0,0` represents the top-left coordinate. The first value, `x`, increases to the right. The second value, `y`, increases downward. So, the coordinate `3,0` is to the right of `0,0`, and the coordinate `0,7` is below `0,0`. The coordinates in this example form the following pattern, where `#` is a dot on the paper and `.` is an empty, unmarked position:
|
||||
|
||||
...#..#..#.
|
||||
....#......
|
||||
...........
|
||||
#..........
|
||||
...#....#.#
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
.#....#.##.
|
||||
....#......
|
||||
......#...#
|
||||
#..........
|
||||
#.#........
|
||||
|
||||
|
||||
Then, there is a list of _fold instructions_. Each instruction indicates a line on the transparent paper and wants you to fold the paper _up_ (for horizontal `y=...` lines) or _left_ (for vertical `x=...` lines). In this example, the first fold instruction is `fold along y=7`, which designates the line formed by all of the positions where `y` is `7` (marked here with `-`):
|
||||
|
||||
...#..#..#.
|
||||
....#......
|
||||
...........
|
||||
#..........
|
||||
...#....#.#
|
||||
...........
|
||||
...........
|
||||
-----------
|
||||
...........
|
||||
...........
|
||||
.#....#.##.
|
||||
....#......
|
||||
......#...#
|
||||
#..........
|
||||
#.#........
|
||||
|
||||
|
||||
Because this is a horizontal line, fold the bottom half _up_. Some of the dots might end up overlapping after the fold is complete, but dots will never appear exactly on a fold line. The result of doing this fold looks like this:
|
||||
|
||||
#.##..#..#.
|
||||
#...#......
|
||||
......#...#
|
||||
#...#......
|
||||
.#.#..#.###
|
||||
...........
|
||||
...........
|
||||
|
||||
|
||||
Now, only `17` dots are visible.
|
||||
|
||||
Notice, for example, the two dots in the bottom left corner before the transparent paper is folded; after the fold is complete, those dots appear in the top left corner (at `0,0` and `0,1`). Because the paper is transparent, the dot just below them in the result (at `0,3`) remains visible, as it can be seen through the transparent paper.
|
||||
|
||||
Also notice that some dots can end up _overlapping_; in this case, the dots merge together and become a single dot.
|
||||
|
||||
The second fold instruction is `fold along x=5`, which indicates this line:
|
||||
|
||||
#.##.|#..#.
|
||||
#...#|.....
|
||||
.....|#...#
|
||||
#...#|.....
|
||||
.#.#.|#.###
|
||||
.....|.....
|
||||
.....|.....
|
||||
|
||||
|
||||
Because this is a vertical line, fold _left_:
|
||||
|
||||
#####
|
||||
#...#
|
||||
#...#
|
||||
#...#
|
||||
#####
|
||||
.....
|
||||
.....
|
||||
|
||||
|
||||
The instructions made a square!
|
||||
|
||||
The transparent paper is pretty big, so for now, focus on just completing the first fold. After the first fold in the example above, _`17`_ dots are visible - dots that end up overlapping after the fold is completed count as a single dot.
|
||||
|
||||
_How many dots are visible after completing just the first fold instruction on your transparent paper?_
|
||||
|
||||
### Part Two
|
||||
|
||||
<span title="How can you fold it that many times? You tell me, I'm not the one folding it.">Finish folding</span> the transparent paper according to the instructions. The manual says the code is always _eight capital letters_.
|
||||
|
||||
_What code do you use to activate the infrared thermal imaging camera system?_
|
||||
67
day-13/day-13.py
Normal file
67
day-13/day-13.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def fold(axis, fold_at, dots):
|
||||
new_dots = set()
|
||||
for (x, y) in dots:
|
||||
if axis == 'x':
|
||||
new_x = fold_at - abs(x - fold_at)
|
||||
if new_x != fold_at:
|
||||
new_dots.add((new_x, y))
|
||||
elif axis == 'y':
|
||||
new_y = fold_at - abs(y - fold_at)
|
||||
if new_y != fold_at:
|
||||
new_dots.add((x, new_y))
|
||||
return new_dots
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
dots = set()
|
||||
instructions = []
|
||||
for line in input:
|
||||
if ',' in line:
|
||||
x, y = line.strip().split(',')
|
||||
dots.add((int(x), int(y)))
|
||||
elif 'fold along' in line:
|
||||
d, c = line.strip().split()[-1].split('=')
|
||||
instructions.append((d, int(c)))
|
||||
axis, fold_at = instructions.pop(0)
|
||||
dots = fold(axis, fold_at, dots)
|
||||
result = len(dots)
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
dots = set()
|
||||
instructions = []
|
||||
for line in input:
|
||||
if ',' in line:
|
||||
x, y = line.strip().split(',')
|
||||
dots.add((int(x), int(y)))
|
||||
elif 'fold along' in line:
|
||||
d, c = line.strip().split()[-1].split('=')
|
||||
instructions.append((d, int(c)))
|
||||
while len(instructions):
|
||||
axis, fold_at = instructions.pop(0)
|
||||
dots = fold(axis, fold_at, dots)
|
||||
max_x = max([x for x, _ in dots])
|
||||
max_y = max([y for _, y in dots])
|
||||
print("Part 2 result:")
|
||||
for y in range(max_y + 1):
|
||||
for x in range(max_x + 1):
|
||||
if (x, y) in dots:
|
||||
print('█', end='')
|
||||
else:
|
||||
print('░', end='')
|
||||
print()
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
971
day-13/input.txt
Normal file
971
day-13/input.txt
Normal file
@@ -0,0 +1,971 @@
|
||||
218,593
|
||||
1176,617
|
||||
57,515
|
||||
806,674
|
||||
517,740
|
||||
402,553
|
||||
1118,53
|
||||
596,577
|
||||
473,147
|
||||
166,817
|
||||
149,154
|
||||
209,228
|
||||
170,159
|
||||
124,376
|
||||
960,481
|
||||
107,73
|
||||
646,313
|
||||
708,393
|
||||
1215,696
|
||||
706,780
|
||||
969,149
|
||||
686,189
|
||||
552,771
|
||||
523,322
|
||||
1058,789
|
||||
155,205
|
||||
616,782
|
||||
113,154
|
||||
1017,280
|
||||
15,352
|
||||
774,623
|
||||
649,633
|
||||
435,591
|
||||
353,638
|
||||
273,327
|
||||
1139,180
|
||||
763,821
|
||||
360,506
|
||||
219,432
|
||||
656,770
|
||||
499,124
|
||||
1081,691
|
||||
1079,68
|
||||
1012,204
|
||||
1146,665
|
||||
669,421
|
||||
192,558
|
||||
92,873
|
||||
626,477
|
||||
1250,435
|
||||
65,99
|
||||
790,36
|
||||
1208,795
|
||||
1290,634
|
||||
344,229
|
||||
1171,43
|
||||
179,154
|
||||
75,316
|
||||
398,788
|
||||
725,80
|
||||
793,694
|
||||
1146,217
|
||||
179,740
|
||||
848,841
|
||||
972,100
|
||||
1010,313
|
||||
495,399
|
||||
410,4
|
||||
442,315
|
||||
1222,75
|
||||
458,768
|
||||
10,100
|
||||
683,537
|
||||
994,570
|
||||
771,91
|
||||
1240,330
|
||||
417,201
|
||||
1280,828
|
||||
462,725
|
||||
795,204
|
||||
833,697
|
||||
1206,357
|
||||
164,217
|
||||
438,312
|
||||
161,621
|
||||
1154,497
|
||||
910,49
|
||||
236,704
|
||||
428,350
|
||||
338,100
|
||||
1215,870
|
||||
589,219
|
||||
956,245
|
||||
1299,752
|
||||
1139,409
|
||||
736,750
|
||||
70,564
|
||||
1266,847
|
||||
20,219
|
||||
1056,78
|
||||
688,488
|
||||
326,376
|
||||
445,666
|
||||
1086,829
|
||||
1155,205
|
||||
715,138
|
||||
484,455
|
||||
1010,649
|
||||
226,544
|
||||
460,710
|
||||
1032,616
|
||||
811,385
|
||||
756,126
|
||||
644,655
|
||||
518,693
|
||||
97,451
|
||||
671,800
|
||||
624,705
|
||||
336,303
|
||||
468,294
|
||||
356,652
|
||||
651,495
|
||||
869,546
|
||||
174,642
|
||||
1068,705
|
||||
708,365
|
||||
566,733
|
||||
950,164
|
||||
266,843
|
||||
435,562
|
||||
391,873
|
||||
391,294
|
||||
75,764
|
||||
38,112
|
||||
388,771
|
||||
149,201
|
||||
375,635
|
||||
972,794
|
||||
343,542
|
||||
1299,142
|
||||
1103,525
|
||||
192,753
|
||||
1063,441
|
||||
989,709
|
||||
8,329
|
||||
316,794
|
||||
524,96
|
||||
666,794
|
||||
822,705
|
||||
256,442
|
||||
708,281
|
||||
338,207
|
||||
31,273
|
||||
994,239
|
||||
1203,73
|
||||
170,567
|
||||
231,68
|
||||
1028,259
|
||||
1138,102
|
||||
1079,480
|
||||
944,289
|
||||
714,129
|
||||
935,635
|
||||
977,261
|
||||
443,54
|
||||
723,508
|
||||
376,651
|
||||
1032,894
|
||||
845,807
|
||||
237,621
|
||||
775,530
|
||||
393,123
|
||||
1006,413
|
||||
465,87
|
||||
579,578
|
||||
22,600
|
||||
580,341
|
||||
284,23
|
||||
328,128
|
||||
244,627
|
||||
10,794
|
||||
433,597
|
||||
684,240
|
||||
875,751
|
||||
994,155
|
||||
137,304
|
||||
984,742
|
||||
441,702
|
||||
1079,414
|
||||
776,142
|
||||
656,670
|
||||
1163,819
|
||||
321,633
|
||||
524,315
|
||||
65,219
|
||||
15,94
|
||||
1196,782
|
||||
1250,883
|
||||
136,649
|
||||
977,709
|
||||
709,887
|
||||
338,879
|
||||
1144,77
|
||||
801,210
|
||||
1196,392
|
||||
1057,241
|
||||
391,425
|
||||
602,393
|
||||
643,590
|
||||
669,429
|
||||
1039,544
|
||||
433,297
|
||||
1198,281
|
||||
408,638
|
||||
1220,190
|
||||
910,221
|
||||
254,845
|
||||
430,829
|
||||
790,277
|
||||
758,381
|
||||
758,323
|
||||
323,467
|
||||
500,291
|
||||
520,858
|
||||
951,808
|
||||
114,782
|
||||
186,600
|
||||
391,469
|
||||
1176,57
|
||||
894,628
|
||||
1176,277
|
||||
179,787
|
||||
8,113
|
||||
795,690
|
||||
290,707
|
||||
10,465
|
||||
1170,93
|
||||
301,591
|
||||
982,766
|
||||
1120,649
|
||||
813,633
|
||||
1174,649
|
||||
887,869
|
||||
806,884
|
||||
361,301
|
||||
383,350
|
||||
336,79
|
||||
236,515
|
||||
473,528
|
||||
333,521
|
||||
1258,689
|
||||
1203,521
|
||||
875,487
|
||||
498,371
|
||||
300,313
|
||||
244,267
|
||||
972,330
|
||||
770,782
|
||||
644,476
|
||||
976,887
|
||||
641,17
|
||||
149,714
|
||||
208,355
|
||||
902,638
|
||||
887,826
|
||||
1302,815
|
||||
518,439
|
||||
803,292
|
||||
932,429
|
||||
124,638
|
||||
997,730
|
||||
459,98
|
||||
1243,609
|
||||
505,180
|
||||
623,796
|
||||
539,257
|
||||
694,179
|
||||
1056,721
|
||||
641,473
|
||||
416,628
|
||||
1284,537
|
||||
1280,66
|
||||
172,792
|
||||
402,789
|
||||
1252,544
|
||||
1174,439
|
||||
11,752
|
||||
1158,291
|
||||
1240,620
|
||||
723,386
|
||||
244,4
|
||||
892,621
|
||||
58,765
|
||||
818,236
|
||||
1017,301
|
||||
753,651
|
||||
1148,0
|
||||
1235,764
|
||||
527,816
|
||||
115,637
|
||||
999,787
|
||||
1006,481
|
||||
500,739
|
||||
137,416
|
||||
1000,229
|
||||
997,25
|
||||
313,25
|
||||
508,229
|
||||
1156,326
|
||||
639,516
|
||||
672,411
|
||||
328,376
|
||||
930,134
|
||||
508,341
|
||||
226,350
|
||||
1161,180
|
||||
458,763
|
||||
900,420
|
||||
825,37
|
||||
728,704
|
||||
1084,544
|
||||
792,649
|
||||
808,575
|
||||
872,312
|
||||
113,516
|
||||
48,488
|
||||
831,640
|
||||
298,92
|
||||
23,379
|
||||
381,663
|
||||
674,315
|
||||
402,105
|
||||
417,649
|
||||
663,462
|
||||
500,313
|
||||
304,824
|
||||
514,861
|
||||
810,774
|
||||
669,249
|
||||
107,704
|
||||
1092,593
|
||||
969,297
|
||||
492,336
|
||||
147,819
|
||||
792,693
|
||||
1278,276
|
||||
107,821
|
||||
997,205
|
||||
1285,396
|
||||
728,67
|
||||
704,894
|
||||
1140,735
|
||||
1121,292
|
||||
423,205
|
||||
102,430
|
||||
408,644
|
||||
194,184
|
||||
294,612
|
||||
810,333
|
||||
582,827
|
||||
1158,603
|
||||
1262,488
|
||||
874,719
|
||||
728,827
|
||||
318,143
|
||||
492,457
|
||||
336,43
|
||||
940,871
|
||||
515,466
|
||||
293,525
|
||||
1115,86
|
||||
611,364
|
||||
509,646
|
||||
418,379
|
||||
462,753
|
||||
987,287
|
||||
639,110
|
||||
982,152
|
||||
464,693
|
||||
1086,571
|
||||
917,123
|
||||
1089,297
|
||||
1121,87
|
||||
238,628
|
||||
370,870
|
||||
1180,350
|
||||
169,814
|
||||
694,715
|
||||
411,621
|
||||
826,775
|
||||
134,617
|
||||
641,249
|
||||
887,733
|
||||
874,271
|
||||
410,474
|
||||
796,481
|
||||
1300,465
|
||||
21,399
|
||||
606,198
|
||||
837,147
|
||||
16,411
|
||||
975,14
|
||||
694,826
|
||||
638,483
|
||||
475,107
|
||||
1000,236
|
||||
622,406
|
||||
1031,707
|
||||
279,707
|
||||
666,655
|
||||
837,528
|
||||
763,143
|
||||
418,256
|
||||
957,254
|
||||
864,427
|
||||
668,296
|
||||
1141,485
|
||||
318,781
|
||||
1124,801
|
||||
207,369
|
||||
1103,369
|
||||
805,714
|
||||
333,709
|
||||
514,353
|
||||
1289,399
|
||||
820,889
|
||||
1294,859
|
||||
504,674
|
||||
974,751
|
||||
509,658
|
||||
1196,868
|
||||
1193,676
|
||||
893,693
|
||||
323,159
|
||||
508,105
|
||||
1196,726
|
||||
962,186
|
||||
957,638
|
||||
1176,53
|
||||
1037,215
|
||||
1074,190
|
||||
908,553
|
||||
818,620
|
||||
806,436
|
||||
1297,197
|
||||
671,107
|
||||
152,603
|
||||
147,75
|
||||
930,760
|
||||
917,347
|
||||
682,357
|
||||
13,136
|
||||
956,649
|
||||
1094,666
|
||||
927,544
|
||||
1203,780
|
||||
1049,323
|
||||
589,99
|
||||
90,190
|
||||
279,315
|
||||
80,108
|
||||
278,224
|
||||
343,339
|
||||
872,393
|
||||
812,371
|
||||
304,413
|
||||
423,752
|
||||
536,623
|
||||
632,152
|
||||
1277,299
|
||||
1156,120
|
||||
808,689
|
||||
1047,152
|
||||
380,312
|
||||
1102,243
|
||||
1280,466
|
||||
786,756
|
||||
606,616
|
||||
1121,864
|
||||
1171,690
|
||||
1176,858
|
||||
102,571
|
||||
463,705
|
||||
805,421
|
||||
88,523
|
||||
507,75
|
||||
808,700
|
||||
1208,717
|
||||
1260,653
|
||||
966,859
|
||||
649,261
|
||||
408,698
|
||||
1208,8
|
||||
276,465
|
||||
623,124
|
||||
48,539
|
||||
186,93
|
||||
852,518
|
||||
1066,267
|
||||
1039,246
|
||||
688,618
|
||||
341,205
|
||||
731,578
|
||||
624,369
|
||||
716,11
|
||||
509,864
|
||||
925,735
|
||||
313,369
|
||||
1123,242
|
||||
1154,103
|
||||
237,273
|
||||
793,154
|
||||
458,824
|
||||
1156,581
|
||||
835,107
|
||||
1280,156
|
||||
703,539
|
||||
310,236
|
||||
1210,495
|
||||
966,229
|
||||
192,165
|
||||
738,26
|
||||
192,865
|
||||
801,236
|
||||
694,292
|
||||
776,590
|
||||
1115,98
|
||||
134,841
|
||||
328,152
|
||||
1020,707
|
||||
306,708
|
||||
1300,239
|
||||
1302,49
|
||||
402,509
|
||||
400,49
|
||||
154,781
|
||||
1294,411
|
||||
202,627
|
||||
801,478
|
||||
137,694
|
||||
840,555
|
||||
801,248
|
||||
252,105
|
||||
354,581
|
||||
666,239
|
||||
48,691
|
||||
1091,407
|
||||
894,852
|
||||
1170,801
|
||||
624,189
|
||||
770,840
|
||||
666,877
|
||||
934,24
|
||||
504,884
|
||||
194,812
|
||||
502,351
|
||||
1186,289
|
||||
22,152
|
||||
117,666
|
||||
1210,47
|
||||
669,17
|
||||
917,508
|
||||
428,768
|
||||
540,782
|
||||
48,0
|
||||
812,343
|
||||
1118,841
|
||||
300,649
|
||||
509,248
|
||||
492,218
|
||||
1020,187
|
||||
869,254
|
||||
20,260
|
||||
1010,581
|
||||
802,105
|
||||
1051,485
|
||||
1282,169
|
||||
140,413
|
||||
1146,453
|
||||
238,42
|
||||
540,502
|
||||
842,801
|
||||
1002,126
|
||||
107,521
|
||||
3,847
|
||||
316,564
|
||||
557,451
|
||||
1002,19
|
||||
783,78
|
||||
929,663
|
||||
462,296
|
||||
1170,481
|
||||
892,414
|
||||
1146,441
|
||||
622,40
|
||||
736,556
|
||||
1156,113
|
||||
944,605
|
||||
418,862
|
||||
351,291
|
||||
540,616
|
||||
1154,819
|
||||
149,869
|
||||
1180,586
|
||||
17,80
|
||||
997,369
|
||||
509,684
|
||||
795,466
|
||||
972,739
|
||||
48,355
|
||||
418,173
|
||||
452,70
|
||||
848,764
|
||||
669,473
|
||||
596,544
|
||||
1148,894
|
||||
32,58
|
||||
567,81
|
||||
169,528
|
||||
274,582
|
||||
156,551
|
||||
1144,817
|
||||
848,729
|
||||
114,726
|
||||
360,282
|
||||
366,605
|
||||
1054,452
|
||||
117,676
|
||||
970,212
|
||||
554,126
|
||||
758,464
|
||||
190,439
|
||||
316,15
|
||||
294,724
|
||||
760,357
|
||||
1231,49
|
||||
107,780
|
||||
465,200
|
||||
370,471
|
||||
393,508
|
||||
1056,49
|
||||
934,870
|
||||
336,781
|
||||
1081,891
|
||||
13,249
|
||||
1238,221
|
||||
682,693
|
||||
114,294
|
||||
1133,19
|
||||
602,582
|
||||
31,546
|
||||
616,68
|
||||
624,817
|
||||
478,10
|
||||
735,49
|
||||
490,889
|
||||
1295,110
|
||||
637,366
|
||||
1295,784
|
||||
162,218
|
||||
790,150
|
||||
520,501
|
||||
425,651
|
||||
416,852
|
||||
1279,796
|
||||
867,840
|
||||
162,889
|
||||
803,75
|
||||
477,19
|
||||
443,502
|
||||
55,544
|
||||
410,627
|
||||
410,715
|
||||
880,795
|
||||
186,189
|
||||
79,397
|
||||
1066,362
|
||||
549,297
|
||||
1124,413
|
||||
815,831
|
||||
391,742
|
||||
616,715
|
||||
247,441
|
||||
935,259
|
||||
974,113
|
||||
1097,546
|
||||
518,201
|
||||
335,14
|
||||
266,11
|
||||
418,36
|
||||
269,603
|
||||
846,455
|
||||
867,54
|
||||
863,138
|
||||
52,666
|
||||
490,376
|
||||
899,441
|
||||
1034,465
|
||||
1108,376
|
||||
840,107
|
||||
468,600
|
||||
186,801
|
||||
949,301
|
||||
256,620
|
||||
267,84
|
||||
818,218
|
||||
507,448
|
||||
328,770
|
||||
1081,443
|
||||
1151,831
|
||||
458,394
|
||||
266,65
|
||||
130,579
|
||||
557,203
|
||||
190,201
|
||||
934,651
|
||||
1196,698
|
||||
316,155
|
||||
171,572
|
||||
535,530
|
||||
753,443
|
||||
801,684
|
||||
932,465
|
||||
1255,544
|
||||
632,742
|
||||
294,164
|
||||
820,5
|
||||
28,110
|
||||
788,514
|
||||
320,833
|
||||
1017,614
|
||||
1044,65
|
||||
1310,222
|
||||
1293,80
|
||||
656,124
|
||||
316,17
|
||||
293,593
|
||||
212,894
|
||||
892,379
|
||||
641,429
|
||||
566,371
|
||||
957,256
|
||||
687,124
|
||||
344,658
|
||||
304,600
|
||||
300,245
|
||||
328,681
|
||||
994,291
|
||||
721,219
|
||||
276,632
|
||||
908,789
|
||||
714,544
|
||||
1191,532
|
||||
929,231
|
||||
676,675
|
||||
852,70
|
||||
927,350
|
||||
882,544
|
||||
244,420
|
||||
1136,709
|
||||
1009,143
|
||||
964,126
|
||||
912,106
|
||||
1180,544
|
||||
1210,847
|
||||
1176,281
|
||||
1102,355
|
||||
1200,228
|
||||
552,878
|
||||
788,626
|
||||
8,565
|
||||
967,339
|
||||
44,399
|
||||
927,96
|
||||
792,245
|
||||
22,294
|
||||
477,697
|
||||
1118,878
|
||||
974,815
|
||||
316,330
|
||||
136,21
|
||||
468,93
|
||||
1297,136
|
||||
835,292
|
||||
507,292
|
||||
679,515
|
||||
208,651
|
||||
398,106
|
||||
1252,129
|
||||
960,152
|
||||
587,386
|
||||
502,82
|
||||
445,228
|
||||
1072,42
|
||||
1051,857
|
||||
647,462
|
||||
919,21
|
||||
114,698
|
||||
8,49
|
||||
698,442
|
||||
242,705
|
||||
694,68
|
||||
994,330
|
||||
0,661
|
||||
1174,887
|
||||
498,523
|
||||
815,63
|
||||
530,518
|
||||
1161,197
|
||||
1192,411
|
||||
192,617
|
||||
972,463
|
||||
1196,294
|
||||
55,256
|
||||
134,822
|
||||
146,665
|
||||
426,514
|
||||
639,94
|
||||
547,73
|
||||
1144,189
|
||||
1146,336
|
||||
196,192
|
||||
636,875
|
||||
758,8
|
||||
1215,472
|
||||
408,704
|
||||
520,764
|
||||
833,427
|
||||
972,291
|
||||
1200,540
|
||||
629,807
|
||||
900,250
|
||||
190,649
|
||||
802,341
|
||||
954,802
|
||||
622,488
|
||||
1223,81
|
||||
1215,422
|
||||
231,689
|
||||
177,19
|
||||
1251,756
|
||||
90,173
|
||||
311,784
|
||||
0,670
|
||||
974,781
|
||||
1049,508
|
||||
149,473
|
||||
923,362
|
||||
273,231
|
||||
646,329
|
||||
505,421
|
||||
1064,480
|
||||
293,369
|
||||
239,701
|
||||
952,290
|
||||
642,598
|
||||
48,616
|
||||
1154,551
|
||||
736,338
|
||||
589,795
|
||||
115,803
|
||||
1238,133
|
||||
994,794
|
||||
981,565
|
||||
987,875
|
||||
1268,136
|
||||
418,858
|
||||
1044,323
|
||||
808,319
|
||||
152,767
|
||||
793,620
|
||||
919,425
|
||||
790,501
|
||||
1043,362
|
||||
110,540
|
||||
616,292
|
||||
1054,620
|
||||
520,277
|
||||
599,798
|
||||
1052,836
|
||||
836,217
|
||||
659,47
|
||||
887,434
|
||||
417,693
|
||||
1108,32
|
||||
393,771
|
||||
42,136
|
||||
1091,880
|
||||
146,546
|
||||
48,406
|
||||
666,17
|
||||
331,399
|
||||
338,291
|
||||
336,403
|
||||
1064,644
|
||||
164,441
|
||||
359,546
|
||||
628,357
|
||||
616,112
|
||||
154,774
|
||||
171,322
|
||||
385,735
|
||||
843,652
|
||||
28,784
|
||||
1173,304
|
||||
930,312
|
||||
1073,273
|
||||
899,273
|
||||
0,672
|
||||
636,539
|
||||
266,571
|
||||
992,591
|
||||
709,551
|
||||
192,29
|
||||
775,362
|
||||
485,37
|
||||
313,730
|
||||
994,15
|
||||
651,47
|
||||
970,682
|
||||
254,78
|
||||
1064,414
|
||||
552,886
|
||||
313,205
|
||||
858,267
|
||||
130,138
|
||||
687,796
|
||||
112,281
|
||||
1310,770
|
||||
107,114
|
||||
1300,794
|
||||
644,346
|
||||
1208,571
|
||||
793,416
|
||||
673,366
|
||||
832,884
|
||||
1074,484
|
||||
962,708
|
||||
1206,537
|
||||
1136,642
|
||||
552,323
|
||||
721,99
|
||||
359,348
|
||||
676,708
|
||||
154,313
|
||||
1148,218
|
||||
499,385
|
||||
30,66
|
||||
678,600
|
||||
1028,635
|
||||
502,689
|
||||
932,17
|
||||
1305,709
|
||||
1076,508
|
||||
435,143
|
||||
1191,362
|
||||
708,529
|
||||
1041,603
|
||||
|
||||
fold along x=655
|
||||
fold along y=447
|
||||
fold along x=327
|
||||
fold along y=223
|
||||
fold along x=163
|
||||
fold along y=111
|
||||
fold along x=81
|
||||
fold along y=55
|
||||
fold along x=40
|
||||
fold along y=27
|
||||
fold along y=13
|
||||
fold along y=6
|
||||
70
day-14/README.md
Normal file
70
day-14/README.md
Normal file
@@ -0,0 +1,70 @@
|
||||
# Day 14: Extended Polymerization
|
||||
|
||||
[https://adventofcode.com/2021/day/14](https://adventofcode.com/2021/day/14)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has [polymerization](https://en.wikipedia.org/wiki/Polymerization) equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities.
|
||||
|
||||
The submarine manual contains <span title="HO
|
||||
|
||||
HO -> OH">instructions</span> for finding the optimal polymer formula; specifically, it offers a _polymer template_ and a list of _pair insertion_ rules (your puzzle input). You just need to work out what polymer would result after repeating the pair insertion process a few times.
|
||||
|
||||
For example:
|
||||
|
||||
NNCB
|
||||
|
||||
CH -> B
|
||||
HH -> N
|
||||
CB -> H
|
||||
NH -> C
|
||||
HB -> C
|
||||
HC -> B
|
||||
HN -> C
|
||||
NN -> C
|
||||
BH -> H
|
||||
NC -> B
|
||||
NB -> B
|
||||
BN -> B
|
||||
BB -> N
|
||||
BC -> B
|
||||
CC -> N
|
||||
CN -> C
|
||||
|
||||
|
||||
The first line is the _polymer template_ - this is the starting point of the process.
|
||||
|
||||
The following section defines the _pair insertion_ rules. A rule like `AB -> C` means that when elements `A` and `B` are immediately adjacent, element `C` should be inserted between them. These insertions all happen simultaneously.
|
||||
|
||||
So, starting with the polymer template `NNCB`, the first step simultaneously considers all three pairs:
|
||||
|
||||
* The first pair (`NN`) matches the rule `NN -> C`, so element _`C`_ is inserted between the first `N` and the second `N`.
|
||||
* The second pair (`NC`) matches the rule `NC -> B`, so element _`B`_ is inserted between the `N` and the `C`.
|
||||
* The third pair (`CB`) matches the rule `CB -> H`, so element _`H`_ is inserted between the `C` and the `B`.
|
||||
|
||||
Note that these pairs overlap: the second element of one pair is the first element of the next pair. Also, because all pairs are considered simultaneously, inserted elements are not considered to be part of a pair until the next step.
|
||||
|
||||
After the first step of this process, the polymer becomes `NCNBCHB`.
|
||||
|
||||
Here are the results of a few steps using the above rules:
|
||||
|
||||
Template: NNCB
|
||||
After step 1: NCNBCHB
|
||||
After step 2: NBCCNBBBCBHCB
|
||||
After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB
|
||||
After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB
|
||||
|
||||
|
||||
This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After step 10, `B` occurs 1749 times, `C` occurs 298 times, `H` occurs 161 times, and `N` occurs 865 times; taking the quantity of the most common element (`B`, 1749) and subtracting the quantity of the least common element (`H`, 161) produces `1749 - 161 = 1588`.
|
||||
|
||||
Apply 10 steps of pair insertion to the polymer template and find the most and least common elements in the result. _What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?_
|
||||
|
||||
### Part Two
|
||||
|
||||
The resulting polymer isn't nearly strong enough to reinforce the submarine. You'll need to run more steps of the pair insertion process; a total of _40 steps_ should do it.
|
||||
|
||||
In the above example, the most common element is `B` (occurring `2192039569602` times) and the least common element is `H` (occurring `3849876073` times); subtracting these produces _`2188189693529`_.
|
||||
|
||||
Apply _40_ steps of pair insertion to the polymer template and find the most and least common elements in the result. _What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?_
|
||||
29
day-14/day-14.py
Normal file
29
day-14/day-14.py
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from collections import Counter
|
||||
|
||||
|
||||
def solve(input, part, num_cycles):
|
||||
result = 0
|
||||
template = input[0].strip()
|
||||
rules = {k: v
|
||||
for line in input[2:] for k, v in [line.strip().split(' -> ')]}
|
||||
counts = Counter([template[i: i + 2] for i in range(len(template) - 1)])
|
||||
char_counts = Counter(template)
|
||||
for _ in range(num_cycles):
|
||||
for k, c in counts.copy().items():
|
||||
if k in rules:
|
||||
counts[k] -= c
|
||||
counts[k[0] + rules[k]] += c
|
||||
counts[rules[k] + k[1]] += c
|
||||
char_counts[rules[k]] += c
|
||||
result = char_counts.most_common(1)[0][1] - char_counts.most_common()[-1][1]
|
||||
print("Part", part, "result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
solve(input, 1, 10)
|
||||
solve(input, 2, 40)
|
||||
102
day-14/input.txt
Normal file
102
day-14/input.txt
Normal file
@@ -0,0 +1,102 @@
|
||||
NCOPHKVONVPNSKSHBNPF
|
||||
|
||||
ON -> C
|
||||
CK -> H
|
||||
HC -> B
|
||||
NP -> S
|
||||
NH -> H
|
||||
CB -> C
|
||||
BB -> H
|
||||
BC -> H
|
||||
NN -> C
|
||||
OH -> B
|
||||
SF -> V
|
||||
PB -> H
|
||||
CP -> P
|
||||
BN -> O
|
||||
NB -> B
|
||||
KB -> P
|
||||
PV -> F
|
||||
SH -> V
|
||||
KP -> S
|
||||
OF -> K
|
||||
BS -> V
|
||||
PF -> O
|
||||
BK -> S
|
||||
FB -> B
|
||||
SV -> B
|
||||
BH -> V
|
||||
VK -> N
|
||||
CS -> V
|
||||
FV -> F
|
||||
HS -> C
|
||||
KK -> O
|
||||
SP -> N
|
||||
FK -> B
|
||||
CF -> C
|
||||
HP -> F
|
||||
BF -> O
|
||||
KC -> C
|
||||
VP -> O
|
||||
BP -> P
|
||||
FF -> V
|
||||
NO -> C
|
||||
HK -> C
|
||||
HV -> B
|
||||
PK -> P
|
||||
OV -> F
|
||||
VN -> H
|
||||
PC -> K
|
||||
SB -> H
|
||||
VO -> V
|
||||
BV -> K
|
||||
NC -> H
|
||||
OB -> S
|
||||
SN -> B
|
||||
HF -> P
|
||||
VF -> B
|
||||
HN -> H
|
||||
KS -> S
|
||||
SC -> S
|
||||
CV -> B
|
||||
NS -> P
|
||||
KO -> V
|
||||
FS -> O
|
||||
PH -> K
|
||||
BO -> C
|
||||
FH -> B
|
||||
CO -> O
|
||||
FO -> F
|
||||
VV -> N
|
||||
CH -> V
|
||||
NK -> N
|
||||
PO -> K
|
||||
OK -> K
|
||||
PP -> O
|
||||
OC -> P
|
||||
FC -> N
|
||||
VH -> S
|
||||
PN -> C
|
||||
VB -> C
|
||||
VS -> P
|
||||
HO -> F
|
||||
OP -> S
|
||||
HB -> N
|
||||
CC -> K
|
||||
KN -> S
|
||||
SK -> C
|
||||
OS -> N
|
||||
KH -> B
|
||||
FP -> S
|
||||
NF -> S
|
||||
CN -> S
|
||||
KF -> C
|
||||
SS -> C
|
||||
SO -> S
|
||||
NV -> O
|
||||
FN -> B
|
||||
PS -> S
|
||||
HH -> C
|
||||
VC -> S
|
||||
OO -> C
|
||||
KV -> P
|
||||
170
day-15/README.md
Normal file
170
day-15/README.md
Normal file
@@ -0,0 +1,170 @@
|
||||
# Day 15: Chiton
|
||||
|
||||
[https://adventofcode.com/2021/day/15](https://adventofcode.com/2021/day/15)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You've almost reached the exit of the cave, but the walls are getting closer together. Your submarine can barely still fit, though; the main problem is that the walls of the cave are covered in [chitons](https://en.wikipedia.org/wiki/Chiton), and it would be best not to bump any of them.
|
||||
|
||||
The cavern is large, but has a very low ceiling, restricting your motion to two dimensions. The shape of the cavern resembles a square; a quick scan of chiton density produces a map of _risk level_ throughout the cave (your puzzle input). For example:
|
||||
|
||||
1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581
|
||||
|
||||
|
||||
You start in the top left position, your destination is the bottom right position, and you <span title="Can't go diagonal until we can repair the caterpillar unit. Could be the liquid helium or the superconductors.">cannot move diagonally</span>. The number at each position is its _risk level_; to determine the total risk of an entire path, add up the risk levels of each position you _enter_ (that is, don't count the risk level of your starting position unless you enter it; leaving it adds no risk to your total).
|
||||
|
||||
Your goal is to find a path with the _lowest total risk_. In this example, a path with the lowest total risk is highlighted here:
|
||||
|
||||
1163751742
|
||||
1381373672
|
||||
2136511328
|
||||
3694931569
|
||||
7463417111
|
||||
1319128137
|
||||
1359912421
|
||||
3125421639
|
||||
1293138521
|
||||
2311944581
|
||||
|
||||
|
||||
The total risk of this path is _`40`_ (the starting position is never entered, so its risk is not counted).
|
||||
|
||||
_What is the lowest total risk of any path from the top left to the bottom right?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Now that you know how to find low-risk paths in the cave, you can try to find your way out.
|
||||
|
||||
The entire cave is actually _five times larger in both dimensions_ than you thought; the area you originally scanned is just one tile in a 5x5 tile area that forms the full map. Your original map tile repeats to the right and downward; each time the tile repeats to the right or downward, all of its risk levels _are 1 higher_ than the tile immediately up or left of it. However, risk levels above `9` wrap back around to `1`. So, if your original map had some position with a risk level of `8`, then that same position on each of the 25 total tiles would be as follows:
|
||||
|
||||
8 9 1 2 3
|
||||
9 1 2 3 4
|
||||
1 2 3 4 5
|
||||
2 3 4 5 6
|
||||
3 4 5 6 7
|
||||
|
||||
|
||||
Each single digit above corresponds to the example position with a value of `8` on the top-left tile. Because the full map is actually five times larger in both dimensions, that position appears a total of 25 times, once in each duplicated tile, with the values shown above.
|
||||
|
||||
Here is the full five-times-as-large version of the first example above, with the original map in the top left corner highlighted:
|
||||
|
||||
11637517422274862853338597396444961841755517295286
|
||||
13813736722492484783351359589446246169155735727126
|
||||
21365113283247622439435873354154698446526571955763
|
||||
36949315694715142671582625378269373648937148475914
|
||||
74634171118574528222968563933317967414442817852555
|
||||
13191281372421239248353234135946434524615754563572
|
||||
13599124212461123532357223464346833457545794456865
|
||||
31254216394236532741534764385264587549637569865174
|
||||
12931385212314249632342535174345364628545647573965
|
||||
23119445813422155692453326671356443778246755488935
|
||||
22748628533385973964449618417555172952866628316397
|
||||
24924847833513595894462461691557357271266846838237
|
||||
32476224394358733541546984465265719557637682166874
|
||||
47151426715826253782693736489371484759148259586125
|
||||
85745282229685639333179674144428178525553928963666
|
||||
24212392483532341359464345246157545635726865674683
|
||||
24611235323572234643468334575457944568656815567976
|
||||
42365327415347643852645875496375698651748671976285
|
||||
23142496323425351743453646285456475739656758684176
|
||||
34221556924533266713564437782467554889357866599146
|
||||
33859739644496184175551729528666283163977739427418
|
||||
35135958944624616915573572712668468382377957949348
|
||||
43587335415469844652657195576376821668748793277985
|
||||
58262537826937364893714847591482595861259361697236
|
||||
96856393331796741444281785255539289636664139174777
|
||||
35323413594643452461575456357268656746837976785794
|
||||
35722346434683345754579445686568155679767926678187
|
||||
53476438526458754963756986517486719762859782187396
|
||||
34253517434536462854564757396567586841767869795287
|
||||
45332667135644377824675548893578665991468977611257
|
||||
44961841755517295286662831639777394274188841538529
|
||||
46246169155735727126684683823779579493488168151459
|
||||
54698446526571955763768216687487932779859814388196
|
||||
69373648937148475914825958612593616972361472718347
|
||||
17967414442817852555392896366641391747775241285888
|
||||
46434524615754563572686567468379767857948187896815
|
||||
46833457545794456865681556797679266781878137789298
|
||||
64587549637569865174867197628597821873961893298417
|
||||
45364628545647573965675868417678697952878971816398
|
||||
56443778246755488935786659914689776112579188722368
|
||||
55172952866628316397773942741888415385299952649631
|
||||
57357271266846838237795794934881681514599279262561
|
||||
65719557637682166874879327798598143881961925499217
|
||||
71484759148259586125936169723614727183472583829458
|
||||
28178525553928963666413917477752412858886352396999
|
||||
57545635726865674683797678579481878968159298917926
|
||||
57944568656815567976792667818781377892989248891319
|
||||
75698651748671976285978218739618932984172914319528
|
||||
56475739656758684176786979528789718163989182927419
|
||||
67554889357866599146897761125791887223681299833479
|
||||
|
||||
|
||||
Equipped with the full map, you can now find a path from the top left corner to the bottom right corner with the lowest total risk:
|
||||
|
||||
11637517422274862853338597396444961841755517295286
|
||||
13813736722492484783351359589446246169155735727126
|
||||
21365113283247622439435873354154698446526571955763
|
||||
36949315694715142671582625378269373648937148475914
|
||||
74634171118574528222968563933317967414442817852555
|
||||
13191281372421239248353234135946434524615754563572
|
||||
13599124212461123532357223464346833457545794456865
|
||||
31254216394236532741534764385264587549637569865174
|
||||
12931385212314249632342535174345364628545647573965
|
||||
23119445813422155692453326671356443778246755488935
|
||||
22748628533385973964449618417555172952866628316397
|
||||
24924847833513595894462461691557357271266846838237
|
||||
32476224394358733541546984465265719557637682166874
|
||||
47151426715826253782693736489371484759148259586125
|
||||
85745282229685639333179674144428178525553928963666
|
||||
24212392483532341359464345246157545635726865674683
|
||||
24611235323572234643468334575457944568656815567976
|
||||
42365327415347643852645875496375698651748671976285
|
||||
23142496323425351743453646285456475739656758684176
|
||||
34221556924533266713564437782467554889357866599146
|
||||
33859739644496184175551729528666283163977739427418
|
||||
35135958944624616915573572712668468382377957949348
|
||||
43587335415469844652657195576376821668748793277985
|
||||
58262537826937364893714847591482595861259361697236
|
||||
96856393331796741444281785255539289636664139174777
|
||||
35323413594643452461575456357268656746837976785794
|
||||
35722346434683345754579445686568155679767926678187
|
||||
53476438526458754963756986517486719762859782187396
|
||||
34253517434536462854564757396567586841767869795287
|
||||
45332667135644377824675548893578665991468977611257
|
||||
44961841755517295286662831639777394274188841538529
|
||||
46246169155735727126684683823779579493488168151459
|
||||
54698446526571955763768216687487932779859814388196
|
||||
69373648937148475914825958612593616972361472718347
|
||||
17967414442817852555392896366641391747775241285888
|
||||
46434524615754563572686567468379767857948187896815
|
||||
46833457545794456865681556797679266781878137789298
|
||||
64587549637569865174867197628597821873961893298417
|
||||
45364628545647573965675868417678697952878971816398
|
||||
56443778246755488935786659914689776112579188722368
|
||||
55172952866628316397773942741888415385299952649631
|
||||
57357271266846838237795794934881681514599279262561
|
||||
65719557637682166874879327798598143881961925499217
|
||||
71484759148259586125936169723614727183472583829458
|
||||
28178525553928963666413917477752412858886352396999
|
||||
57545635726865674683797678579481878968159298917926
|
||||
57944568656815567976792667818781377892989248891319
|
||||
75698651748671976285978218739618932984172914319528
|
||||
56475739656758684176786979528789718163989182927419
|
||||
67554889357866599146897761125791887223681299833479
|
||||
|
||||
|
||||
The total risk of this path is _`315`_ (the starting position is still never entered, so its risk is not counted).
|
||||
|
||||
Using the full map, _what is the lowest total risk of any path from the top left to the bottom right?_
|
||||
107
day-15/day-15.py
Normal file
107
day-15/day-15.py
Normal file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from heapq import heappush, heappop
|
||||
|
||||
|
||||
def part_1(input):
|
||||
"""The solution for Part 1 is an implementation of dijkstra algorithm. The input data is stored
|
||||
in a dictionary 'nodes' where the keys are a tuple of the coordinates and the value is the cost
|
||||
(risk level) to travel to this node.
|
||||
The total cost for each node will be stored in a dictionary named 'cost'. We start with a queue,
|
||||
that will contain all the nodes we have to check. Initially there is only the starting node
|
||||
known. The queue also contains the cost for each node to be able to prioritize them in an heapq.
|
||||
The 'done' set is used to remember the already processed nodes.
|
||||
In each iteration we take the node with lowest cost known so far from the queue and set it as
|
||||
the active node. If this node is already stored in 'done' we can skip it and start the next
|
||||
iteration immediately. Nodes can be pushed to the queue multiple times but we only need to
|
||||
process them once, hence we always use the node with the lowest cost from the queue.
|
||||
If the node hasn't been processed yet we check all of its four neighbors if they have been
|
||||
processed. If not we calculate the total cost for this node by adding the risk level to the cost
|
||||
of the active node. The total cost will then be compared to the current known cost for this new
|
||||
node. If it is lower or not yet existing the cost will be updated in the 'cost' dictionary and
|
||||
the new node will be pushed to the queue. At this point we could also push the active node to a
|
||||
dictionary containing the previous node for each node to be able to construct the path. But
|
||||
since only the cost is asked we don't store this information.
|
||||
Finally the active node will be pushed to the 'done' set and start the next iteration if there
|
||||
are elements in the queue.
|
||||
"""
|
||||
result = 0
|
||||
x_size = len(input[0].strip())
|
||||
y_size = len(input)
|
||||
nodes = {(x, y): int(v) for y, line in enumerate(input)
|
||||
for x, v in enumerate(line.strip())}
|
||||
start = (0, 0)
|
||||
end = (x_size - 1, y_size - 1)
|
||||
cost = {start: 0}
|
||||
queue = [(0, start)]
|
||||
done = set()
|
||||
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
||||
while len(queue):
|
||||
cur_cost, current = heappop(queue)
|
||||
if current in done:
|
||||
continue
|
||||
(xc, yc) = current
|
||||
next = set()
|
||||
for (xn, yn) in neighbors:
|
||||
nbr = (xc + xn, yc + yn)
|
||||
if nbr in nodes and not nbr in done:
|
||||
next.add(nbr)
|
||||
for n in next:
|
||||
next_cost = cur_cost + nodes[n]
|
||||
if not n in cost or next_cost < cost[n]:
|
||||
cost[n] = next_cost
|
||||
heappush(queue, (next_cost, n))
|
||||
done.add(current)
|
||||
result = cost[end]
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
"""The part 2 solution is essentially the same as the solution for part 1. The only difference
|
||||
is that the grid now repeats itself five times to the the right and downwards. The grid itself
|
||||
is only stored once and the new boundaries and costs for new nodes are calculated on the fly
|
||||
while the iteration across the nodes happen.
|
||||
"""
|
||||
result = 0
|
||||
x_size = len(input[0].strip())
|
||||
y_size = len(input)
|
||||
repeat = 5
|
||||
nodes = {(x, y): int(v) for y, line in enumerate(input)
|
||||
for x, v in enumerate(line.strip())}
|
||||
start = (0, 0)
|
||||
end = ((x_size * repeat) - 1, (y_size * repeat) - 1)
|
||||
cost = {start: 0}
|
||||
queue = [(0, start)]
|
||||
done = set()
|
||||
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
||||
while len(queue):
|
||||
cur_cost, current = heappop(queue)
|
||||
if current in done:
|
||||
continue
|
||||
(xc, yc) = current
|
||||
next = set()
|
||||
for (xn, yn) in neighbors:
|
||||
x = xc + xn
|
||||
y = yc + yn
|
||||
if 0 <= x < (x_size * repeat) and 0 <= y < (y_size * repeat):
|
||||
if not (x, y) in done:
|
||||
next.add((x, y))
|
||||
for (x, y) in next:
|
||||
next_node_cost = nodes[(x % x_size, y % y_size)] + \
|
||||
(x // x_size) + (y // y_size)
|
||||
if next_node_cost > 9:
|
||||
next_node_cost -= 9
|
||||
if not (x, y) in cost or (cur_cost + next_node_cost) < cost[(x, y)]:
|
||||
cost[(x, y)] = cur_cost + next_node_cost
|
||||
heappush(queue, (cur_cost + next_node_cost, (x, y)))
|
||||
done.add(current)
|
||||
result = cost[end]
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
100
day-15/input.txt
Normal file
100
day-15/input.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
4249856395422795894919869133487611581179923326874763428673979547991221931142777981153991369468629849
|
||||
5812974178739823463799939791688998895568796557798392761499941349143539572865883254186633218867928826
|
||||
3699989976298596286299499129934993241824395574879938998946914116375199242199151918863674914554714898
|
||||
5682435936794718871685718386458294198391116125679589438794914499278679393779734596558953699438589518
|
||||
7681197997388219696918569664119968498599547892968929425479817979816979144947916716989874825679487436
|
||||
9981166198272997899142698141878643123757515999788822988261499197559193945291512682763935126815448215
|
||||
8849481991861599951293183728419792414164347979985169641698899853377259811688489269959429131918919179
|
||||
3146684963669199195628973847379928251333566129941616877139631993381755512697185555441659879412994594
|
||||
3547126819874919985836685298322994247998729919239243539198191229787198622919819681997288193865811343
|
||||
9349351519955698988787949741387799819991699489867797184914918979814262987979129782223984139929928681
|
||||
8556723973779719572644889927179188961591428979299692841637999459259999966917347419979917999989799429
|
||||
3999593212698784221858498479516759888388689669747143292191959989949179914898181617598363288863529673
|
||||
5428799869888883819715648841341382669887652713779547744299157935892529712964887994849791591489974817
|
||||
4165839572399389794126943784626997119389378141921327523621283992924955951342126919499984987328936781
|
||||
5918974268979888998966997691965798988519797198467567586265175937858739845974399727152913189899879868
|
||||
9918963885878999715999979758376917418219222841971748992311279797791196671188181557777929939979528569
|
||||
8799414739499371349792499832971671794937933561999619313535199597778778121557962319178399272824346194
|
||||
8129974849671628977826913868589128781917962416787192199363828395129743811293383873718935942898484543
|
||||
3291978181898819892998112489871391737197991911931592663875989291645713999792932894185783865998135615
|
||||
9769786394195196445418935989967399989919987914119824796623857976492949168247992292316817898188699298
|
||||
9695549579996919793895372134169684298121551587731119812488691243992189122596899959128989294926989913
|
||||
9731949899392599399916551967299296355733976428888499681995484999191298894166112999923884965685649798
|
||||
1379132844766698587968789875949841895175974769991975485998386847244993897999111588556816596847214788
|
||||
9995799248928516994491995979579982981536188627577229466616615274772189816183363386951765979225171476
|
||||
8119319184142479165692338899796498149599699117488698945689444844778989694876998388519355148181719697
|
||||
3969928779117129139766611778112739189981889899228317887833728779584723699986312572133539799467285917
|
||||
2828571791973431936329179952568811618879743616915382919867751789598712817617446745199253856716911941
|
||||
7877882851638951395858496388153888963667598177681792531864174354491939941593119389999293999633917577
|
||||
1741348787717975984768578798922563489939392171468619989248936559712179995116534277937799235981547597
|
||||
8994795857858489999558911549196942878698886391795462739582595987979187765281397182149953478248998829
|
||||
8612529258832189244164522719918668148697754189678792826892293447659399729274986899475955657297984999
|
||||
7979418978468149867435687251779984869732288683757891423519948382875198942891979899776225467769271611
|
||||
3818925865589486991983914829611968896197719999391296298299521911997968172695633992575991389714563593
|
||||
9939187896519664586181929999697892324492792718919161519713861732615919297932929685698963679515326392
|
||||
9189845839678777775364697497289839263197942629799278637458319429822229529989343196991899576448897282
|
||||
9999639983298888593994869989699624152112558163996649649924665617939567974367965998835369235385845781
|
||||
8485297496991559289188422661599882518896282381858287969539917799964977429172882227811329528518187389
|
||||
9923859471976698253169673595135694645862699839964421741799449165978835991198979991891434697214818847
|
||||
9948337779794835971121969995653787569496217989663848398739231628499247383942859555527489595679787897
|
||||
9614621899899158259613995889586798538683918139138198111781714698794348571485366962113966939699643589
|
||||
1886264968913257167733217995794996991471499861875887994593877487186697874771821988536581746639569567
|
||||
9996998152929759225939499689998449399761985591467834853824558991322485728674388927966892986878819319
|
||||
9898162778195791515947756898474977179296647585999979756996981928598799139892651743929891711498489794
|
||||
2521299845948789489447298917175981779999984794295846999894374299918878915896995183978239599211199661
|
||||
3595238712479964219979978796399416285395878319927814389893184733214219892965998974989825195778431936
|
||||
2235456515733797949374988899892819815718692811711999741978953215792979895857589889485595198499933267
|
||||
1197979995691548749958849994592898117171521989543481689999898771166569678234761678582598848227891693
|
||||
6216285194657981924762962276999868669495239816738592141587889187963869774297156591399718859932485599
|
||||
4729519687582299378618117855237999797646997991843389649799259193169319238188911375949387726963679678
|
||||
9699419763993869239747599293979686119998329461424981594878199858799991786943919119924945895985515374
|
||||
5917996721981481869846954978898329942115828969756258675677679711875328179436699932532229987171169929
|
||||
7229157819969968259887444794288923399898267871298685198291217856595548347212555983111245182519289622
|
||||
9941195813129393544872257827938956788461327756591766941724841646918922553881862784891489959867389928
|
||||
8479286666256699729946931949971218817964289363968557999375893759943919891576159694877195239993489148
|
||||
5189737855542763417865693998141987771991218843271999857384919975699659994988779213888396912127939755
|
||||
5829249497186735715873851983779438136179792549877615899657346513899789927819449917872492988915513982
|
||||
6893461933919197988858287721313611812883946878115312372236989924698848988818298419696991695398125941
|
||||
6981819519985951199181939989475291951792185935616749128365399541588872686782591994295847959591392931
|
||||
7988629179489397799986811211539191291919253927863868887948841837793918643181962412676745991232121578
|
||||
5889916119249763577671931536771981885869515736589522848999798153991769949974999631378632167199887678
|
||||
9893498949812711525997272863949911589719196319429992823161587263989111998263931884594518988198746153
|
||||
6167489848782461731786755137947748897385171725523188729519395821564894154311747791864617427287926885
|
||||
1284879221869544119521638879871365523816995929979939483888749839719816469311148686844482791986117755
|
||||
4617558896994997356988295927288939978311852269226941898983267192735959576936969989579628535528783519
|
||||
5328888897937299985799899978919859582992981919818841999199494771499878669927759686618478946895944119
|
||||
6799938769339968858662394789696395769769575687756577828218986197652972952814799386756962996181892597
|
||||
9396911993551199899798629969782451326725698246878288812846813423295351841296547341338696917479175299
|
||||
8998397898488795919859127962748999922875998559197478977986192197254779199892998169999599176629148878
|
||||
1698547699914798869767429873945711593683922969939799919274119396933311939635974962783917832779918579
|
||||
8773297884249519189999647199932138888596519893944669261146627181688998288918962987398761891458879884
|
||||
5661719599479729158986561758999787569799247991581291493981747289118938848919798944839816176679395936
|
||||
9819441791329173937924877935925271972592729118224733234158273767899274195899119793821988798684414696
|
||||
1976289898791262299218117985854188291999988166561946723974852936794841371689495873895293179648555216
|
||||
9723987778468774982999879135869177783838973792981148981488979249865929898395999487197993391716979759
|
||||
1177899997181866344793594917459353978963997881815188848687916378799676376889949425869917561752649758
|
||||
9791868299666371298719945725279668482196145817963941526858953389928439799189979599987117288975594221
|
||||
9359479864494253699797539169919312739992899782984267865929872973278316996551116989646898578889798559
|
||||
2891458316689974817533795738749516416557382959989231868945595215936673184172899784699156791788186319
|
||||
3197871588419391799499768999958793419827795378639897913112989895282789729517678369821971988591168789
|
||||
3128895611274175995969789557139191499997198995898179681198268479688712943414819511197717969357964626
|
||||
8894829141789688146813983655599262175354512492811217287197518971727198974788999775881793393394664199
|
||||
6989187141894459991973787674231495975898898321231239587159329169888699482823813357919299199727591361
|
||||
5291418199993963928598629179855771924331332887583129988192175911928891699934755817621873135693191816
|
||||
9859979117149771714936925976542992919833772828551725982321627568959639165547556486133816386958767691
|
||||
7859228193989921397351284929914697967294416985799189145628697417412211943669121428893746928296129853
|
||||
9898681974164499972995857666974959138563814594959416796689799455999579812615141516926348999849199192
|
||||
3718388972799194318442969794235896693965351819955961988597649825892827718431384296684438269296581369
|
||||
6622814526191793264982158196814969278991591173839699792937584466991496599918911981477179793167467286
|
||||
9979379866996693519416698914822874331128813255572798589585921254251617789813794989743747235329888595
|
||||
1797898211694565884562549271126691989581781692389727679449696138934914999198975329972191479272399949
|
||||
8799526921898877188579896888615687919221936817161219171496896799791784217996429736635848912899357959
|
||||
3998179987191983936537841259615889646999196799579578519955782169872772733999838687699675879992385199
|
||||
4629983158488678918339598918897184289885199997966199238946856787429929894529981431432825472899789379
|
||||
2648419335213961853594831348897955886326266994947879975892927267713419578719291698994997922172724295
|
||||
4791456616384992968619169989914284879576442418663919488293715173289682167949877799818988158798898697
|
||||
8231746913997992473892883523293984991882222989389687618931693321291896977659253682566976938919319772
|
||||
8791786476374176864813358771936196161691499499725886199548984199739722733758439858858114991899439768
|
||||
7864649397847188893939876129989155438478439599981116718616269385899323813687798117158931299417619263
|
||||
1516286662577862992677589421596899929673339521868597254888585791795752635967517418681822599565577716
|
||||
6187189898988162537924787542999557639266889281629557986986981493311772812199877994518195163178939239
|
||||
132
day-16/README.md
Normal file
132
day-16/README.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# Day 16: Packet Decoder
|
||||
|
||||
[https://adventofcode.com/2021/day/16](https://adventofcode.com/2021/day/16)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
As you leave the cave and reach open waters, you receive a transmission from the Elves back on the ship.
|
||||
|
||||
The transmission was sent using the Buoyancy Interchange Transmission System (<span title="Just be glad it wasn't sent using the BuoyancY Transmission Encoding System.">BITS</span>), a method of packing numeric expressions into a binary sequence. Your submarine's computer has saved the transmission in [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) (your puzzle input).
|
||||
|
||||
The first step of decoding the message is to convert the hexadecimal representation into binary. Each character of hexadecimal corresponds to four bits of binary data:
|
||||
|
||||
0 = 0000
|
||||
1 = 0001
|
||||
2 = 0010
|
||||
3 = 0011
|
||||
4 = 0100
|
||||
5 = 0101
|
||||
6 = 0110
|
||||
7 = 0111
|
||||
8 = 1000
|
||||
9 = 1001
|
||||
A = 1010
|
||||
B = 1011
|
||||
C = 1100
|
||||
D = 1101
|
||||
E = 1110
|
||||
F = 1111
|
||||
|
||||
|
||||
The BITS transmission contains a single _packet_ at its outermost layer which itself contains many other packets. The hexadecimal representation of this packet might encode a few extra `0` bits at the end; these are not part of the transmission and should be ignored.
|
||||
|
||||
Every packet begins with a standard header: the first three bits encode the packet _version_, and the next three bits encode the packet _type ID_. These two values are numbers; all numbers encoded in any packet are represented as binary with the most significant bit first. For example, a version encoded as the binary sequence `100` represents the number `4`.
|
||||
|
||||
Packets with type ID `4` represent a _literal value_. Literal value packets encode a single binary number. To do this, the binary number is padded with leading zeroes until its length is a multiple of four bits, and then it is broken into groups of four bits. Each group is prefixed by a `1` bit except the last group, which is prefixed by a `0` bit. These groups of five bits immediately follow the packet header. For example, the hexadecimal string `D2FE28` becomes:
|
||||
|
||||
110100101111111000101000
|
||||
VVVTTTAAAAABBBBBCCCCC
|
||||
|
||||
|
||||
Below each bit is a label indicating its purpose:
|
||||
|
||||
* The three bits labeled `V` (`110`) are the packet version, `6`.
|
||||
* The three bits labeled `T` (`100`) are the packet type ID, `4`, which means the packet is a literal value.
|
||||
* The five bits labeled `A` (`10111`) start with a `1` (not the last group, keep reading) and contain the first four bits of the number, `0111`.
|
||||
* The five bits labeled `B` (`11110`) start with a `1` (not the last group, keep reading) and contain four more bits of the number, `1110`.
|
||||
* The five bits labeled `C` (`00101`) start with a `0` (last group, end of packet) and contain the last four bits of the number, `0101`.
|
||||
* The three unlabeled `0` bits at the end are extra due to the hexadecimal representation and should be ignored.
|
||||
|
||||
So, this packet represents a literal value with binary representation `011111100101`, which is `2021` in decimal.
|
||||
|
||||
Every other type of packet (any packet with a type ID other than `4`) represent an _operator_ that performs some calculation on one or more sub-packets contained within. Right now, the specific operations aren't important; focus on parsing the hierarchy of sub-packets.
|
||||
|
||||
An operator packet contains one or more packets. To indicate which subsequent binary data represents its sub-packets, an operator packet can use one of two modes indicated by the bit immediately after the packet header; this is called the _length type ID_:
|
||||
|
||||
* If the length type ID is `0`, then the next _15_ bits are a number that represents the _total length in bits_ of the sub-packets contained by this packet.
|
||||
* If the length type ID is `1`, then the next _11_ bits are a number that represents the _number of sub-packets immediately contained_ by this packet.
|
||||
|
||||
Finally, after the length type ID bit and the 15-bit or 11-bit field, the sub-packets appear.
|
||||
|
||||
For example, here is an operator packet (hexadecimal string `38006F45291200`) with length type ID `0` that contains two sub-packets:
|
||||
|
||||
00111000000000000110111101000101001010010001001000000000
|
||||
VVVTTTILLLLLLLLLLLLLLLAAAAAAAAAAABBBBBBBBBBBBBBBB
|
||||
|
||||
|
||||
* The three bits labeled `V` (`001`) are the packet version, `1`.
|
||||
* The three bits labeled `T` (`110`) are the packet type ID, `6`, which means the packet is an operator.
|
||||
* The bit labeled `I` (`0`) is the length type ID, which indicates that the length is a 15-bit number representing the number of bits in the sub-packets.
|
||||
* The 15 bits labeled `L` (`000000000011011`) contain the length of the sub-packets in bits, `27`.
|
||||
* The 11 bits labeled `A` contain the first sub-packet, a literal value representing the number `10`.
|
||||
* The 16 bits labeled `B` contain the second sub-packet, a literal value representing the number `20`.
|
||||
|
||||
After reading 11 and 16 bits of sub-packet data, the total length indicated in `L` (27) is reached, and so parsing of this packet stops.
|
||||
|
||||
As another example, here is an operator packet (hexadecimal string `EE00D40C823060`) with length type ID `1` that contains three sub-packets:
|
||||
|
||||
11101110000000001101010000001100100000100011000001100000
|
||||
VVVTTTILLLLLLLLLLLAAAAAAAAAAABBBBBBBBBBBCCCCCCCCCCC
|
||||
|
||||
|
||||
* The three bits labeled `V` (`111`) are the packet version, `7`.
|
||||
* The three bits labeled `T` (`011`) are the packet type ID, `3`, which means the packet is an operator.
|
||||
* The bit labeled `I` (`1`) is the length type ID, which indicates that the length is a 11-bit number representing the number of sub-packets.
|
||||
* The 11 bits labeled `L` (`00000000011`) contain the number of sub-packets, `3`.
|
||||
* The 11 bits labeled `A` contain the first sub-packet, a literal value representing the number `1`.
|
||||
* The 11 bits labeled `B` contain the second sub-packet, a literal value representing the number `2`.
|
||||
* The 11 bits labeled `C` contain the third sub-packet, a literal value representing the number `3`.
|
||||
|
||||
After reading 3 complete sub-packets, the number of sub-packets indicated in `L` (3) is reached, and so parsing of this packet stops.
|
||||
|
||||
For now, parse the hierarchy of the packets throughout the transmission and _add up all of the version numbers_.
|
||||
|
||||
Here are a few more examples of hexadecimal-encoded transmissions:
|
||||
|
||||
* `8A004A801A8002F478` represents an operator packet (version 4) which contains an operator packet (version 1) which contains an operator packet (version 5) which contains a literal value (version 6); this packet has a version sum of _`16`_.
|
||||
* `620080001611562C8802118E34` represents an operator packet (version 3) which contains two sub-packets; each sub-packet is an operator packet that contains two literal values. This packet has a version sum of _`12`_.
|
||||
* `C0015000016115A2E0802F182340` has the same structure as the previous example, but the outermost packet uses a different length type ID. This packet has a version sum of _`23`_.
|
||||
* `A0016C880162017C3686B18A3D4780` is an operator packet that contains an operator packet that contains an operator packet that contains five literal values; it has a version sum of _`31`_.
|
||||
|
||||
Decode the structure of your hexadecimal-encoded BITS transmission; _what do you get if you add up the version numbers in all packets?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Now that you have the structure of your transmission decoded, you can calculate the value of the expression it represents.
|
||||
|
||||
Literal values (type ID `4`) represent a single number as described above. The remaining type IDs are more interesting:
|
||||
|
||||
* Packets with type ID `0` are _sum_ packets - their value is the sum of the values of their sub-packets. If they only have a single sub-packet, their value is the value of the sub-packet.
|
||||
* Packets with type ID `1` are _product_ packets - their value is the result of multiplying together the values of their sub-packets. If they only have a single sub-packet, their value is the value of the sub-packet.
|
||||
* Packets with type ID `2` are _minimum_ packets - their value is the minimum of the values of their sub-packets.
|
||||
* Packets with type ID `3` are _maximum_ packets - their value is the maximum of the values of their sub-packets.
|
||||
* Packets with type ID `5` are _greater than_ packets - their value is _1_ if the value of the first sub-packet is greater than the value of the second sub-packet; otherwise, their value is _0_. These packets always have exactly two sub-packets.
|
||||
* Packets with type ID `6` are _less than_ packets - their value is _1_ if the value of the first sub-packet is less than the value of the second sub-packet; otherwise, their value is _0_. These packets always have exactly two sub-packets.
|
||||
* Packets with type ID `7` are _equal to_ packets - their value is _1_ if the value of the first sub-packet is equal to the value of the second sub-packet; otherwise, their value is _0_. These packets always have exactly two sub-packets.
|
||||
|
||||
Using these rules, you can now work out the value of the outermost packet in your BITS transmission.
|
||||
|
||||
For example:
|
||||
|
||||
* `C200B40A82` finds the sum of `1` and `2`, resulting in the value _`3`_.
|
||||
* `04005AC33890` finds the product of `6` and `9`, resulting in the value _`54`_.
|
||||
* `880086C3E88112` finds the minimum of `7`, `8`, and `9`, resulting in the value _`7`_.
|
||||
* `CE00C43D881120` finds the maximum of `7`, `8`, and `9`, resulting in the value _`9`_.
|
||||
* `D8005AC2A8F0` produces `1`, because `5` is less than `15`.
|
||||
* `F600BC2D8F` produces `0`, because `5` is not greater than `15`.
|
||||
* `9C005AC2F8F0` produces `0`, because `5` is not equal to `15`.
|
||||
* `9C0141080250320F1802104A08` produces `1`, because `1` + `3` = `2` \* `2`.
|
||||
|
||||
_What do you get if you evaluate the expression represented by your hexadecimal-encoded BITS transmission?_
|
||||
109
day-16/day-16.py
Normal file
109
day-16/day-16.py
Normal file
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from math import prod
|
||||
|
||||
|
||||
def get_next_packet(msg, idx, packets, res=0):
|
||||
"""The function is called recursively and will read a single packet starting at index 'idx' in
|
||||
'msg'. The packet will be appended to the 'packets' list. For each packet the type_id and a
|
||||
payload is stored. The payload is a literal for packets of type 4 or will be filled with
|
||||
subsequent packages for other types by recursion of this function. The version will be added to
|
||||
the result.
|
||||
At the end the updated index and result will be returned.
|
||||
"""
|
||||
if (idx+11) >= len(msg):
|
||||
idx = -1
|
||||
return idx, res
|
||||
version = int(msg[idx:idx + 3], 2)
|
||||
idx += 3
|
||||
res += version
|
||||
type_id = int(msg[idx:idx + 3], 2)
|
||||
idx += 3
|
||||
payload = None
|
||||
match type_id:
|
||||
case 4:
|
||||
end = False
|
||||
literal = 0
|
||||
while not end:
|
||||
if not int(msg[idx:idx + 1], 2):
|
||||
end = True
|
||||
idx += 1
|
||||
literal = (literal << 4) | int(msg[idx:idx + 4], 2)
|
||||
idx += 4
|
||||
payload = literal
|
||||
case _:
|
||||
length_id = int(msg[idx:idx + 1], 2)
|
||||
idx += 1
|
||||
length = 0
|
||||
payload = []
|
||||
if length_id:
|
||||
length = int(msg[idx:idx + 11], 2)
|
||||
idx += 11
|
||||
for _ in range(length):
|
||||
idx, res = get_next_packet(msg, idx, payload, res)
|
||||
else:
|
||||
length = int(msg[idx:idx + 15], 2)
|
||||
idx += 15
|
||||
next_idx = idx + length
|
||||
while idx < next_idx:
|
||||
idx, res = get_next_packet(msg, idx, payload, res)
|
||||
packets.append({'type_id': type_id, 'payload': payload})
|
||||
return idx, res
|
||||
|
||||
|
||||
def calc_result(packets):
|
||||
"""The function recursively steps through the packets and performs the defined calculations."""
|
||||
res = 0
|
||||
for p in packets:
|
||||
match p['type_id']:
|
||||
case 4:
|
||||
res = p['payload']
|
||||
case _:
|
||||
sub_packets = []
|
||||
for i in range(len(p['payload'])):
|
||||
sub_packets.append(calc_result([p['payload'][i]]))
|
||||
match p['type_id']:
|
||||
case 0:
|
||||
res = sum(sub_packets)
|
||||
case 1:
|
||||
res = prod(sub_packets)
|
||||
case 2:
|
||||
res = min(sub_packets)
|
||||
case 3:
|
||||
res = max(sub_packets)
|
||||
case 5:
|
||||
res = 1 if sub_packets[0] > sub_packets[1] else 0
|
||||
case 6:
|
||||
res = 1 if sub_packets[0] < sub_packets[1] else 0
|
||||
case 7:
|
||||
res = 1 if sub_packets[0] == sub_packets[1] else 0
|
||||
return res
|
||||
|
||||
|
||||
def solve(input):
|
||||
"""Part 1 and part 2 are solved in one go so we don't have to parse the bitstream twice. We
|
||||
start by stripping newline and zeros at the end of the input line, convert it to an integer
|
||||
with base 16 (hexadecimal) and back to a binary string. The bin() function adds '0b' at the
|
||||
beginning so we only store te actual binary data. Afterwards we need to add zeros in the
|
||||
beginning until the binary string has a length of four times the hexadecimal representation
|
||||
(Each hex digit is four binary digits).
|
||||
"""
|
||||
result_p1 = 0
|
||||
line = input[0].rstrip().strip('0')
|
||||
msg = bin(int(line, 16))[2:]
|
||||
while len(msg) < len(line) * 4:
|
||||
msg = '0' + msg
|
||||
packets = []
|
||||
next = 0
|
||||
while -1 != next:
|
||||
next, result_p1 = get_next_packet(msg, next, packets, result_p1)
|
||||
result_p2 = calc_result(packets)
|
||||
print("Part 1 result:", result_p1)
|
||||
print("Part 2 result:", result_p2)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
solve(input)
|
||||
1
day-16/input.txt
Normal file
1
day-16/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
E058F79802FA00A4C1C496E5C738D860094BDF5F3ED004277DD87BB36C8EA800BDC3891D4AFA212012B64FE21801AB80021712E3CC771006A3E47B8811E4C01900043A1D41686E200DC4B8DB06C001098411C22B30085B2D6B743A6277CF719B28C9EA11AEABB6D200C9E6C6F801F493C7FE13278FFC26467C869BC802839E489C19934D935C984B88460085002F931F7D978740668A8C0139279C00D40401E8D1082318002111CE0F460500BE462F3350CD20AF339A7BB4599DA7B755B9E6B6007D25E87F3D2977543F00016A2DCB029009193D6842A754015CCAF652D6609D2F1EE27B28200C0A4B1DFCC9AC0109F82C4FC17880485E00D4C0010F8D110E118803F0DA1845A932B82E200D41E94AD7977699FED38C0169DD53B986BEE7E00A49A2CE554A73D5A6ED2F64B4804419508B00584019877142180803715224C613009E795E58FA45EA7C04C012D004E7E3FE64C27E3FE64C24FA5D331CFB024E0064DEEB49D0CC401A2004363AC6C8344008641B8351B08010882917E3D1801D2C7CA0124AE32DD3DDE86CF52BBFAAC2420099AC01496269FD65FA583A5A9ECD781A20094CE10A73F5F4EB450200D326D270021A9F8A349F7F897E85A4020CF802F238AEAA8D22D1397BF27A97FD220898600C4926CBAFCD1180087738FD353ECB7FDE94A6FBCAA0C3794875708032D8A1A0084AE378B994AE378B9A8007CD370A6F36C17C9BFCAEF18A73B2028C0A004CBC7D695773FAF1006E52539D2CFD800D24B577E1398C259802D3D23AB00540010A8611260D0002130D23645D3004A6791F22D802931FA4E46B31FA4E4686004A8014805AE0801AC050C38010600580109EC03CC200DD40031F100B166005200898A00690061860072801CE007B001573B5493004248EA553E462EC401A64EE2F6C7E23740094C952AFF031401A95A7192475CACF5E3F988E29627600E724DBA14CBE710C2C4E72302C91D12B0063F2BBFFC6A586A763B89C4DC9A0
|
||||
142
day-17/README.md
Normal file
142
day-17/README.md
Normal file
@@ -0,0 +1,142 @@
|
||||
# Day 17: Trick Shot
|
||||
|
||||
[https://adventofcode.com/2021/day/17](https://adventofcode.com/2021/day/17)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You finally decode the Elves' message. `<span title="Maybe you need to turn the message 90 degrees counterclockwise?">HI</span>`, the message says. You continue searching for the sleigh keys.
|
||||
|
||||
Ahead of you is what appears to be a large [ocean trench](https://en.wikipedia.org/wiki/Oceanic_trench). Could the keys have fallen into it? You'd better send a probe to investigate.
|
||||
|
||||
The probe launcher on your submarine can fire the probe with any [integer](https://en.wikipedia.org/wiki/Integer) velocity in the `x` (forward) and `y` (upward, or downward if negative) directions. For example, an initial `x,y` velocity like `0,10` would fire the probe straight up, while an initial velocity like `10,-1` would fire the probe forward at a slight downward angle.
|
||||
|
||||
The probe's `x,y` position starts at `0,0`. Then, it will follow some trajectory by moving in _steps_. On each step, these changes occur in the following order:
|
||||
|
||||
* The probe's `x` position increases by its `x` velocity.
|
||||
* The probe's `y` position increases by its `y` velocity.
|
||||
* Due to drag, the probe's `x` velocity changes by `1` toward the value `0`; that is, it decreases by `1` if it is greater than `0`, increases by `1` if it is less than `0`, or does not change if it is already `0`.
|
||||
* Due to gravity, the probe's `y` velocity decreases by `1`.
|
||||
|
||||
For the probe to successfully make it into the trench, the probe must be on some trajectory that causes it to be within a _target area_ after any step. The submarine computer has already calculated this target area (your puzzle input). For example:
|
||||
|
||||
target area: x=20..30, y=-10..-5
|
||||
|
||||
This target area means that you need to find initial `x,y` velocity values such that after any step, the probe's `x` position is at least `20` and at most `30`, _and_ the probe's `y` position is at least `-10` and at most `-5`.
|
||||
|
||||
Given this target area, one initial velocity that causes the probe to be within the target area after any step is `7,2`:
|
||||
|
||||
.............#....#............
|
||||
.......#..............#........
|
||||
...............................
|
||||
S........................#.....
|
||||
...............................
|
||||
...............................
|
||||
...........................#...
|
||||
...............................
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTT#TT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
|
||||
|
||||
In this diagram, `S` is the probe's initial position, `0,0`. The `x` coordinate increases to the right, and the `y` coordinate increases upward. In the bottom right, positions that are within the target area are shown as `T`. After each step (until the target area is reached), the position of the probe is marked with `#`. (The bottom-right `#` is both a position the probe reaches and a position in the target area.)
|
||||
|
||||
Another initial velocity that causes the probe to be within the target area after any step is `6,3`:
|
||||
|
||||
...............#..#............
|
||||
...........#........#..........
|
||||
...............................
|
||||
......#..............#.........
|
||||
...............................
|
||||
...............................
|
||||
S....................#.........
|
||||
...............................
|
||||
...............................
|
||||
...............................
|
||||
.....................#.........
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................T#TTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
|
||||
|
||||
Another one is `9,0`:
|
||||
|
||||
S........#.....................
|
||||
.................#.............
|
||||
...............................
|
||||
........................#......
|
||||
...............................
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTT#
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
....................TTTTTTTTTTT
|
||||
|
||||
|
||||
One initial velocity that _doesn't_ cause the probe to be within the target area after any step is `17,-4`:
|
||||
|
||||
S..............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
.................#.............................................
|
||||
....................TTTTTTTTTTT................................
|
||||
....................TTTTTTTTTTT................................
|
||||
....................TTTTTTTTTTT................................
|
||||
....................TTTTTTTTTTT................................
|
||||
....................TTTTTTTTTTT..#.............................
|
||||
....................TTTTTTTTTTT................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
................................................#..............
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
...............................................................
|
||||
..............................................................#
|
||||
|
||||
|
||||
The probe appears to pass through the target area, but is never within it after any step. Instead, it continues down and to the right - only the first few steps are shown.
|
||||
|
||||
If you're going to fire a highly scientific probe out of a super cool probe launcher, you might as well do it with _style_. How high can you make the probe go while still reaching the target area?
|
||||
|
||||
In the above example, using an initial velocity of `6,9` is the best you can do, causing the probe to reach a maximum `y` position of _`45`_. (Any higher initial `y` velocity causes the probe to overshoot the target area entirely.)
|
||||
|
||||
Find the initial velocity that causes the probe to reach the highest `y` position and still eventually be within the target area after any step. _What is the highest `y` position it reaches on this trajectory?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Maybe a fancy trick shot isn't the best idea; after all, you only have one probe, so you had better not miss.
|
||||
|
||||
To get the best idea of what your options are for launching the probe, you need to find _every initial velocity_ that causes the probe to eventually be within the target area after any step.
|
||||
|
||||
In the above example, there are _`112`_ different initial velocity values that meet these criteria:
|
||||
|
||||
23,-10 25,-9 27,-5 29,-6 22,-6 21,-7 9,0 27,-7 24,-5
|
||||
25,-7 26,-6 25,-5 6,8 11,-2 20,-5 29,-10 6,3 28,-7
|
||||
8,0 30,-6 29,-8 20,-10 6,7 6,4 6,1 14,-4 21,-6
|
||||
26,-10 7,-1 7,7 8,-1 21,-9 6,2 20,-7 30,-10 14,-3
|
||||
20,-8 13,-2 7,3 28,-8 29,-9 15,-3 22,-5 26,-8 25,-8
|
||||
25,-6 15,-4 9,-2 15,-2 12,-2 28,-9 12,-3 24,-6 23,-7
|
||||
25,-10 7,8 11,-3 26,-7 7,1 23,-9 6,0 22,-10 27,-6
|
||||
8,1 22,-8 13,-4 7,6 28,-6 11,-4 12,-4 26,-9 7,4
|
||||
24,-10 23,-8 30,-8 7,0 9,-1 10,-1 26,-5 22,-9 6,5
|
||||
7,5 23,-6 28,-10 10,-2 11,-1 20,-9 14,-2 29,-7 13,-3
|
||||
23,-5 24,-8 27,-9 30,-7 28,-5 21,-10 7,9 6,6 21,-5
|
||||
27,-10 7,2 30,-9 21,-8 22,-7 24,-9 20,-6 6,9 29,-5
|
||||
8,-2 27,-8 30,-5 24,-7
|
||||
|
||||
|
||||
_How many distinct initial velocity values cause the probe to be within the target area after any step?_
|
||||
55
day-17/day-17.py
Normal file
55
day-17/day-17.py
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def part_1(input):
|
||||
"""The max height is reached by using the maximum start y velocity that doesn't overshoot the
|
||||
landing area. The x velocity doesn't mater here, because x position won't change after x
|
||||
velocity reaches 0. So there will always be an vx that drops straight between the x boundaries.
|
||||
Because the y velocity decreases linear we will reach our starting height after the velocity
|
||||
changed exactly to the negative value of our starting velocity. At this point we have to make
|
||||
sure not to overhoot the landing area, so the next velocity needs to be the same value as the
|
||||
lowest coordinate of the landing area to be able to start with the highest possible starting
|
||||
velocity. This leaves us with a starting velocity of abs(y_min + 1) to reach the maximum
|
||||
possible height. For positive landing coordinates y_max has to be used.
|
||||
"""
|
||||
result = 0
|
||||
_, y = input[0].rstrip().replace('target area: ', '').replace(
|
||||
'x=', '').replace('y=', '').split(', ')
|
||||
y_min, _ = [int(c) for c in y.split('..')]
|
||||
vy0 = abs(y_min + 1)
|
||||
result = vy0 * (vy0 + 1) // 2
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
x, y = input[0].rstrip().replace('target area: ', '').replace(
|
||||
'x=', '').replace('y=', '').split(', ')
|
||||
x_min, x_max = [int(c) for c in x.split('..')]
|
||||
y_min, y_max = [int(c) for c in y.split('..')]
|
||||
for vx_0 in range(x_max):
|
||||
for vy_0 in range(y_min, -y_min):
|
||||
vx = vx_0
|
||||
vy = vy_0
|
||||
x_pos = 0
|
||||
y_pos = 0
|
||||
while x_pos < x_max and y_pos > y_min:
|
||||
x_pos += vx
|
||||
y_pos += vy
|
||||
if vx != 0:
|
||||
vx -= 1
|
||||
vy -= 1
|
||||
if x_min <= x_pos <= x_max and \
|
||||
y_min <= y_pos <= y_max:
|
||||
result += 1
|
||||
break
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
1
day-17/input.txt
Normal file
1
day-17/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
target area: x=25..67, y=-260..-200
|
||||
202
day-18/README.md
Normal file
202
day-18/README.md
Normal file
@@ -0,0 +1,202 @@
|
||||
# Day 18: Snailfish
|
||||
|
||||
[https://adventofcode.com/2021/day/18](https://adventofcode.com/2021/day/18)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
You descend into the ocean trench and encounter some [snailfish](https://en.wikipedia.org/wiki/Snailfish). They say they saw the sleigh keys! They'll even tell you which direction the keys went if you help one of the smaller snailfish with his _<span title="Or 'maths', if you have more than one.">math</span> homework_.
|
||||
|
||||
Snailfish numbers aren't like regular numbers. Instead, every snailfish number is a _pair_ - an ordered list of two elements. Each element of the pair can be either a regular number or another pair.
|
||||
|
||||
Pairs are written as `[x,y]`, where `x` and `y` are the elements within the pair. Here are some example snailfish numbers, one snailfish number per line:
|
||||
|
||||
[1,2]
|
||||
[[1,2],3]
|
||||
[9,[8,7]]
|
||||
[[1,9],[8,5]]
|
||||
[[[[1,2],[3,4]],[[5,6],[7,8]]],9]
|
||||
[[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]]
|
||||
[[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]]
|
||||
|
||||
|
||||
This snailfish homework is about _addition_. To add two snailfish numbers, form a pair from the left and right parameters of the addition operator. For example, `[1,2]` + `[[3,4],5]` becomes `[[1,2],[[3,4],5]]`.
|
||||
|
||||
There's only one problem: _snailfish numbers must always be reduced_, and the process of adding two snailfish numbers can result in snailfish numbers that need to be reduced.
|
||||
|
||||
To _reduce a snailfish number_, you must repeatedly do the first action in this list that applies to the snailfish number:
|
||||
|
||||
* If any pair is _nested inside four pairs_, the leftmost such pair _explodes_.
|
||||
* If any regular number is _10 or greater_, the leftmost such regular number _splits_.
|
||||
|
||||
Once no action in the above list applies, the snailfish number is reduced.
|
||||
|
||||
During reduction, at most one action applies, after which the process returns to the top of the list of actions. For example, if _split_ produces a pair that meets the _explode_ criteria, that pair _explodes_ before other _splits_ occur.
|
||||
|
||||
To _explode_ a pair, the pair's left value is added to the first regular number to the left of the exploding pair (if any), and the pair's right value is added to the first regular number to the right of the exploding pair (if any). Exploding pairs will always consist of two regular numbers. Then, the entire exploding pair is replaced with the regular number `0`.
|
||||
|
||||
Here are some examples of a single explode action:
|
||||
|
||||
* `[[[[[9,8],1],2],3],4]` becomes `[[[[0,9],2],3],4]` (the `9` has no regular number to its left, so it is not added to any regular number).
|
||||
* `[7,[6,[5,[4,[3,2]]]]]` becomes `[7,[6,[5,[7,0]]]]` (the `2` has no regular number to its right, and so it is not added to any regular number).
|
||||
* `[[6,[5,[4,[3,2]]]],1]` becomes `[[6,[5,[7,0]]],3]`.
|
||||
* `[[3,[2,[1,[7,3]]]],[6,[5,[4,[3,2]]]]]` becomes `[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]` (the pair `[3,2]` is unaffected because the pair `[7,3]` is further to the left; `[3,2]` would explode on the next action).
|
||||
* `[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]` becomes `[[3,[2,[8,0]]],[9,[5,[7,0]]]]`.
|
||||
|
||||
To _split_ a regular number, replace it with a pair; the left element of the pair should be the regular number divided by two and rounded _down_, while the right element of the pair should be the regular number divided by two and rounded _up_. For example, `10` becomes `[5,5]`, `11` becomes `[5,6]`, `12` becomes `[6,6]`, and so on.
|
||||
|
||||
Here is the process of finding the reduced result of `[[[[4,3],4],4],[7,[[8,4],9]]]` + `[1,1]`:
|
||||
|
||||
after addition: [[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]
|
||||
after explode: [[[[0,7],4],[7,[[8,4],9]]],[1,1]]
|
||||
after explode: [[[[0,7],4],[15,[0,13]]],[1,1]]
|
||||
after split: [[[[0,7],4],[[7,8],[0,13]]],[1,1]]
|
||||
after split: [[[[0,7],4],[[7,8],[0,[6,7]]]],[1,1]]
|
||||
after explode: [[[[0,7],4],[[7,8],[6,0]]],[8,1]]
|
||||
|
||||
|
||||
Once no reduce actions apply, the snailfish number that remains is the actual result of the addition operation: `[[[[0,7],4],[[7,8],[6,0]]],[8,1]]`.
|
||||
|
||||
The homework assignment involves adding up a _list of snailfish numbers_ (your puzzle input). The snailfish numbers are each listed on a separate line. Add the first snailfish number and the second, then add that result and the third, then add that result and the fourth, and so on until all numbers in the list have been used once.
|
||||
|
||||
For example, the final sum of this list is `[[[[1,1],[2,2]],[3,3]],[4,4]]`:
|
||||
|
||||
[1,1]
|
||||
[2,2]
|
||||
[3,3]
|
||||
[4,4]
|
||||
|
||||
|
||||
The final sum of this list is `[[[[3,0],[5,3]],[4,4]],[5,5]]`:
|
||||
|
||||
[1,1]
|
||||
[2,2]
|
||||
[3,3]
|
||||
[4,4]
|
||||
[5,5]
|
||||
|
||||
|
||||
The final sum of this list is `[[[[5,0],[7,4]],[5,5]],[6,6]]`:
|
||||
|
||||
[1,1]
|
||||
[2,2]
|
||||
[3,3]
|
||||
[4,4]
|
||||
[5,5]
|
||||
[6,6]
|
||||
|
||||
|
||||
Here's a slightly larger example:
|
||||
|
||||
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
|
||||
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
|
||||
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
|
||||
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
|
||||
[7,[5,[[3,8],[1,4]]]]
|
||||
[[2,[2,2]],[8,[8,1]]]
|
||||
[2,9]
|
||||
[1,[[[9,3],9],[[9,0],[0,7]]]]
|
||||
[[[5,[7,4]],7],1]
|
||||
[[[[4,2],2],6],[8,7]]
|
||||
|
||||
|
||||
The final sum `[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]` is found after adding up the above snailfish numbers:
|
||||
|
||||
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
|
||||
+ [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
|
||||
= [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
|
||||
|
||||
[[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
|
||||
+ [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
|
||||
= [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
|
||||
|
||||
[[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
|
||||
+ [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
|
||||
= [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
|
||||
|
||||
[[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
|
||||
+ [7,[5,[[3,8],[1,4]]]]
|
||||
= [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
|
||||
|
||||
[[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
|
||||
+ [[2,[2,2]],[8,[8,1]]]
|
||||
= [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
|
||||
|
||||
[[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
|
||||
+ [2,9]
|
||||
= [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
|
||||
|
||||
[[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
|
||||
+ [1,[[[9,3],9],[[9,0],[0,7]]]]
|
||||
= [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
|
||||
|
||||
[[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
|
||||
+ [[[5,[7,4]],7],1]
|
||||
= [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
|
||||
|
||||
[[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
|
||||
+ [[[[4,2],2],6],[8,7]]
|
||||
= [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]
|
||||
|
||||
|
||||
To check whether it's the right answer, the snailfish teacher only checks the _magnitude_ of the final sum. The magnitude of a pair is 3 times the magnitude of its left element plus 2 times the magnitude of its right element. The magnitude of a regular number is just that number.
|
||||
|
||||
For example, the magnitude of `[9,1]` is `3*9 + 2*1 = 29`; the magnitude of `[1,9]` is `3*1 + 2*9 = 21`. Magnitude calculations are recursive: the magnitude of `[[9,1],[1,9]]` is `3*29 + 2*21 = 129`.
|
||||
|
||||
Here are a few more magnitude examples:
|
||||
|
||||
* `[[1,2],[[3,4],5]]` becomes _`143`_.
|
||||
* `[[[[0,7],4],[[7,8],[6,0]]],[8,1]]` becomes _`1384`_.
|
||||
* `[[[[1,1],[2,2]],[3,3]],[4,4]]` becomes _`445`_.
|
||||
* `[[[[3,0],[5,3]],[4,4]],[5,5]]` becomes _`791`_.
|
||||
* `[[[[5,0],[7,4]],[5,5]],[6,6]]` becomes _`1137`_.
|
||||
* `[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]` becomes _`3488`_.
|
||||
|
||||
So, given this example homework assignment:
|
||||
|
||||
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
|
||||
[[[5,[2,8]],4],[5,[[9,9],0]]]
|
||||
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
|
||||
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
|
||||
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
|
||||
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
|
||||
[[[[5,4],[7,7]],8],[[8,3],8]]
|
||||
[[9,3],[[9,9],[6,[4,9]]]]
|
||||
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
|
||||
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
|
||||
|
||||
|
||||
The final sum is:
|
||||
|
||||
[[[[6,6],[7,6]],[[7,7],[7,0]]],[[[7,7],[7,7]],[[7,8],[9,9]]]]
|
||||
|
||||
The magnitude of this final sum is _`4140`_.
|
||||
|
||||
Add up all of the snailfish numbers from the homework assignment in the order they appear. _What is the magnitude of the final sum?_
|
||||
|
||||
### Part Two
|
||||
|
||||
You notice a second question on the back of the homework assignment:
|
||||
|
||||
What is the largest magnitude you can get from adding only two of the snailfish numbers?
|
||||
|
||||
Note that snailfish addition is not [commutative](https://en.wikipedia.org/wiki/Commutative_property) - that is, `x + y` and `y + x` can produce different results.
|
||||
|
||||
Again considering the last example homework assignment above:
|
||||
|
||||
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
|
||||
[[[5,[2,8]],4],[5,[[9,9],0]]]
|
||||
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
|
||||
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
|
||||
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
|
||||
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
|
||||
[[[[5,4],[7,7]],8],[[8,3],8]]
|
||||
[[9,3],[[9,9],[6,[4,9]]]]
|
||||
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
|
||||
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
|
||||
|
||||
|
||||
The largest magnitude of the sum of any two snailfish numbers in this list is _`3993`_. This is the magnitude of `[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]` + `[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]`, which reduces to `[[[[7,8],[6,6]],[[6,0],[7,7]]],[[[7,8],[8,8]],[[7,9],[0,6]]]]`.
|
||||
|
||||
_What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment?_
|
||||
168
day-18/day-18.py
Normal file
168
day-18/day-18.py
Normal file
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def parse(line):
|
||||
"""This function parses an input line to nested tuples of length 2. It works by stripping the
|
||||
outermost bracket-pair from the string and counting the 'depth' of the current brackets. After
|
||||
we got back to the initial level 0 the next comma marks the index where the left and right part
|
||||
are seperated. Those will be parsed by a recursive parse call each. If there is no comma on a
|
||||
line, we are on the deepest level of this branch and return the value as integer. Else the left
|
||||
and right return value will be returned in a tuple. The result will bea binary tree of the
|
||||
snailfish number.
|
||||
Alternatives for parsing would be eval() - which can be unsafe and is slow - or json.parse().
|
||||
"""
|
||||
if ',' not in line:
|
||||
return int(line)
|
||||
line = line[1:-1]
|
||||
level = 0
|
||||
split_idx = 0
|
||||
for i, c in enumerate(line):
|
||||
if '[' == c:
|
||||
level += 1
|
||||
elif ']' == c:
|
||||
level -= 1
|
||||
elif 0 == level and ',' == c:
|
||||
split_idx = i
|
||||
break
|
||||
left = parse(line[:split_idx])
|
||||
right = parse(line[split_idx + 1:])
|
||||
return (left, right)
|
||||
|
||||
|
||||
def explode_to_left(n, l):
|
||||
(left, right) = n
|
||||
res = True
|
||||
if isinstance(right, int):
|
||||
right = right + l
|
||||
else:
|
||||
res, right = explode_to_left(right, l)
|
||||
if not res:
|
||||
if isinstance(left, int):
|
||||
left = left + l
|
||||
res = True
|
||||
else:
|
||||
res, left = explode_to_left(left, l)
|
||||
if not res:
|
||||
res = False
|
||||
return res, (left, right)
|
||||
|
||||
|
||||
def explode_to_right(n, r):
|
||||
(left, right) = n
|
||||
res = True
|
||||
if isinstance(left, int):
|
||||
left = left + r
|
||||
else:
|
||||
res, left = explode_to_right(left, r)
|
||||
if not res:
|
||||
if isinstance(right, int):
|
||||
right = right + r
|
||||
res = True
|
||||
else:
|
||||
res, right = explode_to_right(right, r)
|
||||
if not res:
|
||||
res = False
|
||||
return res, (left, right)
|
||||
|
||||
|
||||
def explode(n, lvl=0):
|
||||
if isinstance(n, int):
|
||||
return False, n, None, None
|
||||
(left, right) = n
|
||||
if lvl == 4 and isinstance(left, int) and isinstance(right, int):
|
||||
return True, 0, left, right
|
||||
lvl += 1
|
||||
res, left, l, r = explode(left, lvl)
|
||||
if res:
|
||||
if r:
|
||||
if isinstance(right, int):
|
||||
right = right + r
|
||||
r = None
|
||||
else:
|
||||
res, right = explode_to_right(right, r)
|
||||
r = None if res else r
|
||||
return True, (left, right), l, r
|
||||
res, right, l, r = explode(right, lvl)
|
||||
if res:
|
||||
if l:
|
||||
if isinstance(left, int):
|
||||
left = left + l
|
||||
l = None
|
||||
else:
|
||||
res, left = explode_to_left(left, l)
|
||||
l = None if res else l
|
||||
return True, (left, right), l, r
|
||||
return False, n, None, None
|
||||
|
||||
|
||||
def split(n):
|
||||
if isinstance(n, int):
|
||||
if 10 <= n:
|
||||
return True, (n // 2, (n + 1) // 2)
|
||||
return False, n
|
||||
(left, right) = n
|
||||
res, left = split(left)
|
||||
if not res:
|
||||
res, right = split(right)
|
||||
return res, (left, right)
|
||||
|
||||
|
||||
def magnitude(n):
|
||||
m = 0
|
||||
(left, right) = n
|
||||
if isinstance(left, int):
|
||||
m += 3 * left
|
||||
else:
|
||||
m += 3 * magnitude(left)
|
||||
if isinstance(right, int):
|
||||
m += 2 * right
|
||||
else:
|
||||
m += 2 * magnitude(right)
|
||||
return m
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
n = None
|
||||
for line in input:
|
||||
line = line.rstrip()
|
||||
if not n:
|
||||
n = parse(line)
|
||||
continue
|
||||
n = (n, parse(line))
|
||||
res = True
|
||||
while res:
|
||||
while res:
|
||||
res, n, _, _ = explode(n)
|
||||
res, n = split(n)
|
||||
result = magnitude(n)
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
nbrs = set()
|
||||
for line in input:
|
||||
line = line.rstrip()
|
||||
nbrs.add(parse(line))
|
||||
for n1 in nbrs:
|
||||
for n2 in nbrs:
|
||||
if n1 == n2:
|
||||
continue
|
||||
n = (n1, n2)
|
||||
res = True
|
||||
while res:
|
||||
while res:
|
||||
res, n, _, _ = explode(n)
|
||||
res, n = split(n)
|
||||
result = max(result, magnitude(n))
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
100
day-18/input.txt
Normal file
100
day-18/input.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
[[[0,6],[[8,9],[3,7]]],[[[3,4],[7,0]],[[6,9],[4,8]]]]
|
||||
[[2,2],[[[7,7],5],[[0,7],2]]]
|
||||
[6,[9,[[7,9],7]]]
|
||||
[[[[5,1],[9,3]],8],[4,[2,[6,6]]]]
|
||||
[[[4,3],[0,4]],[[[4,5],[9,3]],3]]
|
||||
[[[[2,7],7],[[6,5],6]],[[[2,3],[7,9]],[0,3]]]
|
||||
[[[3,[6,2]],[7,[9,4]]],3]
|
||||
[[[[9,3],4],[3,9]],8]
|
||||
[[[7,8],[[2,6],1]],[[[1,7],5],[[5,6],[6,1]]]]
|
||||
[[[[0,7],9],[[6,6],[8,4]]],[[[9,2],[4,8]],[[8,5],[0,6]]]]
|
||||
[[6,[[5,6],[3,8]]],[[8,9],[4,3]]]
|
||||
[[[[0,6],1],[[2,4],[1,4]]],[[7,5],[8,3]]]
|
||||
[[[[0,7],1],[[5,7],7]],[[[3,3],[6,7]],[[2,8],[2,9]]]]
|
||||
[[7,7],[[1,[3,7]],9]]
|
||||
[[8,[[3,0],0]],[[[8,3],0],9]]
|
||||
[[[[6,2],[2,6]],3],[6,[[4,7],2]]]
|
||||
[[[5,[2,3]],[8,[8,7]]],[[0,0],2]]
|
||||
[[1,6],[7,[7,[9,0]]]]
|
||||
[[[7,[7,6]],[7,4]],[[7,2],[6,5]]]
|
||||
[1,[[8,[9,5]],2]]
|
||||
[[[[8,2],[6,5]],[4,[9,2]]],[[0,[2,6]],[6,6]]]
|
||||
[[1,[[7,2],5]],[[[6,0],[8,1]],8]]
|
||||
[[[[0,6],[6,6]],2],[[4,2],[2,4]]]
|
||||
[[5,[9,0]],[2,5]]
|
||||
[7,[[9,7],[[9,9],4]]]
|
||||
[[5,[[6,4],7]],[8,[[4,4],[9,0]]]]
|
||||
[2,[[[3,2],[1,9]],[[3,8],[7,5]]]]
|
||||
[[[[8,2],0],[5,[4,3]]],0]
|
||||
[[[0,[7,8]],[[9,6],7]],[[7,[1,0]],[[0,3],7]]]
|
||||
[[[[8,3],0],[[4,8],[7,9]]],[[7,1],[[8,4],[4,4]]]]
|
||||
[[[2,0],[[6,6],7]],[[2,[3,9]],[[5,6],[4,6]]]]
|
||||
[[[[1,4],8],[9,6]],8]
|
||||
[[7,[9,1]],[1,[[8,5],[6,8]]]]
|
||||
[8,[[2,6],5]]
|
||||
[[[9,[7,8]],[[7,8],6]],3]
|
||||
[1,[[[2,1],7],[[2,6],7]]]
|
||||
[[7,[4,[6,1]]],[[[4,9],8],[[0,1],[1,7]]]]
|
||||
[[[7,9],[[2,6],[2,4]]],[[2,[1,7]],[[3,9],[8,9]]]]
|
||||
[[[[4,5],[4,7]],[[4,0],[9,9]]],0]
|
||||
[3,[[[6,9],2],[5,3]]]
|
||||
[1,[8,[[0,8],[1,3]]]]
|
||||
[[[7,[9,2]],[4,[0,3]]],2]
|
||||
[3,[[[7,7],6],[[8,4],1]]]
|
||||
[[[[6,3],[2,6]],[[6,9],[8,1]]],[[[2,1],[7,5]],[[7,3],[7,3]]]]
|
||||
[[[1,6],[[5,1],[5,0]]],[[1,0],[6,9]]]
|
||||
[[[[8,6],[3,3]],[[2,1],[4,1]]],[1,[[7,7],[8,5]]]]
|
||||
[[1,5],[6,[[2,3],[2,4]]]]
|
||||
[[0,[7,[9,0]]],[9,0]]
|
||||
[[[5,[1,9]],[0,[9,8]]],[[[6,7],[6,3]],[8,1]]]
|
||||
[[[4,7],[6,[2,1]]],5]
|
||||
[[3,[4,0]],[2,[4,5]]]
|
||||
[[[4,0],[6,[8,3]]],[[0,6],8]]
|
||||
[[[[9,9],0],[[1,8],0]],[[1,6],[3,4]]]
|
||||
[[[[4,3],4],1],[0,[[2,1],[3,9]]]]
|
||||
[[[8,[6,2]],[6,0]],7]
|
||||
[[9,[6,[3,1]]],[[[5,9],0],[4,5]]]
|
||||
[4,[7,[[2,5],4]]]
|
||||
[[2,[8,[2,9]]],[[[0,1],[3,5]],1]]
|
||||
[[[7,9],[7,3]],[[1,[7,1]],[1,2]]]
|
||||
[[[7,0],[[1,0],8]],[[9,[7,6]],[9,[7,2]]]]
|
||||
[[[8,1],[[0,6],2]],[9,[[1,8],[5,4]]]]
|
||||
[6,[[[9,5],[5,4]],3]]
|
||||
[[4,[[6,8],[8,3]]],[[9,[0,9]],7]]
|
||||
[[[6,9],[[2,3],8]],[[9,[5,1]],[[7,6],5]]]
|
||||
[[0,1],5]
|
||||
[[4,[1,9]],[[8,0],8]]
|
||||
[[5,[0,6]],[1,8]]
|
||||
[[[[9,2],7],7],[4,[1,[5,6]]]]
|
||||
[[7,[9,[6,5]]],[[6,9],1]]
|
||||
[[[5,2],[0,[1,4]]],[[0,4],[[9,4],8]]]
|
||||
[[[[7,1],[4,9]],3],[[[4,5],8],[7,[0,4]]]]
|
||||
[[[9,[8,0]],7],[[[4,5],8],[[4,3],[8,5]]]]
|
||||
[[9,[7,0]],[[3,[1,7]],[[7,0],7]]]
|
||||
[[2,[[6,2],6]],8]
|
||||
[[[8,[9,6]],[[5,8],[7,2]]],[4,[9,9]]]
|
||||
[[[[0,5],0],[[8,4],4]],[[7,9],8]]
|
||||
[[[0,[0,3]],[0,[8,8]]],[[[2,1],3],4]]
|
||||
[0,[[4,1],[[9,9],2]]]
|
||||
[[3,[7,[6,7]]],[0,2]]
|
||||
[7,2]
|
||||
[0,[3,[[3,4],[4,4]]]]
|
||||
[[[[0,1],[5,9]],[[4,2],7]],[5,[1,8]]]
|
||||
[[7,1],[[1,[9,9]],[[8,4],8]]]
|
||||
[[[1,[8,3]],[[3,7],0]],[[2,0],[[1,6],[9,9]]]]
|
||||
[[[1,4],[1,4]],[[2,[2,7]],[2,[7,1]]]]
|
||||
[[1,[[6,8],[8,6]]],[0,[8,0]]]
|
||||
[1,[[2,0],7]]
|
||||
[[[[6,0],9],[[6,9],[8,3]]],[[3,[9,9]],6]]
|
||||
[[[[9,8],[2,8]],[2,3]],[6,2]]
|
||||
[[[6,[2,2]],7],[[3,[7,8]],7]]
|
||||
[[[5,[3,7]],1],[[[4,0],3],[5,4]]]
|
||||
[[[7,[4,3]],[9,[4,4]]],7]
|
||||
[[2,[[1,5],6]],[[2,3],[[2,5],[7,1]]]]
|
||||
[[[[3,9],[1,9]],3],[5,[[0,6],[3,2]]]]
|
||||
[[[3,[7,5]],[[7,7],[2,8]]],[4,[1,[0,0]]]]
|
||||
[[4,[2,[8,7]]],[[[0,5],0],9]]
|
||||
[9,[9,[6,4]]]
|
||||
[[5,[[4,9],2]],[9,9]]
|
||||
[[1,[[6,0],[9,9]]],[[[8,4],1],[[5,2],[6,1]]]]
|
||||
[[1,[[9,0],8]],6]
|
||||
389
day-19/README.md
Normal file
389
day-19/README.md
Normal file
@@ -0,0 +1,389 @@
|
||||
# Day 19: Beacon Scanner
|
||||
|
||||
[https://adventofcode.com/2021/day/19](https://adventofcode.com/2021/day/19)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
As your [probe](https://adventofcode.com/2021/day/17) drifted down through this area, it released an assortment of _beacons_ and _scanners_ into the water. It's difficult to navigate in the pitch black open waters of the ocean trench, but if you can build a map of the trench using data from the scanners, you should be able to safely reach the bottom.
|
||||
|
||||
The beacons and scanners float motionless in the water; they're designed to maintain the same position for long periods of time. Each scanner is capable of detecting all beacons in a large cube centered on the scanner; beacons that are at most 1000 units away from the scanner in each of the three axes (`x`, `y`, and `z`) have their precise position determined relative to the scanner. However, scanners cannot detect other scanners. The submarine has automatically summarized the relative positions of beacons detected by each scanner (your puzzle input).
|
||||
|
||||
For example, if a scanner is at `x,y,z` coordinates `500,0,-500` and there are beacons at `-500,1000,-1500` and `1501,0,-500`, the scanner could report that the first beacon is at `-1000,1000,-1000` (relative to the scanner) but would not detect the second beacon at all.
|
||||
|
||||
Unfortunately, while each scanner can report the positions of all detected beacons relative to itself, _the scanners do not know their own position_. You'll need to determine the positions of the beacons and scanners yourself.
|
||||
|
||||
The scanners and beacons map a single contiguous 3d region. This region can be reconstructed by finding pairs of scanners that have overlapping detection regions such that there are _at least 12 beacons_ that both scanners detect within the overlap. By establishing 12 common beacons, you can precisely determine where the scanners are relative to each other, allowing you to reconstruct the beacon map one scanner at a time.
|
||||
|
||||
For a moment, consider only two dimensions. Suppose you have the following scanner reports:
|
||||
|
||||
--- scanner 0 ---
|
||||
0,2
|
||||
4,1
|
||||
3,3
|
||||
|
||||
--- scanner 1 ---
|
||||
-1,-1
|
||||
-5,0
|
||||
-2,1
|
||||
|
||||
|
||||
Drawing `x` increasing rightward, `y` increasing upward, scanners as `S`, and beacons as `B`, scanner `0` detects this:
|
||||
|
||||
...B.
|
||||
B....
|
||||
....B
|
||||
S....
|
||||
|
||||
|
||||
Scanner `1` detects this:
|
||||
|
||||
...B..
|
||||
B....S
|
||||
....B.
|
||||
|
||||
|
||||
For this example, assume scanners only need 3 overlapping beacons. Then, the beacons visible to both scanners overlap to produce the following complete map:
|
||||
|
||||
...B..
|
||||
B....S
|
||||
....B.
|
||||
S.....
|
||||
|
||||
|
||||
Unfortunately, there's a second problem: the scanners also don't know their _rotation or facing direction_. Due to magnetic alignment, each scanner is rotated some integer number of 90-degree turns around all of the `x`, `y`, and `z` axes. That is, one scanner might call a direction positive `x`, while another scanner might call that direction negative `y`. Or, two scanners might agree on which direction is positive `x`, but one scanner might be upside-down from the perspective of the other scanner. In total, each scanner could be in any of 24 different orientations: facing positive or negative `x`, `y`, or `z`, and considering any of four directions "up" from that facing.
|
||||
|
||||
For example, here is an arrangement of beacons as seen from a scanner in the same position but in different orientations:
|
||||
|
||||
--- scanner 0 ---
|
||||
-1,-1,1
|
||||
-2,-2,2
|
||||
-3,-3,3
|
||||
-2,-3,1
|
||||
5,6,-4
|
||||
8,0,7
|
||||
|
||||
--- scanner 0 ---
|
||||
1,-1,1
|
||||
2,-2,2
|
||||
3,-3,3
|
||||
2,-1,3
|
||||
-5,4,-6
|
||||
-8,-7,0
|
||||
|
||||
--- scanner 0 ---
|
||||
-1,-1,-1
|
||||
-2,-2,-2
|
||||
-3,-3,-3
|
||||
-1,-3,-2
|
||||
4,6,5
|
||||
-7,0,8
|
||||
|
||||
--- scanner 0 ---
|
||||
1,1,-1
|
||||
2,2,-2
|
||||
3,3,-3
|
||||
1,3,-2
|
||||
-4,-6,5
|
||||
7,0,8
|
||||
|
||||
--- scanner 0 ---
|
||||
1,1,1
|
||||
2,2,2
|
||||
3,3,3
|
||||
3,1,2
|
||||
-6,-4,-5
|
||||
0,7,-8
|
||||
|
||||
|
||||
By finding pairs of scanners that both see at least 12 of the same beacons, you can assemble the entire map. For example, consider the following report:
|
||||
|
||||
--- scanner 0 ---
|
||||
404,-588,-901
|
||||
528,-643,409
|
||||
-838,591,734
|
||||
390,-675,-793
|
||||
-537,-823,-458
|
||||
-485,-357,347
|
||||
-345,-311,381
|
||||
-661,-816,-575
|
||||
-876,649,763
|
||||
-618,-824,-621
|
||||
553,345,-567
|
||||
474,580,667
|
||||
-447,-329,318
|
||||
-584,868,-557
|
||||
544,-627,-890
|
||||
564,392,-477
|
||||
455,729,728
|
||||
-892,524,684
|
||||
-689,845,-530
|
||||
423,-701,434
|
||||
7,-33,-71
|
||||
630,319,-379
|
||||
443,580,662
|
||||
-789,900,-551
|
||||
459,-707,401
|
||||
|
||||
--- scanner 1 ---
|
||||
686,422,578
|
||||
605,423,415
|
||||
515,917,-361
|
||||
-336,658,858
|
||||
95,138,22
|
||||
-476,619,847
|
||||
-340,-569,-846
|
||||
567,-361,727
|
||||
-460,603,-452
|
||||
669,-402,600
|
||||
729,430,532
|
||||
-500,-761,534
|
||||
-322,571,750
|
||||
-466,-666,-811
|
||||
-429,-592,574
|
||||
-355,545,-477
|
||||
703,-491,-529
|
||||
-328,-685,520
|
||||
413,935,-424
|
||||
-391,539,-444
|
||||
586,-435,557
|
||||
-364,-763,-893
|
||||
807,-499,-711
|
||||
755,-354,-619
|
||||
553,889,-390
|
||||
|
||||
--- scanner 2 ---
|
||||
649,640,665
|
||||
682,-795,504
|
||||
-784,533,-524
|
||||
-644,584,-595
|
||||
-588,-843,648
|
||||
-30,6,44
|
||||
-674,560,763
|
||||
500,723,-460
|
||||
609,671,-379
|
||||
-555,-800,653
|
||||
-675,-892,-343
|
||||
697,-426,-610
|
||||
578,704,681
|
||||
493,664,-388
|
||||
-671,-858,530
|
||||
-667,343,800
|
||||
571,-461,-707
|
||||
-138,-166,112
|
||||
-889,563,-600
|
||||
646,-828,498
|
||||
640,759,510
|
||||
-630,509,768
|
||||
-681,-892,-333
|
||||
673,-379,-804
|
||||
-742,-814,-386
|
||||
577,-820,562
|
||||
|
||||
--- scanner 3 ---
|
||||
-589,542,597
|
||||
605,-692,669
|
||||
-500,565,-823
|
||||
-660,373,557
|
||||
-458,-679,-417
|
||||
-488,449,543
|
||||
-626,468,-788
|
||||
338,-750,-386
|
||||
528,-832,-391
|
||||
562,-778,733
|
||||
-938,-730,414
|
||||
543,643,-506
|
||||
-524,371,-870
|
||||
407,773,750
|
||||
-104,29,83
|
||||
378,-903,-323
|
||||
-778,-728,485
|
||||
426,699,580
|
||||
-438,-605,-362
|
||||
-469,-447,-387
|
||||
509,732,623
|
||||
647,635,-688
|
||||
-868,-804,481
|
||||
614,-800,639
|
||||
595,780,-596
|
||||
|
||||
--- scanner 4 ---
|
||||
727,592,562
|
||||
-293,-554,779
|
||||
441,611,-461
|
||||
-714,465,-776
|
||||
-743,427,-804
|
||||
-660,-479,-426
|
||||
832,-632,460
|
||||
927,-485,-438
|
||||
408,393,-506
|
||||
466,436,-512
|
||||
110,16,151
|
||||
-258,-428,682
|
||||
-393,719,612
|
||||
-211,-452,876
|
||||
808,-476,-593
|
||||
-575,615,604
|
||||
-485,667,467
|
||||
-680,325,-822
|
||||
-627,-443,-432
|
||||
872,-547,-609
|
||||
833,512,582
|
||||
807,604,487
|
||||
839,-516,451
|
||||
891,-625,532
|
||||
-652,-548,-490
|
||||
30,-46,-14
|
||||
|
||||
|
||||
Because all coordinates are relative, in this example, all "absolute" positions will be expressed relative to scanner `0` (using the orientation of scanner `0` and as if scanner `0` is at coordinates `0,0,0`).
|
||||
|
||||
Scanners `0` and `1` have overlapping detection cubes; the 12 beacons they both detect (relative to scanner `0`) are at the following coordinates:
|
||||
|
||||
-618,-824,-621
|
||||
-537,-823,-458
|
||||
-447,-329,318
|
||||
404,-588,-901
|
||||
544,-627,-890
|
||||
528,-643,409
|
||||
-661,-816,-575
|
||||
390,-675,-793
|
||||
423,-701,434
|
||||
-345,-311,381
|
||||
459,-707,401
|
||||
-485,-357,347
|
||||
|
||||
|
||||
These same 12 beacons (in the same order) but from the perspective of scanner `1` are:
|
||||
|
||||
686,422,578
|
||||
605,423,415
|
||||
515,917,-361
|
||||
-336,658,858
|
||||
-476,619,847
|
||||
-460,603,-452
|
||||
729,430,532
|
||||
-322,571,750
|
||||
-355,545,-477
|
||||
413,935,-424
|
||||
-391,539,-444
|
||||
553,889,-390
|
||||
|
||||
|
||||
Because of this, scanner `1` must be at `68,-1246,-43` (relative to scanner `0`).
|
||||
|
||||
Scanner `4` overlaps with scanner `1`; the 12 beacons they both detect (relative to scanner `0`) are:
|
||||
|
||||
459,-707,401
|
||||
-739,-1745,668
|
||||
-485,-357,347
|
||||
432,-2009,850
|
||||
528,-643,409
|
||||
423,-701,434
|
||||
-345,-311,381
|
||||
408,-1815,803
|
||||
534,-1912,768
|
||||
-687,-1600,576
|
||||
-447,-329,318
|
||||
-635,-1737,486
|
||||
|
||||
|
||||
So, scanner `4` is at `-20,-1133,1061` (relative to scanner `0`).
|
||||
|
||||
Following this process, scanner `2` must be at `1105,-1205,1229` (relative to scanner `0`) and scanner `3` must be at `-92,-2380,-20` (relative to scanner `0`).
|
||||
|
||||
The full list of beacons (relative to scanner `0`) is:
|
||||
|
||||
-892,524,684
|
||||
-876,649,763
|
||||
-838,591,734
|
||||
-789,900,-551
|
||||
-739,-1745,668
|
||||
-706,-3180,-659
|
||||
-697,-3072,-689
|
||||
-689,845,-530
|
||||
-687,-1600,576
|
||||
-661,-816,-575
|
||||
-654,-3158,-753
|
||||
-635,-1737,486
|
||||
-631,-672,1502
|
||||
-624,-1620,1868
|
||||
-620,-3212,371
|
||||
-618,-824,-621
|
||||
-612,-1695,1788
|
||||
-601,-1648,-643
|
||||
-584,868,-557
|
||||
-537,-823,-458
|
||||
-532,-1715,1894
|
||||
-518,-1681,-600
|
||||
-499,-1607,-770
|
||||
-485,-357,347
|
||||
-470,-3283,303
|
||||
-456,-621,1527
|
||||
-447,-329,318
|
||||
-430,-3130,366
|
||||
-413,-627,1469
|
||||
-345,-311,381
|
||||
-36,-1284,1171
|
||||
-27,-1108,-65
|
||||
7,-33,-71
|
||||
12,-2351,-103
|
||||
26,-1119,1091
|
||||
346,-2985,342
|
||||
366,-3059,397
|
||||
377,-2827,367
|
||||
390,-675,-793
|
||||
396,-1931,-563
|
||||
404,-588,-901
|
||||
408,-1815,803
|
||||
423,-701,434
|
||||
432,-2009,850
|
||||
443,580,662
|
||||
455,729,728
|
||||
456,-540,1869
|
||||
459,-707,401
|
||||
465,-695,1988
|
||||
474,580,667
|
||||
496,-1584,1900
|
||||
497,-1838,-617
|
||||
527,-524,1933
|
||||
528,-643,409
|
||||
534,-1912,768
|
||||
544,-627,-890
|
||||
553,345,-567
|
||||
564,392,-477
|
||||
568,-2007,-577
|
||||
605,-1665,1952
|
||||
612,-1593,1893
|
||||
630,319,-379
|
||||
686,-3108,-505
|
||||
776,-3184,-501
|
||||
846,-3110,-434
|
||||
1135,-1161,1235
|
||||
1243,-1093,1063
|
||||
1660,-552,429
|
||||
1693,-557,386
|
||||
1735,-437,1738
|
||||
1749,-1800,1813
|
||||
1772,-405,1572
|
||||
1776,-675,371
|
||||
1779,-442,1789
|
||||
1780,-1548,337
|
||||
1786,-1538,337
|
||||
1847,-1591,415
|
||||
1889,-1729,1762
|
||||
1994,-1805,1792
|
||||
|
||||
|
||||
In total, there are _`79`_ beacons.
|
||||
|
||||
Assemble the full map of beacons. _How many beacons are there?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Sometimes, it's a good idea to appreciate just how <span title="The deepest parts of the ocean are about as deep as the altitude of a normal commercial aircraft, roughly 11 kilometers or 36000 feet.">big</span> the ocean is. Using the [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry), how far apart do the scanners get?
|
||||
|
||||
In the above example, scanners `2` (`1105,-1205,1229`) and `3` (`-92,-2380,-20`) are the largest Manhattan distance apart. In total, they are `1197 + 1175 + 1249 = 3621` units apart.
|
||||
|
||||
_What is the largest Manhattan distance between any two scanners?_
|
||||
160
day-19/day-19.py
Normal file
160
day-19/day-19.py
Normal file
@@ -0,0 +1,160 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def parse(input):
|
||||
scanners = []
|
||||
s = []
|
||||
for line in input:
|
||||
line = line.rstrip()
|
||||
if 'scanner' in line:
|
||||
s = []
|
||||
continue
|
||||
if len(line):
|
||||
s.append(tuple([int(x) for x in line.split(',')]))
|
||||
else:
|
||||
scanners.append(s)
|
||||
if len(s):
|
||||
scanners.append(s)
|
||||
return scanners
|
||||
|
||||
|
||||
def calc_distances(s):
|
||||
d = [set() for _ in s]
|
||||
for i in range(len(s)):
|
||||
for j in range(i+1, len(s)):
|
||||
dist = 0
|
||||
for k in range(len(s[i])):
|
||||
dist += (s[i][k] - s[j][k]) ** 2
|
||||
d[i].add(dist)
|
||||
d[j].add(dist)
|
||||
return d
|
||||
|
||||
|
||||
def num_overlapping(s0, s1):
|
||||
num = 0
|
||||
for b0 in s0:
|
||||
for b1 in s1:
|
||||
if 11 <= len(b0 & b1):
|
||||
num += 1
|
||||
return num
|
||||
|
||||
|
||||
def get_transform_func(v0, v1):
|
||||
(x0, y0, z0) = v0
|
||||
(x1, y1, z1) = v1
|
||||
transform = '('
|
||||
if abs(x0) == abs(x1):
|
||||
if 0 > x0 // x1:
|
||||
transform += '-'
|
||||
transform += 'x,'
|
||||
elif abs(x0) == abs(y1):
|
||||
if 0 > x0 // y1:
|
||||
transform += '-'
|
||||
transform += 'y,'
|
||||
elif abs(x0) == abs(z1):
|
||||
if 0 > x0 // z1:
|
||||
transform += '-'
|
||||
transform += 'z,'
|
||||
if abs(y0) == abs(x1):
|
||||
if 0 > y0 // x1:
|
||||
transform += '-'
|
||||
transform += 'x,'
|
||||
elif abs(y0) == abs(y1):
|
||||
if 0 > y0 // y1:
|
||||
transform += '-'
|
||||
transform += 'y,'
|
||||
elif abs(y0) == abs(z1):
|
||||
if 0 > y0 // z1:
|
||||
transform += '-'
|
||||
transform += 'z,'
|
||||
if abs(z0) == abs(x1):
|
||||
if 0 > z0 // x1:
|
||||
transform += '-'
|
||||
transform += 'x'
|
||||
elif abs(z0) == abs(y1):
|
||||
if 0 > z0 // y1:
|
||||
transform += '-'
|
||||
transform += 'y'
|
||||
elif abs(z0) == abs(z1):
|
||||
if 0 > z0 // z1:
|
||||
transform += '-'
|
||||
transform += 'z'
|
||||
transform += ')'
|
||||
return transform
|
||||
|
||||
|
||||
def transform(scanners, idx, done=[]):
|
||||
done.append(idx)
|
||||
for next in scanners[idx]['overlapping']:
|
||||
if next in done:
|
||||
continue
|
||||
overlapping = []
|
||||
for i, b0 in enumerate(scanners[idx]['metrics']):
|
||||
for j, b1 in enumerate(scanners[next]['metrics']):
|
||||
if 11 <= len(b0 & b1):
|
||||
overlapping.append((i, j))
|
||||
if 2 <= len(overlapping):
|
||||
break
|
||||
if 2 <= len(overlapping):
|
||||
break
|
||||
assert len(overlapping) == 2
|
||||
(x0, y0, z0) = scanners[idx]['beacons'][overlapping[0][0]]
|
||||
(x1, y1, z1) = scanners[idx]['beacons'][overlapping[1][0]]
|
||||
v0 = (x0 - x1, y0 - y1, z0 - z1)
|
||||
(x0, y0, z0) = scanners[next]['beacons'][overlapping[0][1]]
|
||||
(x1, y1, z1) = scanners[next]['beacons'][overlapping[1][1]]
|
||||
v1 = (x0 - x1, y0 - y1, z0 - z1)
|
||||
transform_func = get_transform_func(v0, v1)
|
||||
for i, b in enumerate(scanners[next]['beacons']):
|
||||
(x, y, z) = b
|
||||
scanners[next]['beacons'][i] = eval(transform_func)
|
||||
(x0, y0, z0) = scanners[idx]['beacons'][overlapping[0][0]]
|
||||
(x1, y1, z1) = scanners[next]['beacons'][overlapping[0][1]]
|
||||
(xo, yo, zo) = (x0 - x1, y0 - y1, z0 - z1)
|
||||
scanners[next]['origin'] = (xo, yo, zo)
|
||||
for i in range(len(scanners[next]['beacons'])):
|
||||
(x, y, z) = scanners[next]['beacons'][i]
|
||||
scanners[next]['beacons'][i] = (x + xo, y + yo, z + zo)
|
||||
transform(scanners, next, done)
|
||||
|
||||
|
||||
def solve(input):
|
||||
result_p1 = 0
|
||||
result_p2 = 0
|
||||
beacons = parse(input)
|
||||
scanners = []
|
||||
for b in beacons:
|
||||
scanners.append({
|
||||
'beacons': b,
|
||||
'metrics': calc_distances(b),
|
||||
'origin': (0, 0, 0),
|
||||
'overlapping': set()})
|
||||
for i in range(len(scanners)):
|
||||
for j in range(i + 1, len(scanners)):
|
||||
num = num_overlapping(
|
||||
scanners[i]['metrics'], scanners[j]['metrics'])
|
||||
if num:
|
||||
scanners[i]['overlapping'].add(j)
|
||||
scanners[j]['overlapping'].add(i)
|
||||
transform(scanners, 0)
|
||||
m = set()
|
||||
for s in scanners:
|
||||
for b in s['beacons']:
|
||||
m.add(b)
|
||||
result_p1 = len(m)
|
||||
for i, si in enumerate(scanners):
|
||||
(xi, yi, zi) = si['origin']
|
||||
for j, sj in enumerate(scanners[i + 1:]):
|
||||
(xj, yj, zj) = sj['origin']
|
||||
md = abs(xi - xj) + abs(yi - yj) + abs(zi - zj)
|
||||
result_p2 = max(result_p2, md)
|
||||
print("Part 1 result:", result_p1)
|
||||
print("Part 2 result:", result_p2)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
solve(input)
|
||||
1005
day-19/input.txt
Normal file
1005
day-19/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
125
day-20/README.md
Normal file
125
day-20/README.md
Normal file
@@ -0,0 +1,125 @@
|
||||
# Day 20: Trench Map
|
||||
|
||||
[https://adventofcode.com/2021/day/20](https://adventofcode.com/2021/day/20)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
With the scanners fully deployed, you turn their attention to mapping the floor of the ocean trench.
|
||||
|
||||
When you get back the image from the scanners, it seems to just be random noise. Perhaps you can combine an image enhancement algorithm and the input image (your puzzle input) to clean it up a little.
|
||||
|
||||
For example:
|
||||
|
||||
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##
|
||||
#..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###
|
||||
.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#.
|
||||
.#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#.....
|
||||
.#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#..
|
||||
...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.....
|
||||
..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
|
||||
|
||||
#..#.
|
||||
#....
|
||||
##..#
|
||||
..#..
|
||||
..###
|
||||
|
||||
|
||||
The first section is the _image enhancement algorithm_. It is normally given on a single line, but it has been wrapped to multiple lines in this example for legibility. The second section is the _input image_, a two-dimensional grid of _light pixels_ (`#`) and _dark pixels_ (`.`).
|
||||
|
||||
The image enhancement algorithm describes how to enhance an image by _simultaneously_ converting all pixels in the input image into an output image. Each pixel of the output image is determined by looking at a 3x3 square of pixels centered on the corresponding input image pixel. So, to determine the value of the pixel at (5,10) in the output image, nine pixels from the input image need to be considered: (4,9), (4,10), (4,11), (5,9), (5,10), (5,11), (6,9), (6,10), and (6,11). These nine input pixels are combined into a single binary number that is used as an index in the _image enhancement algorithm_ string.
|
||||
|
||||
For example, to determine the output pixel that corresponds to the very middle pixel of the input image, the nine pixels marked by `[...]` would need to be considered:
|
||||
|
||||
# . . # .
|
||||
#[. . .].
|
||||
#[# . .]#
|
||||
.[. # .].
|
||||
. . # # #
|
||||
|
||||
|
||||
Starting from the top-left and reading across each row, these pixels are `...`, then `#..`, then `.#.`; combining these forms `...#...#.`. By turning dark pixels (`.`) into `0` and light pixels (`#`) into `1`, the binary number `000100010` can be formed, which is `34` in decimal.
|
||||
|
||||
The image enhancement algorithm string is exactly 512 characters long, enough to match every possible 9-bit binary number. The first few characters of the string (numbered starting from zero) are as follows:
|
||||
|
||||
0 10 20 30 34 40 50 60 70
|
||||
| | | | | | | | |
|
||||
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##
|
||||
|
||||
|
||||
In the middle of this first group of characters, the character at index 34 can be found: `#`. So, the output pixel in the center of the output image should be `#`, a _light pixel_.
|
||||
|
||||
This process can then be repeated to calculate every pixel of the output image.
|
||||
|
||||
Through advances in imaging technology, the images being operated on here are _infinite_ in size. _Every_ pixel of the infinite output image needs to be calculated exactly based on the relevant pixels of the input image. The small input image you have is only a small region of the actual infinite input image; the rest of the input image consists of dark pixels (`.`). For the purposes of the example, to save on space, only a portion of the infinite-sized input and output images will be shown.
|
||||
|
||||
The starting input image, therefore, looks something like this, with more dark pixels (`.`) extending forever in every direction not shown here:
|
||||
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
.....#..#......
|
||||
.....#.........
|
||||
.....##..#.....
|
||||
.......#.......
|
||||
.......###.....
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
|
||||
|
||||
By applying the image enhancement algorithm to every pixel simultaneously, the following output image can be obtained:
|
||||
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
.....##.##.....
|
||||
....#..#.#.....
|
||||
....##.#..#....
|
||||
....####..#....
|
||||
.....#..##.....
|
||||
......##..#....
|
||||
.......#.#.....
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
|
||||
|
||||
Through further advances in imaging technology, the above output image can also be used as an input image! This allows it to be enhanced _a second time_:
|
||||
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
..........#....
|
||||
....#..#.#.....
|
||||
...#.#...###...
|
||||
...#...##.#....
|
||||
...#.....#.#...
|
||||
....#.#####....
|
||||
.....#.#####...
|
||||
......##.##....
|
||||
.......###.....
|
||||
...............
|
||||
...............
|
||||
...............
|
||||
|
||||
|
||||
Truly incredible - now the small details are really starting to come through. After enhancing the original input image twice, _`35`_ pixels are lit.
|
||||
|
||||
Start with the original input image and apply the image enhancement algorithm twice, being careful to account for the infinite size of the images. _How many pixels are lit in the resulting image?_
|
||||
|
||||
### Part Two
|
||||
|
||||
You still can't quite make out the details in the image. Maybe you just didn't [enhance](https://en.wikipedia.org/wiki/Kernel_(image_processing)) it <span title="Yeah, that's definitely the problem.">enough</span>.
|
||||
|
||||
If you enhance the starting input image in the above example a total of _50_ times, _`3351`_ pixels are lit in the final output image.
|
||||
|
||||
Start again with the original input image and apply the image enhancement algorithm 50 times. _How many pixels are lit in the resulting image?_
|
||||
80
day-20/day-20.py
Normal file
80
day-20/day-20.py
Normal file
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from time import sleep
|
||||
|
||||
|
||||
def enhance(algorithm, in_image, boundary):
|
||||
out_image = set()
|
||||
min_x = min([x for x, _ in in_image])
|
||||
max_x = max([x + 1 for x, _ in in_image])
|
||||
min_y = min([y for _, y in in_image])
|
||||
max_y = max([y + 1 for _, y in in_image])
|
||||
for y in range(min_y - boundary, max_y + boundary):
|
||||
for x in range(min_x - boundary, max_x + boundary):
|
||||
idx = 0
|
||||
for dy in [-1, 0, 1]:
|
||||
for dx in [-1, 0, 1]:
|
||||
idx <<= 1
|
||||
if (x + dx, y + dy) in in_image:
|
||||
idx |= 1
|
||||
if '#' == algorithm[idx]:
|
||||
out_image.add((x, y))
|
||||
return out_image
|
||||
|
||||
|
||||
def show(image):
|
||||
min_x = min([x for x, _ in image])
|
||||
max_x = max([x + 1 for x, _ in image])
|
||||
min_y = min([y for _, y in image])
|
||||
max_y = max([y + 1 for _, y in image])
|
||||
for y in range(min_y, max_y):
|
||||
for x in range(min_x, max_x):
|
||||
if (x, y) in image:
|
||||
print('█', end='')
|
||||
else:
|
||||
print('░', end='')
|
||||
print()
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
algorithm = input[0].rstrip()
|
||||
image = set([(x, y) for y, line in enumerate(input[2:])
|
||||
for x, c in enumerate(line) if c == '#'])
|
||||
image = enhance(algorithm, image, 3)
|
||||
image = enhance(algorithm, image, -1)
|
||||
result = len(image)
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
algorithm = input[0].rstrip()
|
||||
image = set([(x, y) for y, line in enumerate(input[2:])
|
||||
for x, c in enumerate(line) if c == '#'])
|
||||
for _ in range(25):
|
||||
image = enhance(algorithm, image, 3)
|
||||
image = enhance(algorithm, image, -1)
|
||||
result = len(image)
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
def game_of_life(input):
|
||||
result = 0
|
||||
algorithm = '.......#...#.##....#.###.######....#.##..##.#....######.###.#......#.##..##.#....######.###.#....##.#...#.......###.#...#..........#.##..##.#....######.###.#....##.#...#.......###.#...#........##.#...#.......###.#...#.......#...............#..................#.##..##.#....######.###.#....##.#...#.......###.#...#........##.#...#.......###.#...#.......#...............#................##.#...#.......###.#...#.......#...............#...............#...............#...............................................'
|
||||
image = set([(x, y) for y, line in enumerate(input[2:])
|
||||
for x, c in enumerate(line) if c == '#'])
|
||||
while True:
|
||||
image = enhance(algorithm, image, 0)
|
||||
show(image)
|
||||
print()
|
||||
sleep(0.1)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
# game_of_life(input)
|
||||
102
day-20/input.txt
Normal file
102
day-20/input.txt
Normal file
@@ -0,0 +1,102 @@
|
||||
##....###.#.##...##....####..###.#.######.#.#.##.#.####.#.#####.##.##..##.###.###..##.##..#####.##..#..##..#...#.####..#.###..#....####.#..##.##...#######.###...#.######..#..#...###..###.#####.##..#.#.###.#.###.#..#.###.###.#..##.....####..#.##.##.#..#...###.#.....##..#....#.##..#....#....####.#...#.#.##.#...#.##..#..#.#..###.###.#.##...##.#.##.##..#..##.#..#...######.#.#..###....##...##....#....##....#.#..##..#####.####.#.##...#...#.#.#....#.####...#.##..#...#..#....#..#..#..##.#.#.#.#######.###..##.#.....
|
||||
|
||||
#...##.##.#..#..#..#.....##.#.#..#.###.####...####.#.###.....#.########..###...###....#......###...#
|
||||
####.##.#.###.#.##....##..##.....####..#.##.....##.###.#..#...####..#..###..###.#...##..#.#..#.#####
|
||||
###.#..#..#..##..#..#.###..#..###.###.#.###......###.##.#.##.#........#..#...#..#...#.##..#.##.....#
|
||||
.#.#..#..#.####..##.#...##...#.##.#...##.....#.####.#.##.#.#..####.#.#..#..###.#..###......##.##....
|
||||
###.##......####..#.#..#.##.###.##.##..#####.#.#...#.###.#.#.#....####..#.####..#..##...###.##.#..##
|
||||
.#####.#..##..###.#..#..#..##..#.##.##..##..#####..#.#.#.##....##.#.###.#####.##..##.#..####...###.#
|
||||
..#..##..########...#...##....#.#..#.#.###.#...#.#..#####.###...##.#..#.##.##..#.#.#....#...##.....#
|
||||
.......##..............##...#.####....#####.###.###.#.#..#.#.####.###.#####.##..#.....####.#...##...
|
||||
#..#...##.#.##...##.####..##.....###.#.##.#####.##...#.#.###.#.##..#..#.####..###..##.##...#...#.#.#
|
||||
.#....##.#..#########.#.##.##.####....####.#..#..#...#....###.#..#.##.##...#.########.#...##.#.#####
|
||||
.....##.###.##.##..##...#....#.##.#.###...###..#..###.#.##....###.###..#...####........#..##.##...##
|
||||
##.##.##..#....#.#..#..##..#..#.####..###....######.####..#...#.#..####.....#.#.##.#.#..##..#.#.#.#.
|
||||
#######....###.###...###..###..#..#.#..#...##.##.#.##.#.#..#.#.##.#.#####.##..######.####..##..##.##
|
||||
#..#.#..#..#.#......###....##....##.###.##.#..##..###.#..###.##..####..#....####.#...####..#.....##.
|
||||
.#.####......###.....#.#.##...##.###.##....#.##...#.#.....###.#..#.###...#...#..#..##.##..#..#.#.#..
|
||||
#.#...#.####..##...#.####.##....##.##.......#######...#.#..##.##.....#...#..###....#...#..#.......##
|
||||
####.##..#..#...######.##..##.#.######..#####..###.###.#.....###.#.###...###.##...##.#......#.#.####
|
||||
####..#..#......##.......######.#####....#..#..##.##.##.#.##.....#..##.###.....###.##.#..##.##.##.##
|
||||
..##.#.#####...#.##.#.#.##...#..##.######.#.#.#####...#..##.##..##..#.##.....##.##.#.#.#.##...#.#..#
|
||||
.#.#.....#..#..#.###.##.##...###...########.####.####..##.###....##..###.#..###..###.....#.##..#.#.#
|
||||
##.#.##.....#....#.#.....###..#..##.#.##..##.###...#..#...###.....####.#..##.##..#...#.##.##......#.
|
||||
.###.#.#..####.#.#..#.##..#.#.#...######.#.#########.####...#.#.#..##...#.#...#...#...#.##..........
|
||||
.###.###...##..#..##.##.#...#.#..###.##.#...####.######..##.#.....##.#####..#....#.###.###.#####.##.
|
||||
#..###.######....##....####..#...#..##...#.###..#..###...###.#...#...##...##.#.##.##....#..#........
|
||||
.#...#..####.####......#..##....#.#....####..##.#..#.##......#.##.....####..####..#.#...#.##...####.
|
||||
#.####.#..####..#..#.######.##.#####....#.####.#...#.#.#..#.#.###..#..#....#.#.#.#..#.###.....#....#
|
||||
....##.###..###.###.#.##.##.##.#.###..###.#..#..#####....##.#...##.##.#.....#.##.##.#.#####..#####..
|
||||
..#.##.#.###.#....#.#..#.######..#......##.....##.##....#...#.#.###.#...#..#.###..#.#.##.#....#..###
|
||||
####.....#.#.#.#.#..#...#.###.##.#..#..#.##.#.##.#...#..###..#.....#....#......#.#...#.##.##.#..#.#.
|
||||
..##...##...###.##.####.#....#...#.###..#.##.#..##....#....#.#.#...........###.##....#..###.#.######
|
||||
.#.#...####...##..##.#.#..#...#.#.########.####..#..###.#..##.##..###.#.##.#....#..###....#..##....#
|
||||
#.###.##.###.###.####...##.#..##..#.#..#.#.##.#.#.##...#..#.#..#.##.#..#...##.###.....#####.#.....##
|
||||
....##........##.##....##......#...#.#...##.#.##.##.##..###....###..#...####.####.####.#..##.##.#..#
|
||||
.###.#.#..#..##..#.#..#.###..#####...###......#..##.#######...##.###...#...#..#...##...##.###.#..#.#
|
||||
#..##.##..#.####...#.###.###.###....####.#####.##...####.###.#.#####.#####.#..#.#####..#.###.#..#.#.
|
||||
..#..#.....##..##.#.##.#.....#..#####.###....#.##.#.##....#.....##....##.#.###.#......#.#.#.#####...
|
||||
######..#.#.##.#####.#.##...#####..#.##.###..##..#..#.###.#.#..#.#.....##..#...##.#...#...#..##..#..
|
||||
.##.#########..###........###.####.#..#...#..#.####......##..#####.#.#.##.##.#...##....##..#.###..#.
|
||||
..#..#..#.##....##..#..#.....###.#....####..####.#.##.#..####.#.#...#.#.###..#..#####...#...#.#.#..#
|
||||
...###.##...##......#.##.#.#..#..#...###..#########..##.....#####..###.###.....##.....####.##.###..#
|
||||
####..##...####.#.###..##.#.##.#.#...#.....#..###.#####....#.##.####..#.##.....##..#.#.##..##.#...##
|
||||
..####....##....#...#.#..###...#.#....##...##..###.#.####.#....##.###.######..######...##....#.##.##
|
||||
##..#.#.#..###.####.#..#.##.###...#.#...###.##.#.#.#.#.#.#.#.#.....##.#..#.#.##.....#..##.......##.#
|
||||
...#####.#..##.##.#...##..#.##...#.##...##....#.#.#..#.#.#.#.#.#.#..##..#.#..#.#.#.#.#.###.#...####.
|
||||
.#..#.#####.#.##..##..#..##..#.####.###......##.####.#.###.######...###..#..####.#...#.#..##....#...
|
||||
##..#.##.##....#..###.#...##..###.#.#..##..#.#..#.#...##.###.#####.#..#...#.#..##.#..##.####.#..#.##
|
||||
.#.##..####.#...#.##.#.#...#..##..#.#......#..###..#...#....###...##...##..##....#..###.##...#####.#
|
||||
#..#.#.#..##..#..###..#.#.###.......#####.##.##.##..#..###.#.#.#.######..###..#..###.###.#..####...#
|
||||
..###...##.##.##..##.#.##.##.#.######....####...##.#.###.#####...####.#...#...#.#..###....#.##...#.#
|
||||
#.###.##.#.##.#.##.#..##...##.#.####.###.###....#.##..#.##..#.#..#.#..##....#..#####...#####.##....#
|
||||
#...##.#####..#..###########.###.....###....###.#.#..#########.#.###.##..#.##.#.#....#.##.##.#.#.##.
|
||||
#...#...##..###....##.#...##..#..#..##.##...#..#...##....###.......#####..#...#.#..#....#.#.##.##.##
|
||||
.##...#..#.####.##...#..##..####..##..#..######.......#.#...#.#..#...#.#.#..###..#.##.##...##....###
|
||||
#######.#.#..#.#.###.##.##...#...#....#...##.#..#.##....#..#.#####..#.####...####.##.##..#####.##.##
|
||||
###..###....##...####.#.#.###......#.###...#.#..#...#####...#.######.#...#.##..##.#...##.#..##.###.#
|
||||
.#.....#.#..#.#...#.##.#.###....###..#.#.#.##...##....#....#...#.#..##.....#..#.....####.#..#..#....
|
||||
.###.##....#.#.#.#...#.####...#.##..##.#.##.##.#.####.#.#..####.####.##...###....###..##..##.#.#####
|
||||
...#.##.###.###.#.##.#.##.##..##.##..#..##..##.########.#..##...###.#.###...#...#.....#...#.....###.
|
||||
##.#.##...###...######.#.....#.#.....#.#.#.####..#..##..##.#####....###.#.##....####...#....##..#..#
|
||||
.#.##..##.##.##########..#..#.#..#####.#####..####.##.#...#.#.##..#..#.##..#...###....#..#.....#..#.
|
||||
###..##...##.#.###....####....#..##.###.....#####..#.#.#..####.####....######.##.#.###.#.####.##..#.
|
||||
#.##.#....#.#...##.#..####.#.##.###.##..##..###.#....##....#.#.#.#.#####.#######...##.##.#.....#...#
|
||||
..###.####.##.#.#..#..###.#.#...###..#...#.#.#...#.#.#.#....#.##.#.####...#..#...###...####..#.#..#.
|
||||
....##.#.####..###.##.###....#########..#.#..##..#....#..###...#.....#.###.##..#.#....###.#.#..#..#.
|
||||
..#....##########..##.#####.###.###.#....#.#..#.##.#...#..###.##..#..###..#######..##...##..#...#...
|
||||
#..#...............#.#.#.#.....###..##.....####..#...#...####.#......###.##.######....#...#......#..
|
||||
.....####.##...#....##..#....######.##.#...##.#.##...####...#.#..####..###.#.#.#######....#.###.##..
|
||||
...##.#.#####.#####.#..#.#...####......##.##.#..#.#.#..####.#######.##..##.##.....##.#.#.##..####..#
|
||||
##...##....#.#....#..#.###.#....#.###.##.#.##.##.###.##.....###.#..#..##...#.##.....###...######...#
|
||||
#....###.#.#########.#..#.#.##...######..##.#.#..##.###...#..#..###..#####.#..####.....####.#..#.#.#
|
||||
.##.###.##...#.###..###.#...#..###.##.##..#.....#....#..#.#...#.##..#..#.#..#.###.######.##.#.#...##
|
||||
.#..#.....##.#..##.#.....#..###...#####...#...#..#.#...#..#....#.#..#....###.###.#####..#.#..##.....
|
||||
#..#..#.#...##.##...#..#..####.###..###.#....#.#..#.#.####.#..#.###.###........###....#..###...#...#
|
||||
##.#.##....#.####..#.....##.........#.#...####...#...##.#########.#.#.##....#....#.##...##.....#####
|
||||
.##.#..#.#.#..####....###.#.##...#.....#....##.#.#..#####.###.....#.####...##.#..#####..#.#...#...##
|
||||
..######..#.###.###.#..#.#####.#.##.#.##..####.#....##.##...#.#..####...##...###..###...#.#####.##.#
|
||||
.#.#.###...#..####.#.#..#.#..#..#..###...#.#...####..##.##...#.#.##..###..###.##.#..###.#.#.#..###.#
|
||||
#..##..##..##.........#.#......#....#.#####....#..#.##.####.#.#.#...#.....#.#..#.###.#....###.....##
|
||||
..#####.#.##...#.#.#..#.....####......##..####.#..##....#...#.####.....##..###.##...###.#.###.###.##
|
||||
.#.##.##.#.######.#.....#.###..###....#.....###...#..##....#..##.#...######.....#.##..#####.#.#####.
|
||||
..#.###.#.##...######..#..#..###.#.#....##.#..#....#....###.#.#..##.#..#.##..##....#..##.#######..##
|
||||
.#.....##.#...#.####..#..###.####.###..##.#..#..#..#.###.###...#.#.##..#..###..####..###.#..#.##.#..
|
||||
##..######...####.##..#.#.######.#..#.####.#..#...#.#.##..#...###.#.....####..####.#...#.....#...#.#
|
||||
#.#.#....#..#.#...##.##..##.####.######...###.#.#..#..#.#.###..##.#..##.###..##..###.###.#...##..###
|
||||
..#####.#..##.##.#.####.###.#....##...#.###.##..#.#.#.###.#.#....#.#.#..###.##.#..#....###...#...##.
|
||||
....####..##.##.#....##..###.##.###..######....#.##.##..#..#####...##.....#..##..#.##..#.......#...#
|
||||
#...##.###..#..#.....#..#.##..#.######.....#.#######....#...######.####...#.##..#..###.#..#.......##
|
||||
###..##.########....###.#.##.###.#..#.####.#.###.###...##...##..####.#...##...#.###..#####.###....#.
|
||||
#.##..#..####..##..###.#.##.....##..##...#.###.#..#.##.##..#..##.###.#..###..###.#.##..#....#..###..
|
||||
..#######.####.....#.#######.#.#..#.#...#.#..###....#...#..###..##...#....#...#.#.##.##..#..#....#.#
|
||||
.....#.#..#..#...#.....##..#......#.##.#.#..#####..#.###.##.##...#.#...#...##.#.#####.#####..###..##
|
||||
.#.##..#.....#..#..####.#.##...##.#...###.#.#..#####.#.#.####..###..#.#.##..#..####.##.##..####..#..
|
||||
#.#####....##.......#...#.##..#..#..##......#...##..####..###...#..#..#.#.###...##.###.####..###..#.
|
||||
#.##...#.###.#.##..####..##..###.###..........####..#.##.....#.#..##########.#.##.#.#..#...##.#.##..
|
||||
#.#.#..##.#..###..###..##......###.#.##.#.#.###.##.###....#....#..###.#.#..####.#..#####.#######..#.
|
||||
##.##..####....#..##..##.#.#...##..#####.#.#...##..##..###..#.#.##..#.####...#.###..#.#.##......##.#
|
||||
##......#.#..##...#..#.#.#.....#...#....####.#.###.......###.#.###...#.#.#.#####.#....##..#.#.......
|
||||
###.##..##..###..##..######..#..#.#...#####..##..#...##.#...#..#.##..#..###.#....##.#.##..##.#.#.#.#
|
||||
####.##.#....#.###.#..#.#####..#.#..#..##...######.###...##..#.#....##.#..####..#...#.#.#.##..##....
|
||||
.####.#.##...#.##..#.##.#...##..######.###.#..#.###.#######.#.......#.#.###.#######.#.#.#.....##..#.
|
||||
59
day-21/README.md
Normal file
59
day-21/README.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# Day 21: Dirac Dice
|
||||
|
||||
[https://adventofcode.com/2021/day/21](https://adventofcode.com/2021/day/21)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
There's not much to do as you slowly descend to the bottom of the ocean. The submarine computer <span title="A STRANGE GAME.">challenges you to a nice game</span> of _Dirac Dice_.
|
||||
|
||||
This game consists of a single [die](https://en.wikipedia.org/wiki/Dice), two [pawns](https://en.wikipedia.org/wiki/Glossary_of_board_games#piece), and a game board with a circular track containing ten spaces marked `1` through `10` clockwise. Each player's _starting space_ is chosen randomly (your puzzle input). Player 1 goes first.
|
||||
|
||||
Players take turns moving. On each player's turn, the player rolls the die _three times_ and adds up the results. Then, the player moves their pawn that many times _forward_ around the track (that is, moving clockwise on spaces in order of increasing value, wrapping back around to `1` after `10`). So, if a player is on space `7` and they roll `2`, `2`, and `1`, they would move forward 5 times, to spaces `8`, `9`, `10`, `1`, and finally stopping on `2`.
|
||||
|
||||
After each player moves, they increase their _score_ by the value of the space their pawn stopped on. Players' scores start at `0`. So, if the first player starts on space `7` and rolls a total of `5`, they would stop on space `2` and add `2` to their score (for a total score of `2`). The game immediately ends as a win for any player whose score reaches _at least `1000`_.
|
||||
|
||||
Since the first game is a practice game, the submarine opens a compartment labeled _deterministic dice_ and a 100-sided die falls out. This die always rolls `1` first, then `2`, then `3`, and so on up to `100`, after which it starts over at `1` again. Play using this die.
|
||||
|
||||
For example, given these starting positions:
|
||||
|
||||
Player 1 starting position: 4
|
||||
Player 2 starting position: 8
|
||||
|
||||
|
||||
This is how the game would go:
|
||||
|
||||
* Player 1 rolls `1`+`2`+`3` and moves to space `10` for a total score of `10`.
|
||||
* Player 2 rolls `4`+`5`+`6` and moves to space `3` for a total score of `3`.
|
||||
* Player 1 rolls `7`+`8`+`9` and moves to space `4` for a total score of `14`.
|
||||
* Player 2 rolls `10`+`11`+`12` and moves to space `6` for a total score of `9`.
|
||||
* Player 1 rolls `13`+`14`+`15` and moves to space `6` for a total score of `20`.
|
||||
* Player 2 rolls `16`+`17`+`18` and moves to space `7` for a total score of `16`.
|
||||
* Player 1 rolls `19`+`20`+`21` and moves to space `6` for a total score of `26`.
|
||||
* Player 2 rolls `22`+`23`+`24` and moves to space `6` for a total score of `22`.
|
||||
|
||||
...after many turns...
|
||||
|
||||
* Player 2 rolls `82`+`83`+`84` and moves to space `6` for a total score of `742`.
|
||||
* Player 1 rolls `85`+`86`+`87` and moves to space `4` for a total score of `990`.
|
||||
* Player 2 rolls `88`+`89`+`90` and moves to space `3` for a total score of `745`.
|
||||
* Player 1 rolls `91`+`92`+`93` and moves to space `10` for a final score, `1000`.
|
||||
|
||||
Since player 1 has at least `1000` points, player 1 wins and the game ends. At this point, the losing player had `745` points and the die had been rolled a total of `993` times; `745 * 993 = 739785`.
|
||||
|
||||
Play a practice game using the deterministic 100-sided die. The moment either player wins, _what do you get if you multiply the score of the losing player by the number of times the die was rolled during the game?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Now that you're warmed up, it's time to play the real game.
|
||||
|
||||
A second compartment opens, this time labeled _Dirac dice_. Out of it falls a single three-sided die.
|
||||
|
||||
As you experiment with the die, you feel a little strange. An informational brochure in the compartment explains that this is a _quantum die_: when you roll it, the universe _splits into multiple copies_, one copy for each possible outcome of the die. In this case, rolling the die always splits the universe into _three copies_: one where the outcome of the roll was `1`, one where it was `2`, and one where it was `3`.
|
||||
|
||||
The game is played the same as before, although to prevent things from getting too far out of hand, the game now ends when either player's score reaches at least _`21`_.
|
||||
|
||||
Using the same starting positions as in the example above, player 1 wins in _`444356092776315`_ universes, while player 2 merely wins in `341960390180808` universes.
|
||||
|
||||
Using your given starting positions, determine every possible outcome. _Find the player that wins in more universes; in how many universes does that player win?_
|
||||
59
day-21/day-21.py
Normal file
59
day-21/day-21.py
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def quantum_roll(state, p0, p1, s0, s1):
|
||||
if s0 >= 21:
|
||||
return (1, 0)
|
||||
if s1 >= 21:
|
||||
return (0, 1)
|
||||
if (p0, p1, s0, s1) in state:
|
||||
return state[(p0, p1, s0, s1)]
|
||||
res = (0, 0)
|
||||
for d0 in [1, 2, 3]:
|
||||
for d1 in [1, 2, 3]:
|
||||
for d2 in [1, 2, 3]:
|
||||
new_p0 = (p0 + d0 + d1 + d2) % 10
|
||||
new_s0 = s0 + new_p0 + 1
|
||||
(w1, w0) = quantum_roll(state, p1, new_p0, s1, new_s0)
|
||||
res = (res[0] + w0, res[1] + w1)
|
||||
state[(p0, p1, s0, s1)] = res
|
||||
return res
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
pos = [int(line.rstrip().split()[-1]) - 1 for line in input]
|
||||
score = [0, 0]
|
||||
p = 0
|
||||
dice = 1
|
||||
while True:
|
||||
pos[p] += sum(range(dice, dice + 3))
|
||||
pos[p] %= 10
|
||||
score[p] += pos[p] + 1
|
||||
dice += 3
|
||||
if 1000 <= score[p]:
|
||||
p += 1
|
||||
p %= 2
|
||||
break
|
||||
p += 1
|
||||
p %= 2
|
||||
result = score[p] * (dice - 1)
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
state = {}
|
||||
p0, p1 = [int(line.rstrip().split()[-1]) - 1 for line in input]
|
||||
result = max(quantum_roll(state, p0, p1, 0, 0))
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
2
day-21/input.txt
Normal file
2
day-21/input.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Player 1 starting position: 4
|
||||
Player 2 starting position: 10
|
||||
192
day-22/README.md
Normal file
192
day-22/README.md
Normal file
@@ -0,0 +1,192 @@
|
||||
# Day 22: Reactor Reboot
|
||||
|
||||
[https://adventofcode.com/2021/day/22](https://adventofcode.com/2021/day/22)
|
||||
|
||||
## Description
|
||||
|
||||
### Part One
|
||||
|
||||
Operating at these extreme ocean depths has overloaded the submarine's reactor; it needs to be rebooted.
|
||||
|
||||
The reactor core is made up of a large 3-dimensional grid made up entirely of cubes, one cube per integer 3-dimensional coordinate (`x,y,z`). Each cube can be either _on_ or _off_; at the start of the reboot process, they are all _off_. (Could it be an old model of a reactor you've seen [before](https://adventofcode.com/2020/day/17)?)
|
||||
|
||||
To reboot the reactor, you just need to set all of the cubes to either _on_ or _off_ by following a list of _reboot steps_ (your puzzle input). Each step specifies a [cuboid](https://en.wikipedia.org/wiki/Cuboid) (the set of all cubes that have coordinates which fall within ranges for `x`, `y`, and `z`) and whether to turn all of the cubes in that cuboid _on_ or _off_.
|
||||
|
||||
For example, given these reboot steps:
|
||||
|
||||
on x=10..12,y=10..12,z=10..12
|
||||
on x=11..13,y=11..13,z=11..13
|
||||
off x=9..11,y=9..11,z=9..11
|
||||
on x=10..10,y=10..10,z=10..10
|
||||
|
||||
|
||||
The first step (`on x=10..12,y=10..12,z=10..12`) turns _on_ a 3x3x3 cuboid consisting of 27 cubes:
|
||||
|
||||
* `10,10,10`
|
||||
* `10,10,11`
|
||||
* `10,10,12`
|
||||
* `10,11,10`
|
||||
* `10,11,11`
|
||||
* `10,11,12`
|
||||
* `10,12,10`
|
||||
* `10,12,11`
|
||||
* `10,12,12`
|
||||
* `11,10,10`
|
||||
* `11,10,11`
|
||||
* `11,10,12`
|
||||
* `11,11,10`
|
||||
* `11,11,11`
|
||||
* `11,11,12`
|
||||
* `11,12,10`
|
||||
* `11,12,11`
|
||||
* `11,12,12`
|
||||
* `12,10,10`
|
||||
* `12,10,11`
|
||||
* `12,10,12`
|
||||
* `12,11,10`
|
||||
* `12,11,11`
|
||||
* `12,11,12`
|
||||
* `12,12,10`
|
||||
* `12,12,11`
|
||||
* `12,12,12`
|
||||
|
||||
The second step (`on x=11..13,y=11..13,z=11..13`) turns _on_ a 3x3x3 cuboid that overlaps with the first. As a result, only 19 additional cubes turn on; the rest are already on from the previous step:
|
||||
|
||||
* `11,11,13`
|
||||
* `11,12,13`
|
||||
* `11,13,11`
|
||||
* `11,13,12`
|
||||
* `11,13,13`
|
||||
* `12,11,13`
|
||||
* `12,12,13`
|
||||
* `12,13,11`
|
||||
* `12,13,12`
|
||||
* `12,13,13`
|
||||
* `13,11,11`
|
||||
* `13,11,12`
|
||||
* `13,11,13`
|
||||
* `13,12,11`
|
||||
* `13,12,12`
|
||||
* `13,12,13`
|
||||
* `13,13,11`
|
||||
* `13,13,12`
|
||||
* `13,13,13`
|
||||
|
||||
The third step (`off x=9..11,y=9..11,z=9..11`) turns _off_ a 3x3x3 cuboid that overlaps partially with some cubes that are on, ultimately turning off 8 cubes:
|
||||
|
||||
* `10,10,10`
|
||||
* `10,10,11`
|
||||
* `10,11,10`
|
||||
* `10,11,11`
|
||||
* `11,10,10`
|
||||
* `11,10,11`
|
||||
* `11,11,10`
|
||||
* `11,11,11`
|
||||
|
||||
The final step (`on x=10..10,y=10..10,z=10..10`) turns _on_ a single cube, `10,10,10`. After this last step, _`39`_ cubes are _on_.
|
||||
|
||||
The initialization procedure only uses cubes that have `x`, `y`, and `z` positions of at least `-50` and at most `50`. For now, ignore cubes outside this region.
|
||||
|
||||
Here is a larger example:
|
||||
|
||||
on x=-20..26,y=-36..17,z=-47..7
|
||||
on x=-20..33,y=-21..23,z=-26..28
|
||||
on x=-22..28,y=-29..23,z=-38..16
|
||||
on x=-46..7,y=-6..46,z=-50..-1
|
||||
on x=-49..1,y=-3..46,z=-24..28
|
||||
on x=2..47,y=-22..22,z=-23..27
|
||||
on x=-27..23,y=-28..26,z=-21..29
|
||||
on x=-39..5,y=-6..47,z=-3..44
|
||||
on x=-30..21,y=-8..43,z=-13..34
|
||||
on x=-22..26,y=-27..20,z=-29..19
|
||||
off x=-48..-32,y=26..41,z=-47..-37
|
||||
on x=-12..35,y=6..50,z=-50..-2
|
||||
off x=-48..-32,y=-32..-16,z=-15..-5
|
||||
on x=-18..26,y=-33..15,z=-7..46
|
||||
off x=-40..-22,y=-38..-28,z=23..41
|
||||
on x=-16..35,y=-41..10,z=-47..6
|
||||
off x=-32..-23,y=11..30,z=-14..3
|
||||
on x=-49..-5,y=-3..45,z=-29..18
|
||||
off x=18..30,y=-20..-8,z=-3..13
|
||||
on x=-41..9,y=-7..43,z=-33..15
|
||||
on x=-54112..-39298,y=-85059..-49293,z=-27449..7877
|
||||
on x=967..23432,y=45373..81175,z=27513..53682
|
||||
|
||||
|
||||
The last two steps are fully outside the initialization procedure area; all other steps are fully within it. After executing these steps in the initialization procedure region, _`590784`_ cubes are _on_.
|
||||
|
||||
Execute the reboot steps. Afterward, considering only cubes in the region `x=-50..50,y=-50..50,z=-50..50`, _how many cubes are on?_
|
||||
|
||||
### Part Two
|
||||
|
||||
Now that the initialization procedure is complete, you can reboot the reactor.
|
||||
|
||||
Starting with all cubes _off_, run all of the _reboot steps_ for all cubes in the reactor.
|
||||
|
||||
Consider the following reboot steps:
|
||||
|
||||
on x=-5..47,y=-31..22,z=-19..33
|
||||
on x=-44..5,y=-27..21,z=-14..35
|
||||
on x=-49..-1,y=-11..42,z=-10..38
|
||||
on x=-20..34,y=-40..6,z=-44..1
|
||||
off x=26..39,y=40..50,z=-2..11
|
||||
on x=-41..5,y=-41..6,z=-36..8
|
||||
off x=-43..-33,y=-45..-28,z=7..25
|
||||
on x=-33..15,y=-32..19,z=-34..11
|
||||
off x=35..47,y=-46..-34,z=-11..5
|
||||
on x=-14..36,y=-6..44,z=-16..29
|
||||
on x=-57795..-6158,y=29564..72030,z=20435..90618
|
||||
on x=36731..105352,y=-21140..28532,z=16094..90401
|
||||
on x=30999..107136,y=-53464..15513,z=8553..71215
|
||||
on x=13528..83982,y=-99403..-27377,z=-24141..23996
|
||||
on x=-72682..-12347,y=18159..111354,z=7391..80950
|
||||
on x=-1060..80757,y=-65301..-20884,z=-103788..-16709
|
||||
on x=-83015..-9461,y=-72160..-8347,z=-81239..-26856
|
||||
on x=-52752..22273,y=-49450..9096,z=54442..119054
|
||||
on x=-29982..40483,y=-108474..-28371,z=-24328..38471
|
||||
on x=-4958..62750,y=40422..118853,z=-7672..65583
|
||||
on x=55694..108686,y=-43367..46958,z=-26781..48729
|
||||
on x=-98497..-18186,y=-63569..3412,z=1232..88485
|
||||
on x=-726..56291,y=-62629..13224,z=18033..85226
|
||||
on x=-110886..-34664,y=-81338..-8658,z=8914..63723
|
||||
on x=-55829..24974,y=-16897..54165,z=-121762..-28058
|
||||
on x=-65152..-11147,y=22489..91432,z=-58782..1780
|
||||
on x=-120100..-32970,y=-46592..27473,z=-11695..61039
|
||||
on x=-18631..37533,y=-124565..-50804,z=-35667..28308
|
||||
on x=-57817..18248,y=49321..117703,z=5745..55881
|
||||
on x=14781..98692,y=-1341..70827,z=15753..70151
|
||||
on x=-34419..55919,y=-19626..40991,z=39015..114138
|
||||
on x=-60785..11593,y=-56135..2999,z=-95368..-26915
|
||||
on x=-32178..58085,y=17647..101866,z=-91405..-8878
|
||||
on x=-53655..12091,y=50097..105568,z=-75335..-4862
|
||||
on x=-111166..-40997,y=-71714..2688,z=5609..50954
|
||||
on x=-16602..70118,y=-98693..-44401,z=5197..76897
|
||||
on x=16383..101554,y=4615..83635,z=-44907..18747
|
||||
off x=-95822..-15171,y=-19987..48940,z=10804..104439
|
||||
on x=-89813..-14614,y=16069..88491,z=-3297..45228
|
||||
on x=41075..99376,y=-20427..49978,z=-52012..13762
|
||||
on x=-21330..50085,y=-17944..62733,z=-112280..-30197
|
||||
on x=-16478..35915,y=36008..118594,z=-7885..47086
|
||||
off x=-98156..-27851,y=-49952..43171,z=-99005..-8456
|
||||
off x=2032..69770,y=-71013..4824,z=7471..94418
|
||||
on x=43670..120875,y=-42068..12382,z=-24787..38892
|
||||
off x=37514..111226,y=-45862..25743,z=-16714..54663
|
||||
off x=25699..97951,y=-30668..59918,z=-15349..69697
|
||||
off x=-44271..17935,y=-9516..60759,z=49131..112598
|
||||
on x=-61695..-5813,y=40978..94975,z=8655..80240
|
||||
off x=-101086..-9439,y=-7088..67543,z=33935..83858
|
||||
off x=18020..114017,y=-48931..32606,z=21474..89843
|
||||
off x=-77139..10506,y=-89994..-18797,z=-80..59318
|
||||
off x=8476..79288,y=-75520..11602,z=-96624..-24783
|
||||
on x=-47488..-1262,y=24338..100707,z=16292..72967
|
||||
off x=-84341..13987,y=2429..92914,z=-90671..-1318
|
||||
off x=-37810..49457,y=-71013..-7894,z=-105357..-13188
|
||||
off x=-27365..46395,y=31009..98017,z=15428..76570
|
||||
off x=-70369..-16548,y=22648..78696,z=-1892..86821
|
||||
on x=-53470..21291,y=-120233..-33476,z=-44150..38147
|
||||
off x=-93533..-4276,y=-16170..68771,z=-104985..-24507
|
||||
|
||||
|
||||
After running the above reboot steps, _`2758514936282235`_ cubes are _on_. (Just for <span title="Well, *I* think it's fun.">fun</span>, `474140` of those are also in the initialization procedure region.)
|
||||
|
||||
Starting again with all cubes _off_, execute all reboot steps. Afterward, considering all cubes, _how many cubes are on?_
|
||||
87
day-22/day-22.py
Normal file
87
day-22/day-22.py
Normal file
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
from collections import Counter
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
cubes = Counter()
|
||||
for line in input:
|
||||
cmd, cube = line.rstrip().split()
|
||||
x, y, z = cube.split(',')
|
||||
xn_min, xn_max = [int(c) for c in x.split('=')[1].split('..')]
|
||||
yn_min, yn_max = [int(c) for c in y.split('=')[1].split('..')]
|
||||
zn_min, zn_max = [int(c) for c in z.split('=')[1].split('..')]
|
||||
xn_min = xn_min if xn_min > -50 else -50
|
||||
xn_max = xn_max if xn_max < 51 else 51
|
||||
yn_min = yn_min if yn_min > -50 else -50
|
||||
yn_max = yn_max if yn_max < 51 else 51
|
||||
zn_min = zn_min if zn_min > -50 else -50
|
||||
zn_max = zn_max if zn_max < 51 else 51
|
||||
sn = 1 if 'on' == cmd else -1
|
||||
if xn_max < xn_min or yn_max < yn_min or zn_max < zn_min:
|
||||
continue
|
||||
update = Counter()
|
||||
for (xi_min, xi_max, yi_min, yi_max, zi_min, zi_max), si in cubes.items():
|
||||
x_min = xn_min if xn_min > xi_min else xi_min
|
||||
x_max = xn_max if xn_max < xi_max else xi_max
|
||||
y_min = yn_min if yn_min > yi_min else yi_min
|
||||
y_max = yn_max if yn_max < yi_max else yi_max
|
||||
z_min = zn_min if zn_min > zi_min else zi_min
|
||||
z_max = zn_max if zn_max < zi_max else zi_max
|
||||
if x_min <= x_max and y_min <= y_max and z_min <= z_max:
|
||||
update[(x_min, x_max, y_min, y_max, z_min, z_max)] -= si
|
||||
if sn > 0:
|
||||
update[(xn_min, xn_max, yn_min, yn_max, zn_min, zn_max)] += sn
|
||||
cubes.update(update)
|
||||
to_delete = []
|
||||
for c in cubes:
|
||||
if not cubes[c]:
|
||||
to_delete.append(c)
|
||||
for d in to_delete:
|
||||
del cubes[d]
|
||||
result = sum((x1 - x0 + 1) * (y1 - y0 + 1) * (z1 - z0 + 1) * sgn
|
||||
for (x0, x1, y0, y1, z0, z1), sgn in cubes.items())
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
cubes = Counter()
|
||||
for line in input:
|
||||
cmd, cube = line.rstrip().split()
|
||||
x, y, z = cube.split(',')
|
||||
xn_min, xn_max = [int(c) for c in x.split('=')[1].split('..')]
|
||||
yn_min, yn_max = [int(c) for c in y.split('=')[1].split('..')]
|
||||
zn_min, zn_max = [int(c) for c in z.split('=')[1].split('..')]
|
||||
sn = 1 if 'on' == cmd else -1
|
||||
update = Counter()
|
||||
for (xi_min, xi_max, yi_min, yi_max, zi_min, zi_max), si in cubes.items():
|
||||
x_min = xn_min if xn_min > xi_min else xi_min
|
||||
x_max = xn_max if xn_max < xi_max else xi_max
|
||||
y_min = yn_min if yn_min > yi_min else yi_min
|
||||
y_max = yn_max if yn_max < yi_max else yi_max
|
||||
z_min = zn_min if zn_min > zi_min else zi_min
|
||||
z_max = zn_max if zn_max < zi_max else zi_max
|
||||
if x_min <= x_max and y_min <= y_max and z_min <= z_max:
|
||||
update[(x_min, x_max, y_min, y_max, z_min, z_max)] -= si
|
||||
if sn > 0:
|
||||
update[(xn_min, xn_max, yn_min, yn_max, zn_min, zn_max)] += sn
|
||||
cubes.update(update)
|
||||
to_delete = []
|
||||
for c in cubes:
|
||||
if not cubes[c]:
|
||||
to_delete.append(c)
|
||||
for d in to_delete:
|
||||
del cubes[d]
|
||||
result = sum((x1 - x0 + 1) * (y1 - y0 + 1) * (z1 - z0 + 1) * sgn
|
||||
for (x0, x1, y0, y1, z0, z1), sgn in cubes.items())
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
420
day-22/input.txt
Normal file
420
day-22/input.txt
Normal file
@@ -0,0 +1,420 @@
|
||||
on x=-12..41,y=-1..48,z=-27..19
|
||||
on x=-40..7,y=-47..2,z=-24..22
|
||||
on x=-11..36,y=-29..18,z=-46..0
|
||||
on x=-21..23,y=-18..32,z=-14..39
|
||||
on x=-49..-4,y=-44..10,z=-38..15
|
||||
on x=-29..22,y=-17..33,z=-42..2
|
||||
on x=-20..26,y=-41..13,z=-27..22
|
||||
on x=-11..35,y=-34..16,z=-13..33
|
||||
on x=-1..43,y=-34..11,z=-48..1
|
||||
on x=-32..20,y=-38..13,z=-23..23
|
||||
off x=0..18,y=-9..2,z=-23..-8
|
||||
on x=-11..38,y=-8..42,z=4..48
|
||||
off x=30..42,y=-32..-18,z=4..14
|
||||
on x=-32..19,y=-45..-1,z=-25..22
|
||||
off x=18..33,y=5..19,z=16..30
|
||||
on x=-45..1,y=-40..14,z=-28..18
|
||||
off x=-35..-20,y=-41..-24,z=-26..-12
|
||||
on x=-9..35,y=-18..27,z=-30..18
|
||||
off x=5..16,y=-26..-15,z=5..21
|
||||
on x=-22..27,y=-31..19,z=-36..16
|
||||
on x=-70003..-58017,y=-28949..-6824,z=34637..57201
|
||||
on x=-7511..14289,y=71993..81295,z=-14182..1065
|
||||
on x=41496..62331,y=28739..63867,z=-46404..-22233
|
||||
on x=-52024..-35937,y=41715..68830,z=-50871..-12843
|
||||
on x=57864..70702,y=-52912..-30880,z=13502..34524
|
||||
on x=68548..84052,y=-34332..-28083,z=-15694..13805
|
||||
on x=19572..46018,y=-91175..-66184,z=-19354..5173
|
||||
on x=5001..25611,y=-34428..-3278,z=-81501..-67639
|
||||
on x=28955..45739,y=3796..16038,z=55192..88006
|
||||
on x=-2321..12926,y=-82562..-76867,z=-9287..15586
|
||||
on x=-17907..2536,y=8413..19854,z=70416..89725
|
||||
on x=4653..28469,y=63236..67499,z=22339..51748
|
||||
on x=-8654..10864,y=16840..34644,z=63195..79095
|
||||
on x=-30124..-21549,y=57165..72520,z=-36955..-26429
|
||||
on x=17391..30238,y=-54972..-20496,z=-81951..-51987
|
||||
on x=46168..56617,y=43136..70418,z=3380..27308
|
||||
on x=1615..17921,y=48397..83812,z=43269..45469
|
||||
on x=-79091..-47773,y=36153..58425,z=-22888..-11010
|
||||
on x=57466..79937,y=30666..59763,z=11646..33794
|
||||
on x=-29725..-8310,y=-75680..-63108,z=-43531..-39143
|
||||
on x=-79207..-59591,y=16942..30273,z=12337..45526
|
||||
on x=-8272..18794,y=70196..93422,z=12844..26949
|
||||
on x=-10016..10763,y=22684..42075,z=-81405..-61351
|
||||
on x=-63057..-36503,y=-20032..-4256,z=-65067..-49139
|
||||
on x=-3284..20447,y=-68897..-53056,z=-52160..-22119
|
||||
on x=41716..68477,y=-12125..3683,z=48943..63237
|
||||
on x=-56443..-44652,y=56383..65978,z=6596..32508
|
||||
on x=-64063..-55988,y=-50143..-29492,z=-19329..-7290
|
||||
on x=-40307..-24457,y=-2859..12193,z=68883..85116
|
||||
on x=41615..53213,y=3132..32896,z=55879..59067
|
||||
on x=60411..89514,y=18265..48124,z=-29076..-5776
|
||||
on x=-6668..9186,y=21495..34426,z=67739..90223
|
||||
on x=-31149..-17374,y=282..26729,z=-82337..-66347
|
||||
on x=-28982..-12550,y=-52493..-33002,z=57631..73424
|
||||
on x=75153..85406,y=-6097..32097,z=-2048..22399
|
||||
on x=-39777..-24306,y=40742..65550,z=-70805..-48884
|
||||
on x=50447..61226,y=13927..44884,z=-68517..-41950
|
||||
on x=14069..49966,y=58045..73769,z=-49109..-17154
|
||||
on x=-18059..7973,y=-59119..-42202,z=53942..76180
|
||||
on x=74116..80753,y=-15199..-4482,z=-22883..-6218
|
||||
on x=-30137..-3472,y=-78265..-76268,z=-3856..20738
|
||||
on x=-27525..4210,y=-48999..-30036,z=-66522..-61008
|
||||
on x=-71598..-42691,y=-19346..5442,z=50080..76093
|
||||
on x=36986..50946,y=35576..47733,z=-53704..-42441
|
||||
on x=-20729..5473,y=-62540..-45007,z=-69672..-63211
|
||||
on x=-78793..-57347,y=-15855..16836,z=31142..53183
|
||||
on x=-43253..-27154,y=-80214..-54484,z=-6923..15043
|
||||
on x=21524..51205,y=-78788..-44373,z=31296..45389
|
||||
on x=20477..43834,y=-28619..-9238,z=-89857..-66901
|
||||
on x=-53104..-43676,y=-80037..-42706,z=-8427..-403
|
||||
on x=-78578..-48390,y=-43339..-31792,z=-48259..-30921
|
||||
on x=-18776..8984,y=-6854..6443,z=-93619..-71328
|
||||
on x=-62431..-29429,y=2162..17928,z=-75206..-46164
|
||||
on x=-23898..-4559,y=72332..96734,z=-1414..24935
|
||||
on x=26788..43942,y=36629..57271,z=29772..58885
|
||||
on x=-27121..2320,y=58018..89127,z=-779..25015
|
||||
on x=-14496..14812,y=74395..85109,z=-6120..20267
|
||||
on x=-63045..-54273,y=-61347..-50137,z=12633..16469
|
||||
on x=-36851..-17397,y=68360..81259,z=13658..30867
|
||||
on x=-53037..-31783,y=-43819..-18209,z=48068..78578
|
||||
on x=-20337..-10002,y=-11452..26931,z=74135..95181
|
||||
on x=63991..88424,y=1144..17375,z=31769..37039
|
||||
on x=-60710..-33180,y=-57971..-39278,z=27392..48256
|
||||
on x=-53448..-29454,y=-40070..-13246,z=51989..72994
|
||||
on x=-58051..-28559,y=36540..60271,z=-51506..-36323
|
||||
on x=27620..48354,y=16398..31436,z=59161..78236
|
||||
on x=-44171..-37013,y=53874..78533,z=-38019..-17395
|
||||
on x=40393..54549,y=-59961..-33826,z=-35509..-13584
|
||||
on x=-42360..-27573,y=40941..67687,z=44787..57727
|
||||
on x=6083..24471,y=-69836..-33822,z=-70052..-59163
|
||||
on x=63695..66831,y=-41939..-27767,z=-48427..-24997
|
||||
on x=-16966..-742,y=32877..47896,z=60792..80117
|
||||
on x=45365..58036,y=41440..75828,z=-23904..-3340
|
||||
on x=-67715..-41781,y=-58082..-40366,z=-53669..-27574
|
||||
on x=-31486..-30595,y=-11025..-7711,z=65390..77981
|
||||
on x=-84518..-56987,y=-50238..-16053,z=7898..28429
|
||||
on x=-38856..-18199,y=2769..19471,z=-88402..-67533
|
||||
on x=-43581..-20942,y=-75779..-50858,z=10839..33762
|
||||
on x=67011..70745,y=27669..40298,z=-34724..-13350
|
||||
on x=62761..77222,y=-22986..11140,z=-51193..-17569
|
||||
on x=-40001..-34281,y=47180..75328,z=-31614..-10059
|
||||
on x=64666..87416,y=2690..10109,z=7999..41143
|
||||
on x=-42148..-14892,y=-42763..-28296,z=58830..70600
|
||||
on x=-75685..-55843,y=10307..35011,z=29216..55451
|
||||
on x=-22560..-13554,y=-3439..12443,z=-85612..-67440
|
||||
on x=-54051..-32030,y=-50066..-13171,z=44597..78006
|
||||
on x=68219..79051,y=-16670..-1539,z=-20617..-4436
|
||||
on x=-77924..-54659,y=-19725..-3491,z=18391..41280
|
||||
on x=-39009..-20619,y=33696..45286,z=-68547..-57520
|
||||
on x=-15764..9807,y=-89285..-62437,z=-42159..-17375
|
||||
on x=-60394..-35993,y=53104..86109,z=746..25326
|
||||
on x=-68451..-56368,y=20849..51542,z=12898..35335
|
||||
on x=11358..38211,y=61548..91789,z=-9967..6379
|
||||
on x=12919..30664,y=33943..64058,z=-77691..-55745
|
||||
on x=55573..69815,y=-2052..10422,z=38302..53488
|
||||
on x=5076..29348,y=61677..85518,z=-26379..-12140
|
||||
on x=49939..67865,y=12998..27092,z=44432..56410
|
||||
on x=-70441..-57152,y=-791..18633,z=-54891..-23235
|
||||
on x=-53653..-30543,y=-33763..-1919,z=66075..75308
|
||||
on x=-67798..-63867,y=-24008..-7243,z=-53174..-39718
|
||||
on x=-40022..-22503,y=-75528..-74474,z=-21233..-4581
|
||||
on x=23601..34328,y=14894..41533,z=-80685..-60190
|
||||
on x=-13066..15928,y=-92552..-63413,z=-20376..-9838
|
||||
on x=-49892..-43115,y=60463..79397,z=-22566..-6597
|
||||
on x=56284..69429,y=47483..67335,z=-26130..-1957
|
||||
on x=67867..75026,y=1694..25447,z=-54082..-22987
|
||||
on x=62296..81718,y=-20000..907,z=14439..43290
|
||||
on x=49886..69148,y=29808..51175,z=-33178..-20961
|
||||
on x=-89551..-70430,y=2757..13331,z=25520..36996
|
||||
on x=-48418..-33657,y=-71128..-47860,z=-26141..-17130
|
||||
on x=-44318..-21630,y=5402..23405,z=-89540..-61708
|
||||
on x=40469..48747,y=8098..28230,z=43525..63687
|
||||
on x=-38036..-8983,y=18299..27638,z=-77380..-56859
|
||||
on x=-75705..-56407,y=7861..27680,z=-51037..-34139
|
||||
on x=912..37237,y=-91682..-75217,z=-27426..8968
|
||||
on x=38885..61197,y=43451..53890,z=-43305..-5432
|
||||
on x=-84951..-50234,y=-10400..9610,z=37177..52716
|
||||
on x=-11951..7996,y=63909..75792,z=20464..40501
|
||||
on x=53587..85492,y=-29752..-6057,z=16752..42226
|
||||
on x=-69542..-63786,y=-9748..1328,z=-55963..-40728
|
||||
on x=-47410..-35603,y=-79036..-48393,z=14262..27382
|
||||
on x=-63004..-51874,y=15318..39261,z=-63356..-44013
|
||||
on x=6035..22314,y=-91660..-71249,z=12376..40148
|
||||
on x=56349..79243,y=26964..46599,z=2405..26287
|
||||
on x=-61362..-49465,y=35052..50540,z=33752..41310
|
||||
on x=-57122..-31360,y=-61049..-51330,z=18429..52777
|
||||
on x=72938..82895,y=-7349..10258,z=-10322..12590
|
||||
on x=20847..36770,y=-30373..-12401,z=51790..80140
|
||||
on x=-33702..-11453,y=-88000..-65975,z=-27949..1788
|
||||
on x=21101..42098,y=-33686..-19107,z=-78865..-63117
|
||||
on x=-66086..-43879,y=-34662..-5262,z=-76011..-50225
|
||||
on x=-37086..-24625,y=-73278..-65296,z=-37345..-22697
|
||||
on x=-14271..-2833,y=54891..79533,z=-44895..-43635
|
||||
on x=30458..54853,y=47685..67677,z=15350..41189
|
||||
on x=-88040..-65134,y=-48481..-27025,z=-14808..5096
|
||||
on x=-19178..7122,y=12953..30932,z=57150..95617
|
||||
on x=-30909..-9452,y=68296..90846,z=-27217..-6574
|
||||
on x=-7797..15301,y=-35634..-8361,z=55266..85365
|
||||
on x=42528..53145,y=8363..42394,z=-56708..-42378
|
||||
on x=-22028..-12408,y=49515..69518,z=52806..66694
|
||||
on x=-50326..-22636,y=60260..84018,z=-37235..-10401
|
||||
on x=18641..32553,y=-1551..20824,z=65767..82140
|
||||
on x=-14798..2910,y=16332..18314,z=59983..85220
|
||||
on x=-30825..-14957,y=64605..81347,z=-19408..-665
|
||||
on x=20356..44843,y=-59058..-24602,z=-63510..-49311
|
||||
on x=-65073..-49771,y=52694..53553,z=-1662..2142
|
||||
on x=-36268..-3672,y=-5911..2173,z=-84764..-62930
|
||||
on x=17214..33307,y=25948..45418,z=65227..87293
|
||||
on x=54256..72176,y=19200..53176,z=-24001..-1154
|
||||
on x=-28161..-6767,y=56080..69308,z=-55554..-34389
|
||||
on x=-4781..13451,y=-72029..-57606,z=21375..37185
|
||||
on x=-9221..16225,y=67099..83595,z=8..28701
|
||||
on x=51017..76241,y=37451..52318,z=-9740..13958
|
||||
on x=-37516..-20125,y=-24414..7208,z=-82252..-67529
|
||||
on x=54696..81526,y=31010..58109,z=-28810..6757
|
||||
on x=27352..49784,y=-8560..23076,z=-76248..-72079
|
||||
on x=-44392..-23178,y=-83163..-54734,z=-35916..-19210
|
||||
on x=23058..39659,y=-75066..-59087,z=-45038..-21011
|
||||
on x=-32007..-19199,y=50334..67939,z=-66870..-41822
|
||||
on x=-55482..-16449,y=29686..43681,z=-67451..-49661
|
||||
on x=67412..78219,y=8576..25353,z=26991..50827
|
||||
on x=-32997..-13431,y=33527..54011,z=44261..72783
|
||||
on x=-65226..-44630,y=-60999..-42188,z=-17980..7398
|
||||
on x=-53879..-30199,y=-21372..-4591,z=53471..80119
|
||||
on x=-81750..-73038,y=-12391..16058,z=-27281..-10715
|
||||
on x=-28745..-14037,y=21021..22966,z=53138..75400
|
||||
on x=-11847..-906,y=-87259..-69841,z=15488..37867
|
||||
on x=-79232..-64835,y=26964..55272,z=-7542..9579
|
||||
on x=51390..77199,y=42440..44102,z=20345..37107
|
||||
on x=32634..53125,y=-35711..-9036,z=43392..61586
|
||||
on x=14442..16830,y=-93151..-72116,z=-13713..3490
|
||||
on x=15733..40825,y=-57715..-25371,z=45461..71637
|
||||
on x=52317..74731,y=-28163..-17460,z=25458..58045
|
||||
on x=-27564..2211,y=46580..66691,z=-54415..-32613
|
||||
on x=-51530..-41067,y=-61809..-40596,z=20641..42195
|
||||
on x=-65040..-41924,y=10815..29967,z=-77327..-54738
|
||||
on x=-20238..-12700,y=-54744..-34485,z=-67042..-58487
|
||||
on x=-30616..-15015,y=-84194..-69773,z=-18784..-4851
|
||||
on x=49579..53086,y=22207..53808,z=32578..49883
|
||||
on x=-38888..-20901,y=19502..46376,z=-83484..-57288
|
||||
on x=-62089..-36673,y=-62257..-38961,z=20371..48919
|
||||
on x=53618..91792,y=24026..42487,z=3373..5871
|
||||
on x=-24526..-10081,y=-55138..-33071,z=63307..83130
|
||||
on x=42325..44884,y=-77603..-43509,z=22511..49877
|
||||
on x=51090..80417,y=-44131..-18955,z=-58555..-23094
|
||||
on x=-51830..-16677,y=-31597..-23990,z=-74200..-50560
|
||||
on x=39021..49504,y=-58936..-51403,z=37753..43930
|
||||
on x=-57446..-32174,y=14564..36620,z=-55988..-50797
|
||||
on x=-81709..-60903,y=-43025..-29712,z=-38184..-15474
|
||||
on x=-42860..-31195,y=-15111..-1558,z=70235..83004
|
||||
on x=-40281..-22031,y=69888..82776,z=5755..12513
|
||||
on x=-8526..6810,y=-7076..398,z=69909..94414
|
||||
on x=-87253..-50917,y=11988..23863,z=-45437..-24458
|
||||
on x=52749..65867,y=-64687..-49116,z=-37429..-14336
|
||||
on x=-52376..-25612,y=12421..45814,z=-80178..-58192
|
||||
on x=15855..33455,y=2642..28712,z=-84291..-70575
|
||||
on x=62333..83647,y=-33373..-10852,z=-52945..-33425
|
||||
on x=71964..92089,y=14355..25699,z=-13814..1674
|
||||
on x=-48437..-21995,y=-50417..-39938,z=58338..61260
|
||||
on x=-18072..3100,y=-65604..-38589,z=-61622..-42140
|
||||
off x=21822..31051,y=-31034..-20646,z=68265..89261
|
||||
off x=57606..89660,y=-9179..20933,z=-46591..-14625
|
||||
on x=40081..62029,y=-54900..-39726,z=-32377..-19297
|
||||
off x=-34741..-8427,y=-73789..-49715,z=-57100..-25783
|
||||
on x=38187..69064,y=36824..65615,z=13282..19202
|
||||
on x=-75841..-54039,y=27188..47461,z=16614..31882
|
||||
on x=51157..58307,y=-65634..-48678,z=1041..14388
|
||||
on x=-23733..-13154,y=7035..23913,z=55977..86463
|
||||
off x=-2265..11384,y=-74127..-50988,z=23795..46797
|
||||
off x=57413..81030,y=12293..42890,z=-1391..17128
|
||||
on x=-3314..30025,y=-4747..16210,z=73579..79036
|
||||
off x=64372..88137,y=20334..47674,z=-39429..-11848
|
||||
off x=-55517..-35792,y=53986..79435,z=-1229..11512
|
||||
off x=-25854..-3145,y=-93152..-57598,z=-38171..-12759
|
||||
on x=-66373..-36045,y=44295..53836,z=-57069..-19663
|
||||
off x=-7859..9392,y=-52171..-40966,z=52503..74557
|
||||
off x=30181..53650,y=-53694..-36031,z=50959..57413
|
||||
on x=-33079..-17690,y=-76370..-42737,z=-44672..-29471
|
||||
off x=-36966..-13630,y=14777..35859,z=68791..85389
|
||||
off x=-53262..-43798,y=-65671..-42996,z=-63783..-24372
|
||||
off x=20919..37985,y=35261..54834,z=42197..69015
|
||||
on x=-26551..1746,y=60060..95300,z=-32988..3456
|
||||
off x=-48095..-21271,y=57654..71939,z=14906..31230
|
||||
on x=-41913..-21340,y=-1159..29278,z=69716..79837
|
||||
on x=50071..80080,y=9074..33546,z=40644..63276
|
||||
on x=-82291..-52412,y=-24582..-4153,z=-54567..-20480
|
||||
on x=32689..53302,y=-810..20805,z=-78048..-53470
|
||||
on x=8856..34239,y=27401..50296,z=-79697..-58119
|
||||
off x=67070..78605,y=-45161..-12853,z=5834..35021
|
||||
off x=-9536..11179,y=-1612..37945,z=59287..87302
|
||||
on x=24077..49210,y=-76709..-65435,z=3915..22999
|
||||
on x=-52603..-19789,y=-33182..-13274,z=49905..68922
|
||||
off x=-808..22002,y=-97991..-68373,z=-24516..-3973
|
||||
off x=-69587..-38665,y=32629..47232,z=-49855..-32127
|
||||
on x=-79284..-60462,y=-599..23149,z=18494..39252
|
||||
on x=-9037..22084,y=53017..74877,z=40979..70983
|
||||
on x=22283..43849,y=26194..41117,z=53450..84758
|
||||
off x=-43053..-11534,y=68553..91547,z=-9517..-364
|
||||
off x=-36833..-8215,y=-32657..1320,z=57678..83049
|
||||
off x=-67241..-45492,y=38345..58562,z=14375..32715
|
||||
on x=-880..22583,y=41468..58074,z=56956..76461
|
||||
on x=7590..23528,y=69333..83342,z=-9387..3778
|
||||
off x=18563..54871,y=-83601..-58137,z=-38716..-28534
|
||||
on x=-51255..-23794,y=58843..71628,z=13202..44973
|
||||
on x=-41441..-20802,y=55176..80922,z=-24509..296
|
||||
off x=-36407..-21238,y=-73084..-38320,z=-66918..-33715
|
||||
off x=-8943..15145,y=42317..65331,z=54430..76066
|
||||
on x=-28775..-5479,y=57347..77134,z=24266..41418
|
||||
on x=38558..63760,y=-49420..-33594,z=-45037..-35384
|
||||
on x=-83022..-66950,y=-42022..-24002,z=-28701..4321
|
||||
on x=54109..73974,y=34676..64781,z=17084..34205
|
||||
on x=-12103..17124,y=-13877..16384,z=-94327..-64279
|
||||
off x=38838..57135,y=-44065..-41275,z=32156..45320
|
||||
on x=-29205..-26115,y=-2450..3560,z=-90024..-64453
|
||||
on x=-59932..-49523,y=-26975..-18004,z=50292..65639
|
||||
off x=68347..71179,y=1706..31301,z=-53699..-22525
|
||||
on x=-44914..-37051,y=58761..79795,z=14135..31043
|
||||
on x=14446..43108,y=-26390..1137,z=-73159..-53537
|
||||
on x=38744..46440,y=-65022..-56396,z=-28399..-15583
|
||||
on x=-39357..-17275,y=55880..91308,z=3812..23103
|
||||
on x=37814..45451,y=59689..71129,z=-33091..-3730
|
||||
on x=50669..65679,y=-65089..-39905,z=10428..16497
|
||||
on x=-76944..-63740,y=31943..44246,z=1634..17554
|
||||
off x=24313..39538,y=17660..43463,z=65263..82899
|
||||
on x=-1751..8013,y=52465..63766,z=36766..67427
|
||||
off x=-73979..-54352,y=23710..37649,z=-53777..-41099
|
||||
on x=12648..32312,y=27877..54262,z=48185..79056
|
||||
off x=64634..79787,y=-817..20726,z=-40924..-16995
|
||||
off x=-55159..-34951,y=-52366..-19139,z=33138..66420
|
||||
on x=28611..46884,y=-67432..-56104,z=-41247..-9884
|
||||
on x=53904..90747,y=-8530..12309,z=26543..54359
|
||||
on x=36535..52649,y=-1891..16050,z=-71909..-49345
|
||||
on x=15970..39925,y=19351..42141,z=60410..81771
|
||||
on x=48383..54849,y=56099..77155,z=-17073..8867
|
||||
on x=-15544..14965,y=-15041..13079,z=-85098..-71913
|
||||
off x=14507..43155,y=41806..61782,z=-57635..-51328
|
||||
on x=12433..30956,y=-81104..-66265,z=1364..25472
|
||||
on x=23177..46878,y=62348..73670,z=12336..27435
|
||||
on x=-37369..-24070,y=-67916..-49572,z=33851..56308
|
||||
off x=-33460..-17887,y=-31371..-14920,z=68200..76982
|
||||
on x=-490..21676,y=-39087..-15173,z=-77484..-55507
|
||||
off x=25035..49828,y=-85831..-62348,z=-21746..12536
|
||||
on x=-31755..-12488,y=20314..41767,z=-74849..-53330
|
||||
on x=69374..78844,y=22048..45164,z=-17514..8272
|
||||
on x=-27307..-13795,y=-26885..-899,z=57042..80628
|
||||
on x=-33191..-29034,y=54376..69632,z=24165..60153
|
||||
on x=76374..95339,y=-11315..11232,z=16557..35811
|
||||
off x=-27265..-1773,y=40190..64255,z=-65320..-49841
|
||||
off x=-85104..-61112,y=-55931..-19938,z=-19478..8038
|
||||
on x=65636..95716,y=-12418..14528,z=-4509..9864
|
||||
on x=-56477..-28588,y=1640..14491,z=-74593..-56323
|
||||
off x=-77836..-48354,y=-28324..-11739,z=23836..47437
|
||||
on x=25784..38121,y=-88253..-62923,z=-4452..10503
|
||||
on x=-36465..-3717,y=51759..72031,z=-52628..-26227
|
||||
off x=-43123..-11236,y=65474..75390,z=-42208..-25946
|
||||
on x=8701..39618,y=55548..83009,z=22704..46319
|
||||
off x=-3984..11535,y=-40721..-18864,z=62717..79731
|
||||
on x=30532..44358,y=-43801..-9112,z=59319..79217
|
||||
off x=-53478..-47391,y=23298..48860,z=-67938..-50736
|
||||
on x=-45265..-6689,y=68235..78527,z=-48886..-31219
|
||||
on x=-5902..20911,y=-75932..-56026,z=-57732..-31083
|
||||
on x=-66001..-30299,y=9289..21770,z=-79532..-44535
|
||||
on x=-72812..-58338,y=24623..39420,z=-32375..-11977
|
||||
off x=-71978..-43972,y=43285..58665,z=4474..26311
|
||||
off x=22508..41359,y=-69340..-49781,z=-64210..-33307
|
||||
on x=-44685..-20901,y=-48744..-43584,z=-75155..-44732
|
||||
on x=-72915..-50461,y=16928..42632,z=-50617..-29991
|
||||
off x=-27691..-9898,y=-77111..-45312,z=31228..55262
|
||||
on x=59914..95920,y=414..7209,z=9956..21045
|
||||
on x=-20446..-974,y=-44647..-27682,z=-88125..-68805
|
||||
off x=-7561..8433,y=-18982..894,z=64431..89344
|
||||
off x=-1653..10737,y=-25894..-7704,z=-78232..-72277
|
||||
off x=69346..79970,y=-21981..4499,z=-26907..11209
|
||||
off x=51820..54804,y=52972..70746,z=-2759..2312
|
||||
on x=64434..84026,y=-32402..-3979,z=-7195..20643
|
||||
on x=-83733..-69589,y=-51713..-33016,z=3012..13853
|
||||
on x=10201..21176,y=54036..75285,z=25829..62713
|
||||
on x=-48670..-38360,y=-69832..-50715,z=-37680..-12938
|
||||
on x=-72798..-56099,y=36370..47364,z=-30778..-10630
|
||||
off x=-17456..-7805,y=48648..60817,z=54191..60859
|
||||
off x=-56221..-44459,y=-39286..-17626,z=34456..52464
|
||||
off x=61325..85227,y=-44148..-35369,z=-28781..465
|
||||
off x=-45478..-28582,y=42699..63546,z=-63225..-45361
|
||||
off x=66065..80922,y=12350..28545,z=-21089..-6849
|
||||
on x=-74244..-55003,y=-8055..14243,z=35080..40384
|
||||
on x=-1095..27358,y=53117..71679,z=-57176..-34436
|
||||
on x=-35406..-12582,y=74457..80149,z=-20514..-13161
|
||||
off x=46076..64613,y=-51455..-35919,z=-35318..-21841
|
||||
on x=-52232..-37192,y=-10365..-2869,z=60987..74421
|
||||
on x=-74431..-69100,y=18008..38991,z=-8658..9185
|
||||
on x=-26753..-15624,y=-84413..-73738,z=-40002..-11374
|
||||
on x=33871..57730,y=56710..78730,z=29428..49337
|
||||
on x=-58732..-47192,y=-53228..-42042,z=-56032..-23917
|
||||
on x=-79806..-70568,y=-16031..12148,z=23275..45585
|
||||
on x=41188..62531,y=-7562..3883,z=47661..59951
|
||||
off x=-35220..-12578,y=19766..51340,z=-72170..-50301
|
||||
on x=-56285..-47392,y=-35306..-29170,z=44391..56897
|
||||
on x=-81768..-49994,y=6946..25870,z=47913..50332
|
||||
off x=-42537..-21597,y=-83563..-62724,z=23037..43665
|
||||
on x=-30305..-7671,y=35001..59014,z=46271..76274
|
||||
on x=-45229..-40602,y=-66300..-53150,z=12403..34806
|
||||
off x=-29746..-1711,y=20017..47514,z=-73069..-53366
|
||||
on x=-80075..-60940,y=-19955..9827,z=-8266..-1805
|
||||
on x=57569..80111,y=-65826..-50512,z=3231..25716
|
||||
on x=-75491..-69279,y=29398..33291,z=-17760..11671
|
||||
on x=-25479..2731,y=-87698..-67937,z=-11293..19048
|
||||
on x=-11829..-1061,y=19988..32790,z=-92294..-55506
|
||||
off x=21352..40945,y=40771..48583,z=-67151..-59151
|
||||
off x=-42977..-12057,y=-7257..18553,z=-83582..-65468
|
||||
on x=-7377..9954,y=26865..52444,z=-80529..-47250
|
||||
on x=-59449..-43724,y=-58486..-20224,z=-51557..-35095
|
||||
off x=-37705..-13203,y=-63763..-60864,z=-53820..-25757
|
||||
on x=44366..73289,y=-32806..-14773,z=-43125..-35418
|
||||
off x=-56656..-45872,y=50330..62757,z=-1701..25000
|
||||
on x=58577..74733,y=23861..40108,z=13747..40908
|
||||
off x=-67934..-40559,y=-11226..6016,z=-73412..-44862
|
||||
on x=12757..38824,y=47552..63986,z=42087..55609
|
||||
off x=-13501..5871,y=53876..79448,z=22501..38713
|
||||
on x=-36975..-13196,y=-88730..-58725,z=-16644..6962
|
||||
off x=-69288..-55110,y=38422..64923,z=15936..38032
|
||||
off x=-63471..-48291,y=16466..44719,z=-57299..-29640
|
||||
off x=66581..89710,y=-2354..15493,z=-12364..5232
|
||||
off x=78084..87689,y=-4976..12016,z=-10658..5121
|
||||
off x=28226..56713,y=-71919..-54608,z=-42397..-31750
|
||||
off x=18424..40815,y=63569..76696,z=14543..32698
|
||||
on x=-16245..14527,y=17680..43079,z=72549..91945
|
||||
off x=-77728..-74787,y=-43991..-10317,z=985..15105
|
||||
on x=-77999..-73050,y=-2022..12759,z=-39849..-12316
|
||||
off x=-44790..-21055,y=13687..35763,z=65209..88469
|
||||
on x=40013..59824,y=60136..79649,z=-358..25306
|
||||
on x=-35043..-16737,y=41777..56619,z=-61261..-49086
|
||||
on x=-77033..-56577,y=-5720..4847,z=-26507..-6754
|
||||
off x=-29531..-16639,y=40666..63576,z=44672..65642
|
||||
on x=-82927..-53260,y=-55047..-32334,z=-13487..9314
|
||||
off x=25712..49874,y=6851..19728,z=65528..74206
|
||||
on x=-37081..-15148,y=-60575..-23761,z=-70756..-49969
|
||||
on x=-28282..-9253,y=-40695..-22101,z=-81821..-67099
|
||||
off x=5780..20082,y=33642..46956,z=-71449..-62422
|
||||
on x=-88086..-77371,y=-15014..9175,z=-24709..3204
|
||||
off x=58375..90701,y=-916..23871,z=-9910..9070
|
||||
off x=47053..57166,y=-30456..-13194,z=-52918..-43593
|
||||
on x=3622..23986,y=-15322..-1710,z=-96227..-68508
|
||||
on x=2316..26398,y=-81095..-49688,z=33876..62700
|
||||
on x=30638..51715,y=35872..46823,z=-56349..-29601
|
||||
off x=-27831..-17345,y=-19515..-7960,z=76040..92701
|
||||
off x=14932..42647,y=30167..48535,z=55457..75628
|
||||
off x=32546..61262,y=17932..38658,z=-79115..-51598
|
||||
on x=65470..98033,y=-15674..13931,z=-1497..30342
|
||||
off x=47952..75295,y=13457..34032,z=-60835..-47553
|
||||
off x=-51589..-42217,y=29068..53726,z=35769..61614
|
||||
off x=41670..64539,y=53057..72532,z=15151..45904
|
||||
on x=-91360..-62313,y=-20027..6302,z=-40054..-4009
|
||||
on x=5912..42772,y=64980..77359,z=15793..29251
|
||||
off x=-57478..-52323,y=50326..60081,z=-31046..-3126
|
||||
on x=24971..40373,y=-70190..-48050,z=20111..31535
|
||||
on x=25587..49311,y=27261..46685,z=61743..70691
|
||||
on x=-18263..2723,y=41516..57698,z=-65645..-49422
|
||||
on x=78541..82441,y=8208..25964,z=1134..17009
|
||||
on x=33297..45607,y=60786..66623,z=25560..38578
|
||||
off x=63581..73645,y=11067..42553,z=-21908..-8296
|
||||
Reference in New Issue
Block a user