Compare commits

..

38 Commits

Author SHA1 Message Date
29be797deb Update day 22 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-22 16:51:42 +01:00
52de1b4d9e Add day 22 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-22 13:30:25 +01:00
2681d92bee Add day 21 description
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-21 17:32:51 +01:00
3f5437e383 Update day 21 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-21 16:35:27 +01:00
9576bc490f Update day 21 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-21 16:30:01 +01:00
0ea4309c3e Add day 21 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-21 16:28:40 +01:00
732c455dfe Fix reading of input file
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-20 12:38:13 +01:00
310668d4a6 Remove not used variable
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-20 12:37:43 +01:00
aae16d3c46 Add game of life algorithm for fun
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-20 12:37:19 +01:00
c80d4e7ac5 Optimize day 20 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-20 12:17:06 +01:00
a81d7b914c Remove empty line and add problem description
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-20 11:17:45 +01:00
c02a7c3934 Update day 20 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-20 11:02:55 +01:00
71ebdf8f65 Add day 20 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-20 10:28:07 +01:00
959b52cda2 Update day 19 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-19 21:55:32 +01:00
7def74b81e Update day 19 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-19 21:54:46 +01:00
0050e6dd76 Update day 19 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-19 21:52:44 +01:00
7e1e023caa Update day 19 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-19 18:04:00 +01:00
a7363a8fa3 Add day 19 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-19 17:40:51 +01:00
ca9e753968 Tidy up explode function
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-18 20:55:57 +01:00
014d2947fe Tidy up explode function
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-18 20:50:09 +01:00
66e344a5e5 Replace 'type() is' with 'isinstance()'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-18 20:39:37 +01:00
f137feefe4 Ad function description
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-18 15:36:15 +01:00
3de570f27f Revert last commit
All checks were successful
continuous-integration/drone/push Build is passing
eval() is slow and not safe to use against not previously checked input.
2021-12-18 15:16:03 +01:00
a6a56cb319 Update day 18 solution
All checks were successful
continuous-integration/drone/push Build is passing
Parse function is not needed since it can be replaced by eval().
2021-12-18 15:09:57 +01:00
26e385cdd2 Update day 18 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-18 14:13:27 +01:00
73681dd50b Add day 18 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-18 14:10:47 +01:00
a81a510d6b Update day 17 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-17 16:58:18 +01:00
9b8178438a Remove not needed line
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-17 16:04:13 +01:00
6a57417826 Remove not used code
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-17 12:04:05 +01:00
e022a7db44 Remove not used code
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-17 12:03:19 +01:00
90f2e88b1f Add day 17 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-17 12:01:41 +01:00
14855dcc2d Add task desctiption for each day
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 18:16:59 +01:00
54b83966ed Update readme
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 18:01:46 +01:00
cadeaf1bba Add description for day 16
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 17:59:47 +01:00
ce0b73fce1 Add description for day 15
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 17:43:52 +01:00
86665d264a Rename a variable
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 16:53:25 +01:00
d16807f80d Optimize day 15 solution by using priority queue
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 16:48:45 +01:00
c365cc5b64 Update day 16 solution 2021-12-16 16:41:06 +01:00
37 changed files with 5274 additions and 48 deletions

View File

@@ -5,6 +5,8 @@ Personal stats
--------Part 1-------- --------Part 2-------- --------Part 1-------- --------Part 2--------
Day Time Rank Score Time Rank Score 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 14 00:49:36 7350 0 00:50:14 2214 0
13 00:30:05 3269 0 00:37:02 2884 0 13 00:30:05 3269 0 00:37:02 2884 0
12 06:56:08 16817 0 16:08:35 26326 0 12 06:56:08 16817 0 16:08:35 26326 0

91
day-01/README.md Normal file
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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?_

129
day-13/README.md Normal file
View 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?_

70
day-14/README.md Normal file
View 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?_

170
day-15/README.md Normal file
View 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?_

View File

@@ -1,8 +1,30 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from pathlib import Path from pathlib import Path
from heapq import heappush, heappop
def part_1(input): 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 result = 0
x_size = len(input[0].strip()) x_size = len(input[0].strip())
y_size = len(input) y_size = len(input)
@@ -11,16 +33,13 @@ def part_1(input):
start = (0, 0) start = (0, 0)
end = (x_size - 1, y_size - 1) end = (x_size - 1, y_size - 1)
cost = {start: 0} cost = {start: 0}
queue = set([start]) queue = [(0, start)]
done = set() done = set()
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)]) neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
while len(queue): while len(queue):
cur_cost = None cur_cost, current = heappop(queue)
for n in queue: if current in done:
if cur_cost == None or cost[n] < cur_cost: continue
cur_cost = cost[n]
current = n
queue.remove(current)
(xc, yc) = current (xc, yc) = current
next = set() next = set()
for (xn, yn) in neighbors: for (xn, yn) in neighbors:
@@ -31,13 +50,18 @@ def part_1(input):
next_cost = cur_cost + nodes[n] next_cost = cur_cost + nodes[n]
if not n in cost or next_cost < cost[n]: if not n in cost or next_cost < cost[n]:
cost[n] = next_cost cost[n] = next_cost
queue.add(n) heappush(queue, (next_cost, n))
done.add(current) done.add(current)
result = cost[end] result = cost[end]
print("Part 1 result:", result) print("Part 1 result:", result)
def part_2(input): 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 result = 0
x_size = len(input[0].strip()) x_size = len(input[0].strip())
y_size = len(input) y_size = len(input)
@@ -47,16 +71,13 @@ def part_2(input):
start = (0, 0) start = (0, 0)
end = ((x_size * repeat) - 1, (y_size * repeat) - 1) end = ((x_size * repeat) - 1, (y_size * repeat) - 1)
cost = {start: 0} cost = {start: 0}
queue = set([start]) queue = [(0, start)]
done = set() done = set()
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)]) neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
while len(queue): while len(queue):
cur_cost = None cur_cost, current = heappop(queue)
for n in queue: if current in done:
if cur_cost == None or cost[n] < cur_cost: continue
cur_cost = cost[n]
current = n
queue.remove(current)
(xc, yc) = current (xc, yc) = current
next = set() next = set()
for (xn, yn) in neighbors: for (xn, yn) in neighbors:
@@ -66,13 +87,13 @@ def part_2(input):
if not (x, y) in done: if not (x, y) in done:
next.add((x, y)) next.add((x, y))
for (x, y) in next: for (x, y) in next:
next_cost = nodes[(x % x_size, y % y_size)] + \ next_node_cost = nodes[(x % x_size, y % y_size)] + \
(x // x_size) + (y // y_size) (x // x_size) + (y // y_size)
if next_cost > 9: if next_node_cost > 9:
next_cost -= 9 next_node_cost -= 9
if not (x, y) in cost or (cur_cost + next_cost) < cost[(x, y)]: if not (x, y) in cost or (cur_cost + next_node_cost) < cost[(x, y)]:
cost[(x, y)] = cur_cost + next_cost cost[(x, y)] = cur_cost + next_node_cost
queue.add((x, y)) heappush(queue, (cur_cost + next_node_cost, (x, y)))
done.add(current) done.add(current)
result = cost[end] result = cost[end]
print("Part 2 result:", result) print("Part 2 result:", result)

132
day-16/README.md Normal file
View 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?_

View File

@@ -3,8 +3,14 @@ from pathlib import Path
from math import prod from math import prod
def get_next_packet(msg, start, packets, res=0): def get_next_packet(msg, idx, packets, res=0):
idx = start """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): if (idx+11) >= len(msg):
idx = -1 idx = -1
return idx, res return idx, res
@@ -41,11 +47,12 @@ def get_next_packet(msg, start, packets, res=0):
next_idx = idx + length next_idx = idx + length
while idx < next_idx: while idx < next_idx:
idx, res = get_next_packet(msg, idx, payload, res) idx, res = get_next_packet(msg, idx, payload, res)
packets.append({'version': version, 'type_id': type_id, 'payload': payload}) packets.append({'type_id': type_id, 'payload': payload})
return idx, res return idx, res
def calc_result(packets): def calc_result(packets):
"""The function recursively steps through the packets and performs the defined calculations."""
res = 0 res = 0
for p in packets: for p in packets:
match p['type_id']: match p['type_id']:
@@ -73,38 +80,30 @@ def calc_result(packets):
return res return res
def part_1(input): def solve(input):
result = 0 """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') line = input[0].rstrip().strip('0')
msg = bin(int(line, 16)) msg = bin(int(line, 16))[2:]
msg = msg[2:] while len(msg) < len(line) * 4:
while len(msg) % 4:
msg = '0' + msg msg = '0' + msg
packets = [] packets = []
next = 0 next = 0
while -1 != next: while -1 != next:
next, result = get_next_packet(msg, next, packets, result) next, result_p1 = get_next_packet(msg, next, packets, result_p1)
print("Part 1 result:", result) result_p2 = calc_result(packets)
print("Part 1 result:", result_p1)
print("Part 2 result:", result_p2)
def part_2(input):
result = 0
line = input[0].rstrip().strip('0')
msg = bin(int(line, 16))
msg = msg[2:]
while len(msg) % 4:
msg = '0' + msg
packets = []
next = 0
while -1 != next:
next, _ = get_next_packet(msg, next, packets)
result = calc_result(packets)
print("Part 2 result:", result)
input = list() input = list()
p = Path(__file__).with_name('input.txt') p = Path(__file__).with_name('input.txt')
with open(p) as f: with open(p) as f:
input = f.readlines() input = f.readlines()
part_1(input) solve(input)
part_2(input)

142
day-17/README.md Normal file
View 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
View 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
View File

@@ -0,0 +1 @@
target area: x=25..67, y=-260..-200

202
day-18/README.md Normal file
View 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
View 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
View 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
View 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
View 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

File diff suppressed because it is too large Load Diff

125
day-20/README.md Normal file
View 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
View 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
View File

@@ -0,0 +1,102 @@
##....###.#.##...##....####..###.#.######.#.#.##.#.####.#.#####.##.##..##.###.###..##.##..#####.##..#..##..#...#.####..#.###..#....####.#..##.##...#######.###...#.######..#..#...###..###.#####.##..#.#.###.#.###.#..#.###.###.#..##.....####..#.##.##.#..#...###.#.....##..#....#.##..#....#....####.#...#.#.##.#...#.##..#..#.#..###.###.#.##...##.#.##.##..#..##.#..#...######.#.#..###....##...##....#....##....#.#..##..#####.####.#.##...#...#.#.#....#.####...#.##..#...#..#....#..#..#..##.#.#.#.#######.###..##.#.....
#...##.##.#..#..#..#.....##.#.#..#.###.####...####.#.###.....#.########..###...###....#......###...#
####.##.#.###.#.##....##..##.....####..#.##.....##.###.#..#...####..#..###..###.#...##..#.#..#.#####
###.#..#..#..##..#..#.###..#..###.###.#.###......###.##.#.##.#........#..#...#..#...#.##..#.##.....#
.#.#..#..#.####..##.#...##...#.##.#...##.....#.####.#.##.#.#..####.#.#..#..###.#..###......##.##....
###.##......####..#.#..#.##.###.##.##..#####.#.#...#.###.#.#.#....####..#.####..#..##...###.##.#..##
.#####.#..##..###.#..#..#..##..#.##.##..##..#####..#.#.#.##....##.#.###.#####.##..##.#..####...###.#
..#..##..########...#...##....#.#..#.#.###.#...#.#..#####.###...##.#..#.##.##..#.#.#....#...##.....#
.......##..............##...#.####....#####.###.###.#.#..#.#.####.###.#####.##..#.....####.#...##...
#..#...##.#.##...##.####..##.....###.#.##.#####.##...#.#.###.#.##..#..#.####..###..##.##...#...#.#.#
.#....##.#..#########.#.##.##.####....####.#..#..#...#....###.#..#.##.##...#.########.#...##.#.#####
.....##.###.##.##..##...#....#.##.#.###...###..#..###.#.##....###.###..#...####........#..##.##...##
##.##.##..#....#.#..#..##..#..#.####..###....######.####..#...#.#..####.....#.#.##.#.#..##..#.#.#.#.
#######....###.###...###..###..#..#.#..#...##.##.#.##.#.#..#.#.##.#.#####.##..######.####..##..##.##
#..#.#..#..#.#......###....##....##.###.##.#..##..###.#..###.##..####..#....####.#...####..#.....##.
.#.####......###.....#.#.##...##.###.##....#.##...#.#.....###.#..#.###...#...#..#..##.##..#..#.#.#..
#.#...#.####..##...#.####.##....##.##.......#######...#.#..##.##.....#...#..###....#...#..#.......##
####.##..#..#...######.##..##.#.######..#####..###.###.#.....###.#.###...###.##...##.#......#.#.####
####..#..#......##.......######.#####....#..#..##.##.##.#.##.....#..##.###.....###.##.#..##.##.##.##
..##.#.#####...#.##.#.#.##...#..##.######.#.#.#####...#..##.##..##..#.##.....##.##.#.#.#.##...#.#..#
.#.#.....#..#..#.###.##.##...###...########.####.####..##.###....##..###.#..###..###.....#.##..#.#.#
##.#.##.....#....#.#.....###..#..##.#.##..##.###...#..#...###.....####.#..##.##..#...#.##.##......#.
.###.#.#..####.#.#..#.##..#.#.#...######.#.#########.####...#.#.#..##...#.#...#...#...#.##..........
.###.###...##..#..##.##.#...#.#..###.##.#...####.######..##.#.....##.#####..#....#.###.###.#####.##.
#..###.######....##....####..#...#..##...#.###..#..###...###.#...#...##...##.#.##.##....#..#........
.#...#..####.####......#..##....#.#....####..##.#..#.##......#.##.....####..####..#.#...#.##...####.
#.####.#..####..#..#.######.##.#####....#.####.#...#.#.#..#.#.###..#..#....#.#.#.#..#.###.....#....#
....##.###..###.###.#.##.##.##.#.###..###.#..#..#####....##.#...##.##.#.....#.##.##.#.#####..#####..
..#.##.#.###.#....#.#..#.######..#......##.....##.##....#...#.#.###.#...#..#.###..#.#.##.#....#..###
####.....#.#.#.#.#..#...#.###.##.#..#..#.##.#.##.#...#..###..#.....#....#......#.#...#.##.##.#..#.#.
..##...##...###.##.####.#....#...#.###..#.##.#..##....#....#.#.#...........###.##....#..###.#.######
.#.#...####...##..##.#.#..#...#.#.########.####..#..###.#..##.##..###.#.##.#....#..###....#..##....#
#.###.##.###.###.####...##.#..##..#.#..#.#.##.#.#.##...#..#.#..#.##.#..#...##.###.....#####.#.....##
....##........##.##....##......#...#.#...##.#.##.##.##..###....###..#...####.####.####.#..##.##.#..#
.###.#.#..#..##..#.#..#.###..#####...###......#..##.#######...##.###...#...#..#...##...##.###.#..#.#
#..##.##..#.####...#.###.###.###....####.#####.##...####.###.#.#####.#####.#..#.#####..#.###.#..#.#.
..#..#.....##..##.#.##.#.....#..#####.###....#.##.#.##....#.....##....##.#.###.#......#.#.#.#####...
######..#.#.##.#####.#.##...#####..#.##.###..##..#..#.###.#.#..#.#.....##..#...##.#...#...#..##..#..
.##.#########..###........###.####.#..#...#..#.####......##..#####.#.#.##.##.#...##....##..#.###..#.
..#..#..#.##....##..#..#.....###.#....####..####.#.##.#..####.#.#...#.#.###..#..#####...#...#.#.#..#
...###.##...##......#.##.#.#..#..#...###..#########..##.....#####..###.###.....##.....####.##.###..#
####..##...####.#.###..##.#.##.#.#...#.....#..###.#####....#.##.####..#.##.....##..#.#.##..##.#...##
..####....##....#...#.#..###...#.#....##...##..###.#.####.#....##.###.######..######...##....#.##.##
##..#.#.#..###.####.#..#.##.###...#.#...###.##.#.#.#.#.#.#.#.#.....##.#..#.#.##.....#..##.......##.#
...#####.#..##.##.#...##..#.##...#.##...##....#.#.#..#.#.#.#.#.#.#..##..#.#..#.#.#.#.#.###.#...####.
.#..#.#####.#.##..##..#..##..#.####.###......##.####.#.###.######...###..#..####.#...#.#..##....#...
##..#.##.##....#..###.#...##..###.#.#..##..#.#..#.#...##.###.#####.#..#...#.#..##.#..##.####.#..#.##
.#.##..####.#...#.##.#.#...#..##..#.#......#..###..#...#....###...##...##..##....#..###.##...#####.#
#..#.#.#..##..#..###..#.#.###.......#####.##.##.##..#..###.#.#.#.######..###..#..###.###.#..####...#
..###...##.##.##..##.#.##.##.#.######....####...##.#.###.#####...####.#...#...#.#..###....#.##...#.#
#.###.##.#.##.#.##.#..##...##.#.####.###.###....#.##..#.##..#.#..#.#..##....#..#####...#####.##....#
#...##.#####..#..###########.###.....###....###.#.#..#########.#.###.##..#.##.#.#....#.##.##.#.#.##.
#...#...##..###....##.#...##..#..#..##.##...#..#...##....###.......#####..#...#.#..#....#.#.##.##.##
.##...#..#.####.##...#..##..####..##..#..######.......#.#...#.#..#...#.#.#..###..#.##.##...##....###
#######.#.#..#.#.###.##.##...#...#....#...##.#..#.##....#..#.#####..#.####...####.##.##..#####.##.##
###..###....##...####.#.#.###......#.###...#.#..#...#####...#.######.#...#.##..##.#...##.#..##.###.#
.#.....#.#..#.#...#.##.#.###....###..#.#.#.##...##....#....#...#.#..##.....#..#.....####.#..#..#....
.###.##....#.#.#.#...#.####...#.##..##.#.##.##.#.####.#.#..####.####.##...###....###..##..##.#.#####
...#.##.###.###.#.##.#.##.##..##.##..#..##..##.########.#..##...###.#.###...#...#.....#...#.....###.
##.#.##...###...######.#.....#.#.....#.#.#.####..#..##..##.#####....###.#.##....####...#....##..#..#
.#.##..##.##.##########..#..#.#..#####.#####..####.##.#...#.#.##..#..#.##..#...###....#..#.....#..#.
###..##...##.#.###....####....#..##.###.....#####..#.#.#..####.####....######.##.#.###.#.####.##..#.
#.##.#....#.#...##.#..####.#.##.###.##..##..###.#....##....#.#.#.#.#####.#######...##.##.#.....#...#
..###.####.##.#.#..#..###.#.#...###..#...#.#.#...#.#.#.#....#.##.#.####...#..#...###...####..#.#..#.
....##.#.####..###.##.###....#########..#.#..##..#....#..###...#.....#.###.##..#.#....###.#.#..#..#.
..#....##########..##.#####.###.###.#....#.#..#.##.#...#..###.##..#..###..#######..##...##..#...#...
#..#...............#.#.#.#.....###..##.....####..#...#...####.#......###.##.######....#...#......#..
.....####.##...#....##..#....######.##.#...##.#.##...####...#.#..####..###.#.#.#######....#.###.##..
...##.#.#####.#####.#..#.#...####......##.##.#..#.#.#..####.#######.##..##.##.....##.#.#.##..####..#
##...##....#.#....#..#.###.#....#.###.##.#.##.##.###.##.....###.#..#..##...#.##.....###...######...#
#....###.#.#########.#..#.#.##...######..##.#.#..##.###...#..#..###..#####.#..####.....####.#..#.#.#
.##.###.##...#.###..###.#...#..###.##.##..#.....#....#..#.#...#.##..#..#.#..#.###.######.##.#.#...##
.#..#.....##.#..##.#.....#..###...#####...#...#..#.#...#..#....#.#..#....###.###.#####..#.#..##.....
#..#..#.#...##.##...#..#..####.###..###.#....#.#..#.#.####.#..#.###.###........###....#..###...#...#
##.#.##....#.####..#.....##.........#.#...####...#...##.#########.#.#.##....#....#.##...##.....#####
.##.#..#.#.#..####....###.#.##...#.....#....##.#.#..#####.###.....#.####...##.#..#####..#.#...#...##
..######..#.###.###.#..#.#####.#.##.#.##..####.#....##.##...#.#..####...##...###..###...#.#####.##.#
.#.#.###...#..####.#.#..#.#..#..#..###...#.#...####..##.##...#.#.##..###..###.##.#..###.#.#.#..###.#
#..##..##..##.........#.#......#....#.#####....#..#.##.####.#.#.#...#.....#.#..#.###.#....###.....##
..#####.#.##...#.#.#..#.....####......##..####.#..##....#...#.####.....##..###.##...###.#.###.###.##
.#.##.##.#.######.#.....#.###..###....#.....###...#..##....#..##.#...######.....#.##..#####.#.#####.
..#.###.#.##...######..#..#..###.#.#....##.#..#....#....###.#.#..##.#..#.##..##....#..##.#######..##
.#.....##.#...#.####..#..###.####.###..##.#..#..#..#.###.###...#.#.##..#..###..####..###.#..#.##.#..
##..######...####.##..#.#.######.#..#.####.#..#...#.#.##..#...###.#.....####..####.#...#.....#...#.#
#.#.#....#..#.#...##.##..##.####.######...###.#.#..#..#.#.###..##.#..##.###..##..###.###.#...##..###
..#####.#..##.##.#.####.###.#....##...#.###.##..#.#.#.###.#.#....#.#.#..###.##.#..#....###...#...##.
....####..##.##.#....##..###.##.###..######....#.##.##..#..#####...##.....#..##..#.##..#.......#...#
#...##.###..#..#.....#..#.##..#.######.....#.#######....#...######.####...#.##..#..###.#..#.......##
###..##.########....###.#.##.###.#..#.####.#.###.###...##...##..####.#...##...#.###..#####.###....#.
#.##..#..####..##..###.#.##.....##..##...#.###.#..#.##.##..#..##.###.#..###..###.#.##..#....#..###..
..#######.####.....#.#######.#.#..#.#...#.#..###....#...#..###..##...#....#...#.#.##.##..#..#....#.#
.....#.#..#..#...#.....##..#......#.##.#.#..#####..#.###.##.##...#.#...#...##.#.#####.#####..###..##
.#.##..#.....#..#..####.#.##...##.#...###.#.#..#####.#.#.####..###..#.#.##..#..####.##.##..####..#..
#.#####....##.......#...#.##..#..#..##......#...##..####..###...#..#..#.#.###...##.###.####..###..#.
#.##...#.###.#.##..####..##..###.###..........####..#.##.....#.#..##########.#.##.#.#..#...##.#.##..
#.#.#..##.#..###..###..##......###.#.##.#.#.###.##.###....#....#..###.#.#..####.#..#####.#######..#.
##.##..####....#..##..##.#.#...##..#####.#.#...##..##..###..#.#.##..#.####...#.###..#.#.##......##.#
##......#.#..##...#..#.#.#.....#...#....####.#.###.......###.#.###...#.#.#.#####.#....##..#.#.......
###.##..##..###..##..######..#..#.#...#####..##..#...##.#...#..#.##..#..###.#....##.#.##..##.#.#.#.#
####.##.#....#.###.#..#.#####..#.#..#..##...######.###...##..#.#....##.#..####..#...#.#.#.##..##....
.####.#.##...#.##..#.##.#...##..######.###.#..#.###.#######.#.......#.#.###.#######.#.#.#.....##..#.

59
day-21/README.md Normal file
View 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
View 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
View File

@@ -0,0 +1,2 @@
Player 1 starting position: 4
Player 2 starting position: 10

192
day-22/README.md Normal file
View 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
View 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
View 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