Compare commits

...

122 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
cbd7e79917 Update day 16 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 08:56:08 +01:00
7da0981cc5 Add day 16 result
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-16 08:54:57 +01:00
1255dab24f Update day 15 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-15 19:36:48 +01:00
a3becde292 Update day 15 solution
All checks were successful
continuous-integration/drone/push Build is passing
Don't neet to remember the path
2021-12-15 18:17:35 +01:00
d353c3555e Add day 15 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-15 18:12:05 +01:00
a860842fcf Fix day 14 dolution
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
2021-12-14 11:11:55 +01:00
6136c92fd6 Update day 14 solution
Some checks failed
continuous-integration/drone/push Build is failing
Use collections.counter instead of collections.defaultdict
2021-12-14 10:56:18 +01:00
9aaf38ea2c Update day 14 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-14 10:33:43 +01:00
48a72554bb Remove empty line
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-14 09:55:42 +01:00
77b158aacf Fix day 14 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-14 09:22:27 +01:00
56ee86026d Update day 14 solution
All checks were successful
continuous-integration/drone/push Build is passing
Tidy up
2021-12-14 09:20:25 +01:00
ae2666fb02 Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
collectios.Counter not needed
2021-12-14 06:53:46 +01:00
739cb2a318 Add day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-14 06:52:55 +01:00
03633c432f Fix day 13 part 2 output
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 12:45:58 +01:00
87e8ab9c6a Tidy up day 13 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 12:44:30 +01:00
0b08b34c9a Remove empty line
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
2021-12-13 12:15:50 +01:00
824f4eff67 Update day 13 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 12:11:54 +01:00
5b693a6ac1 Update day 13 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 11:54:40 +01:00
d2d338ab16 Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
Try using set for available edges
2021-12-13 09:35:50 +01:00
801e4747a2 Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 09:31:58 +01:00
f1a4989196 Update day 12 solution
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-13 09:31:20 +01:00
e0a1022a55 Update personal stats
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 09:20:45 +01:00
932ec446b2 Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
Only count the number of sub-paths for part 2
2021-12-13 09:16:38 +01:00
fc9e44a73e Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
Save some ms
2021-12-13 09:00:25 +01:00
a70226032b Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
Use deque instead of list
2021-12-13 08:54:11 +01:00
5995599d6d Fix day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 08:51:11 +01:00
0842b5d196 Fix day 12 solution
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-13 08:50:33 +01:00
ae8d3f0d97 Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
Speed up a little and rename argument twice to visited_twice
2021-12-13 08:26:11 +01:00
cceb733667 Update day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 08:12:53 +01:00
577725fd54 Update day 13 solution for readability
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 07:46:30 +01:00
7928680a02 Fix day 13 output
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 06:40:40 +01:00
d45e0ef80e Improve readability of day 13 part 2 result
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 06:39:47 +01:00
2642aa39f1 Add day 8 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-13 06:38:33 +01:00
0473bf6da2 Add day 12 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-12 22:10:55 +01:00
a5229f5209 Increase resolution for runtime
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-11 14:53:09 +01:00
a2fb393628 Add day 11 solution 2021-12-11 10:47:08 +01:00
228ee1df5a Update run_all.py
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-10 17:50:02 +01:00
06244116b8 Add executable flag to run_all.py
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-10 17:42:54 +01:00
e60bacf710 Update run_all.py
All checks were successful
continuous-integration/drone/push Build is passing
Fix printing total run time.
2021-12-10 08:54:58 +01:00
4b2f5c62e8 Update run_all.py
All checks were successful
continuous-integration/drone/push Build is passing
Print total run time at the end.
2021-12-10 08:53:49 +01:00
8c0e9e4bda Update day 10 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-10 08:50:14 +01:00
124eed1c1f Add day 10 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-10 06:30:19 +01:00
dc6a064b02 Add day 9 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-09 19:16:10 +01:00
5031b4cae7 Add seperator in output between days
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 09:34:34 +01:00
c22e5b16c4 Show execution time
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 09:31:37 +01:00
321f829414 Tidy up day 8 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 09:26:40 +01:00
06e7f08586 Update day 8 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 09:05:44 +01:00
23b82701b9 Add break if digit was found
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 08:35:14 +01:00
fda97ac9ea Update day 8 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 08:11:13 +01:00
14373dfc17 Add day 8 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-08 06:54:12 +01:00
db8f0f726a Add day 7 stats
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-07 13:59:37 +01:00
0941d52528 Update day 7 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-07 09:52:40 +01:00
2ad1db4790 Update 'README.md'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-07 09:47:03 +01:00
113049625c Add day 7 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-07 09:40:09 +01:00
0620c6a256 Add 'day-07/input.txt'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-07 09:38:01 +01:00
c408a267a6 Update day 6 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:58:40 +01:00
445c24c043 Update shebang
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:44:27 +01:00
24e3d51b30 Update shebang
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:43:23 +01:00
a23013420d Update 'day-02/day-02.py'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:43:00 +01:00
f20877c65a Update shebang
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:42:38 +01:00
70e705c5dc Update shebang
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:42:23 +01:00
43272de3e6 Update shebang
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:42:10 +01:00
95e54616a1 Update shebang
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:41:54 +01:00
64ac742faa Update 'README.md'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:40:58 +01:00
ad447842fd Update 'README.md'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:39:42 +01:00
5f6671ec10 Update 'README.md'
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:39:11 +01:00
f04783f911 Add personal stats to readme
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:37:02 +01:00
31ad298e22 Update day 6 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 07:32:21 +01:00
f906aa5487 Update day 6 solution
Some checks failed
continuous-integration/drone/push Build is failing
2021-12-06 07:29:57 +01:00
85e17564b4 Add day 6 solution
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-06 06:29:19 +01:00
fe738ad150 Use Popen to fix output order in drone
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-05 14:46:46 +01:00
a4741f928d Call solutions in sorted order
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-05 14:41:55 +01:00
29458b07a5 Rename drone.yml to .drone.yml
All checks were successful
continuous-integration/drone/push Build is passing
2021-12-05 14:31:25 +01:00
a5fd026ed6 Add drone pipeline to run all days 2021-12-05 14:30:22 +01:00
4f7907b17c Use pathlib for input.txt path 2021-12-05 14:29:50 +01:00
0dee47b9cc Update day 5 solution 2021-12-05 14:03:47 +01:00
8557108268 Update day 5 solution 2021-12-05 13:13:53 +01:00
0a9b847ea1 Update day 5 solution 2021-12-05 13:09:36 +01:00
1a5aadb5d7 Add day 5 solution 2021-12-05 12:33:57 +01:00
8e535ef161 Add day 4 solution 2021-12-04 13:09:22 +01:00
93b7bbedca Add day 3 solution 2021-12-03 17:20:32 +01:00
e5f751fd0f Update day 2 solution
Don't split into list
2021-12-02 18:25:26 +01:00
3700b24968 Add day 2 solution 2021-12-02 18:17:58 +01:00
874453fc1d Fix template 2021-12-02 18:17:34 +01:00
69 changed files with 11019 additions and 10 deletions

9
.drone.yml Normal file
View File

@@ -0,0 +1,9 @@
kind: pipeline
type: docker
name: default
steps:
- name: run
image: python
commands:
- python3 run_all.py

View File

@@ -1,2 +1,24 @@
# advent-of-code-2021
```
Personal stats
--------Part 1-------- --------Part 2--------
Day Time Rank Score Time Rank Score
16 01:03:24 1899 0 02:53:04 4403 0
15 11:35:53 19304 0 12:02:27 15520 0
14 00:49:36 7350 0 00:50:14 2214 0
13 00:30:05 3269 0 00:37:02 2884 0
12 06:56:08 16817 0 16:08:35 26326 0
11 04:38:59 13748 0 04:42:36 13469 0
10 00:13:01 2516 0 00:28:59 3447 0
9 00:21:43 5101 0 13:07:23 29623 0
8 00:17:48 5001 0 00:51:53 1540 0
7 02:50:14 21325 0 03:04:41 20312 0
6 00:11:27 3758 0 00:27:45 3202 0
5 05:19:48 20484 0 05:35:34 18181 0
4 06:17:32 21985 0 06:51:06 20759 0
3 00:29:46 9841 0 04:45:58 23042 0
2 12:13:24 76237 0 12:16:13 72021 0
1 00:05:12 2994 0 00:23:10 5424 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?_

View File

@@ -1,4 +1,6 @@
#!/usr/bin/python3
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
last_number = int(input[0])
@@ -34,8 +36,9 @@ def alt_solution(input, num_summands):
input = list()
with open('input.txt') as fp:
input = fp.readlines()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)

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?_

42
day-02/day-02.py Normal file
View File

@@ -0,0 +1,42 @@
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
h_pos = 0
depth = 0
for line in input:
cmd, rng = line.split()
if 'forward' == cmd:
h_pos += int(rng)
elif 'up' == cmd:
depth -= int(rng)
elif 'down' == cmd:
depth += int(rng)
result = h_pos * depth
print("Part 1 result:", result)
def part_2(input):
h_pos = 0
depth = 0
aim = 0
for line in input:
cmd, rng = line.split()
if 'forward' == cmd:
h_pos += int(rng)
depth += aim * int(rng)
elif 'up' == cmd:
aim -= int(rng)
elif 'down' == cmd:
aim += int(rng)
result = h_pos * depth
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)

1000
day-02/input.txt Normal file

File diff suppressed because it is too large Load Diff

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.)

68
day-03/day-03.py Normal file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
result = 0
gamma = 0
epsilon = 0
num_bits = len(input[0]) - 1
counts = [0] * num_bits
for line in input:
line = int(line, 2)
for i in range(num_bits):
shift = (num_bits - i - 1)
mask = 1 << shift
if mask & line:
counts[i] += 1
for count in counts:
gamma <<= 1
epsilon <<= 1
if len(input)/2 < count:
gamma |= 1
else:
epsilon |= 1
result = gamma * epsilon
print("Part 1 result:", result)
def part_2(input):
result = 0
num_bits = len(input[0]) - 1
oxy_list = input.copy()
co2_list = input.copy()
for i in range(num_bits):
shift = (num_bits - i - 1)
mask = 1 << shift
count = 0
for element in list(oxy_list):
if int(element, 2) & mask:
count += 1
digit = 0
if (len(oxy_list)/2) <= count:
digit = 1
for element in list(oxy_list):
if (int(element, 2) & mask) >> shift != digit:
if 1 < len(oxy_list):
oxy_list.remove(element)
count = 0
for element in list(co2_list):
if int(element, 2) & mask:
count += 1
digit = 1
if (len(co2_list)/2) <= count:
digit = 0
for element in list(co2_list):
if (int(element, 2) & mask) >> shift != digit:
if 1 < len(co2_list):
co2_list.remove(element)
result = int(oxy_list[0], 2) * int(co2_list[0], 2)
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)

1000
day-03/input.txt Normal file

File diff suppressed because it is too large Load Diff

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?_

138
day-04/day-04.py Normal file
View File

@@ -0,0 +1,138 @@
#!/usr/bin/env python3
from pathlib import Path
def read_boards(input):
boards = list()
board = list()
row = 0
for line in input:
if not line.rstrip():
boards.append(board)
board = list()
row = 0
continue
col = 0
for number in line.split():
field = dict()
field['number'] = int(number)
field['marked'] = False
field['row'] = row
field['col'] = col
col += 1
board.append(field)
row += 1
return boards
def mark_number(boards, number):
for board in boards:
for field in board:
if field['number'] == number:
field['marked'] = True
return boards
def check_board(board, num_rows, num_cols):
for row in range(num_rows):
win = True
for field in board:
if not field['marked']:
if field['row'] == row:
win = False
if win:
return True
for col in range(num_cols):
win = True
for field in board:
if not field['marked']:
if field['col'] == col:
win = False
if win:
return True
return False
def get_board_sum(board):
sum = 0
for field in board:
if not field['marked']:
sum += field['number']
return sum
def check_boards(boards, num_rows, num_cols):
for board in boards:
for row in range(num_rows):
win = True
sum = 0
for field in board:
if not field['marked']:
if field['row'] == row:
win = False
sum += field['number']
if win:
return sum
for col in range(num_cols):
win = True
sum = 0
for field in board:
if not field['marked']:
if field['col'] == col:
win = False
sum += field['number']
if win:
return sum
return 0
def part_1(input):
result = 0
drawn = input[0].split(',')
input.remove(input[0])
input.remove(input[0])
boards = read_boards(input)
for number in drawn:
number = int(number)
end = False
boards = mark_number(boards, number)
for board in boards:
if check_board(board, 5, 5):
end = True
result = get_board_sum(board) * number
break
if end:
break
print("Part 1 result:", result)
def part_2(input):
result = 0
drawn = input[0].split(',')
input.remove(input[0])
input.remove(input[0])
boards = read_boards(input)
last_board = list()
for number in drawn:
number = int(number)
boards = mark_number(boards, number)
num_complete = 0
for board in boards:
if check_board(board, 5, 5):
num_complete += 1
if len(boards) - num_complete == 1:
for board in boards:
if not check_board(board, 5, 5):
last_board = board
elif last_board and len(boards) == num_complete:
result = get_board_sum(last_board) * number
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.copy())
part_2(input.copy())

601
day-04/input.txt Normal file
View File

@@ -0,0 +1,601 @@
62,55,98,93,48,28,82,78,19,96,31,42,76,25,34,4,18,80,66,6,14,17,57,54,90,27,40,47,9,36,97,56,87,61,91,1,64,71,99,38,70,5,94,85,49,59,69,26,21,60,0,79,2,95,11,84,20,24,8,51,46,44,88,22,16,53,7,32,89,67,15,86,41,92,10,77,68,63,43,75,33,30,81,37,83,3,39,65,12,45,23,73,72,29,52,58,35,50,13,74
10 83 98 12 33
38 68 2 99 85
16 89 54 50 97
31 8 17 11 76
0 55 66 32 87
77 60 61 59 16
17 46 97 9 73
42 82 25 32 29
48 94 52 55 50
95 14 67 79 34
6 31 5 74 67
76 89 15 78 47
54 49 62 32 38
35 43 61 22 58
75 97 33 8 16
37 58 22 47 30
4 86 77 42 54
73 94 87 34 55
72 53 14 64 88
23 21 36 52 66
59 16 18 85 93
96 56 50 53 94
68 95 77 0 27
47 30 88 46 65
75 82 41 31 23
88 96 94 75 3
31 26 74 90 36
59 22 41 84 54
6 68 37 20 93
10 66 46 9 79
33 78 12 62 73
18 37 7 44 66
69 15 54 53 82
98 20 30 58 81
56 95 36 91 99
47 89 86 58 43
49 72 84 94 51
69 73 15 50 0
46 81 76 31 61
96 91 56 2 34
22 91 70 68 99
25 28 3 42 1
21 19 79 54 4
97 88 49 8 78
44 98 84 83 6
21 0 62 2 9
49 81 50 66 10
24 72 45 96 6
51 82 17 58 91
18 56 39 11 25
22 25 42 98 93
1 26 0 67 65
31 11 37 7 96
28 17 40 79 12
95 5 2 66 10
63 8 97 64 82
43 12 83 1 11
69 84 74 7 59
25 48 38 89 62
22 93 39 71 76
92 94 20 49 21
34 41 61 98 28
93 62 74 12 31
63 77 87 36 55
23 7 4 69 53
61 27 76 56 12
60 7 36 33 97
4 16 89 44 40
51 43 75 79 28
14 69 35 90 8
12 34 94 77 87
24 61 19 23 41
75 46 9 21 64
88 15 40 89 86
32 47 93 3 58
74 16 44 21 30
1 11 71 97 42
88 59 52 28 75
58 26 23 76 8
33 65 80 95 14
54 29 7 80 33
20 18 82 26 93
72 4 45 89 98
99 16 8 22 34
86 61 51 43 64
96 20 88 78 73
65 84 93 79 48
25 99 13 60 41
37 24 82 8 89
44 10 4 58 57
28 70 42 66 15
3 35 52 49 4
77 23 16 30 24
67 75 8 29 47
39 32 80 22 55
70 61 5 77 9
3 23 42 86 31
99 22 41 14 17
93 63 25 10 30
28 52 81 89 40
78 18 42 48 14
4 95 87 64 32
13 10 72 90 46
68 16 57 80 77
50 69 5 63 96
89 15 13 68 84
37 79 56 97 34
60 48 91 87 96
32 25 78 55 11
1 67 57 93 92
89 94 12 46 21
61 67 26 40 76
86 78 6 41 56
35 64 28 73 98
30 17 88 70 71
37 57 36 6 32
89 26 27 22 29
80 49 88 0 46
70 18 50 14 19
34 84 79 90 98
41 23 10 4 88
26 55 17 71 15
68 49 0 14 97
27 61 31 74 99
89 33 64 32 5
63 44 98 56 47
72 2 28 89 77
36 24 26 14 21
7 58 32 31 86
33 0 57 54 4
2 89 46 59 6
62 67 84 95 98
8 12 75 70 88
45 93 38 61 47
37 55 76 82 92
2 50 19 35 34
94 0 48 75 16
18 92 46 38 32
65 78 22 85 77
69 73 88 30 60
98 21 79 41 39
64 1 91 7 44
45 32 72 22 38
78 28 97 69 33
55 12 53 9 61
94 39 67 82 18
11 86 43 92 0
44 8 66 3 91
62 56 38 32 89
27 2 76 90 31
21 79 89 70 85
73 76 92 15 33
36 63 44 99 19
35 75 88 65 3
48 54 97 27 2
35 8 51 77 29
1 11 38 67 99
2 18 94 32 24
54 82 21 98 7
20 0 48 83 74
77 82 68 18 58
9 78 85 59 55
15 73 56 46 10
80 38 26 8 96
41 84 35 86 12
36 89 27 38 22
53 46 5 84 90
23 7 63 29 17
92 41 97 0 43
74 33 26 98 19
69 40 35 84 3
56 49 55 2 28
85 14 50 12 27
65 73 6 42 23
64 68 48 62 22
57 8 21 98 66
39 92 16 95 87
49 1 51 68 48
46 84 17 35 80
20 47 3 75 34
23 15 77 3 91
33 58 69 66 14
88 47 18 16 99
62 89 86 7 67
90 57 35 45 29
89 12 29 39 78
26 52 10 47 97
68 90 65 56 33
63 8 13 27 42
30 66 91 16 51
95 91 88 40 97
63 54 68 26 52
56 76 78 83 62
13 65 90 49 94
44 74 79 48 81
24 27 11 74 0
38 56 53 25 60
50 51 49 10 72
76 34 52 81 9
80 99 82 1 67
90 88 71 53 26
70 19 57 61 89
64 30 0 9 56
4 21 62 38 82
51 40 55 81 20
66 14 90 76 93
21 57 27 55 32
22 43 67 29 81
49 53 39 96 79
12 48 88 63 33
15 7 99 55 84
53 80 47 75 36
1 22 39 91 82
13 76 40 27 81
57 93 8 48 28
82 58 5 84 25
61 19 83 22 44
85 3 14 10 97
35 26 79 20 73
99 21 51 47 81
14 32 63 18 70
50 91 67 1 19
7 31 54 11 8
51 78 35 72 77
47 73 22 5 76
2 22 11 31 13
66 21 83 94 87
69 5 59 14 53
95 41 90 43 92
42 77 10 88 8
19 88 91 43 17
62 83 68 94 28
73 36 58 21 66
55 24 90 12 77
45 6 49 27 63
6 73 93 67 18
29 33 94 24 34
57 96 27 37 60
92 88 81 12 16
49 98 30 10 72
17 40 36 96 68
91 38 88 9 6
16 35 63 25 37
81 43 78 64 52
46 44 69 67 13
73 36 70 5 57
66 55 27 54 44
20 4 68 58 26
96 37 76 80 47
61 2 92 71 64
12 51 54 34 68
69 99 64 44 98
79 67 90 46 65
31 56 45 43 30
29 18 94 19 59
70 26 91 97 0
46 56 93 80 52
47 25 12 31 77
95 72 36 74 2
38 48 68 54 73
79 89 77 24 21
30 14 46 11 38
3 70 84 67 48
45 20 40 63 35
86 74 2 76 43
97 20 2 82 84
8 92 71 88 33
64 26 99 93 66
30 40 28 38 73
62 43 5 81 22
9 47 50 8 62
42 1 80 21 84
66 19 32 2 30
76 97 85 65 45
70 26 73 72 93
80 99 91 96 25
22 76 81 62 51
10 64 53 54 70
55 8 49 60 1
40 67 14 89 16
92 19 72 71 40
29 22 86 43 12
0 65 78 93 10
54 55 42 61 82
52 47 81 99 83
81 22 90 66 82
92 56 63 79 32
72 60 30 42 20
91 38 10 70 13
46 52 47 11 69
11 86 32 54 47
87 38 74 41 69
17 23 36 61 29
97 68 62 65 83
30 0 28 72 19
55 65 28 7 5
90 93 99 48 80
34 94 82 19 86
49 39 69 75 71
8 24 43 33 21
39 70 7 56 20
24 67 86 45 1
33 44 83 76 2
46 78 17 94 48
28 4 30 77 79
18 99 73 55 30
88 92 13 97 1
91 49 11 48 83
94 41 5 29 72
61 17 84 64 90
9 13 65 1 85
11 20 30 86 84
35 83 99 32 38
41 7 6 49 58
90 87 76 23 28
89 16 91 76 78
29 26 27 3 90
42 94 43 9 57
59 66 80 11 24
31 53 75 28 20
82 65 50 30 79
19 53 94 17 59
33 47 78 75 7
84 25 80 83 76
81 95 72 11 21
28 26 52 5 3
4 59 51 32 41
19 58 42 90 43
22 89 39 40 24
36 57 64 20 9
44 65 41 79 75
63 76 6 51 30
12 21 73 29 97
42 55 54 53 25
0 89 47 14 92
56 4 60 63 21
20 50 24 77 22
67 66 64 91 28
36 57 68 87 98
7 86 42 33 39
34 15 64 46 50
56 7 99 69 89
83 23 57 13 70
86 71 85 36 98
33 76 8 54 42
22 88 25 32 45
2 21 40 11 16
84 37 90 27 69
51 1 89 49 15
72 96 0 65 6
97 79 90 95 5
14 96 57 40 30
70 60 52 33 36
10 86 28 51 7
88 20 99 27 63
84 6 57 66 62
56 80 97 55 58
92 46 81 21 26
99 29 27 63 87
39 20 7 35 48
84 40 26 1 46
28 42 29 5 45
63 82 17 31 6
30 78 2 89 67
14 47 60 33 32
40 89 32 50 90
1 5 83 41 77
19 48 6 11 70
78 56 93 36 73
80 9 21 26 22
8 97 13 2 38
70 61 67 55 16
35 42 33 9 28
26 93 86 4 65
79 57 19 98 62
42 91 75 97 66
50 12 53 52 20
56 70 96 5 21
89 57 83 18 17
77 72 95 38 98
40 98 10 67 90
16 7 75 23 13
78 38 53 45 20
0 28 87 94 25
26 83 34 56 8
83 43 49 31 73
62 54 89 12 34
92 35 57 91 52
58 80 20 15 90
51 13 61 8 17
19 65 73 81 5
57 71 52 51 22
48 53 15 34 66
63 45 96 47 49
58 42 56 62 76
44 9 76 49 75
78 51 87 39 54
29 62 47 42 97
48 73 50 89 84
0 40 38 20 81
65 34 92 70 36
24 54 41 31 13
28 40 93 57 20
19 59 89 51 77
80 69 85 76 14
67 78 60 98 88
64 46 4 84 25
50 87 74 56 42
59 0 7 31 61
93 12 9 33 32
29 25 94 40 53
49 77 65 27 18
5 92 75 90 47
46 16 82 1 21
22 3 78 13 85
16 2 12 64 57
51 28 29 46 66
45 84 37 35 50
90 75 34 47 39
10 68 4 31 5
30 23 47 48 7
73 16 71 12 25
91 53 43 79 0
81 64 35 93 37
83 52 87 46 85
15 53 29 5 96
23 61 52 36 83
54 64 99 16 68
60 82 90 58 13
42 14 59 80 27
11 54 7 24 96
43 32 5 95 93
22 49 85 64 40
51 18 39 47 34
63 21 80 75 82
32 6 43 27 25
4 20 40 59 58
46 47 8 65 33
12 21 29 84 2
86 30 26 62 37
34 58 13 38 41
40 53 52 54 94
37 74 16 25 99
22 62 11 61 51
27 96 6 44 0
68 87 53 96 90
17 49 45 13 93
21 38 62 35 27
56 1 65 10 33
16 48 22 47 67
90 79 22 24 72
63 65 18 12 11
69 37 1 10 21
73 45 64 4 8
75 77 25 80 76
84 6 82 5 21
79 62 42 78 35
39 41 59 65 29
25 54 7 31 93
43 86 15 61 96
1 80 34 86 3
12 49 29 7 82
16 70 23 45 2
17 75 52 28 13
38 25 74 77 39
16 11 70 63 14
25 61 13 84 34
96 24 30 38 39
75 72 59 97 91
8 4 62 19 58
5 66 76 33 29
72 92 7 87 73
68 94 93 60 61
21 3 10 20 89
35 47 34 48 59
32 79 54 30 93
19 45 4 26 50
48 86 38 6 85
25 61 66 55 51
68 27 39 20 7
40 57 61 28 85
54 96 20 99 69
83 33 91 2 93
92 30 53 12 16
35 73 58 65 98
60 3 95 59 52
75 89 91 96 92
66 8 34 45 21
6 39 2 50 55
19 26 86 12 94
93 55 44 91 8
81 89 23 77 97
2 92 6 76 39
21 0 56 90 51
16 10 5 32 66
4 62 54 89 43
75 22 13 10 68
91 71 69 56 96
55 12 53 21 39
19 5 51 70 3

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?_

51
day-05/day-05.py Normal file
View File

@@ -0,0 +1,51 @@
#!/usr/bin/env phyton3
from pathlib import Path
from collections import defaultdict
def parse_line(line):
p1, _, p2 = line.split()
x1, y1 = p1.split(',')
x2, y2 = p2.split(',')
return int(x1), int(y1), int(x2), int(y2)
def part_1(input):
result = 0
field = defaultdict(int)
for line in input:
line = line.strip()
x1, y1, x2, y2 = parse_line(line)
if x1 == x2 or y1 == y2:
for x in range(min(x1, x2), max(x1, x2) + 1):
for y in range(min(y1, y2), max(y1, y2) + 1):
field[(x, y)] += 1
result = len([k for k in field if field[k] > 1])
print("Part 1 result:", result)
def part_2(input):
result = 0
field = defaultdict(int)
for line in input:
line = line.strip()
x1, y1, x2, y2 = parse_line(line)
if x1 == x2 or y1 == y2:
for x in range(min(x1, x2), max(x1, x2) + 1):
for y in range(min(y1, y2), max(y1, y2) + 1):
field[(x, y)] += 1
elif abs(x1 - x2) == abs(y1 - y2):
dx = 1 if x1 < x2 else -1
dy = 1 if y1 < y2 else -1
for n in range(abs(x1 - x2) + 1):
field[(x1 + (dx * n), y1 + (dy * n))] += 1
result = len([k for k in field if field[k] > 1])
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)

500
day-05/input.txt Normal file
View File

@@ -0,0 +1,500 @@
456,846 -> 221,846
980,926 -> 73,19
682,930 -> 562,930
766,592 -> 274,100
247,685 -> 247,21
106,800 -> 635,800
953,340 -> 135,340
293,223 -> 293,12
454,196 -> 454,463
886,766 -> 164,766
592,590 -> 192,590
436,982 -> 436,545
731,571 -> 420,260
741,11 -> 466,11
727,541 -> 579,541
341,553 -> 25,553
942,470 -> 942,196
203,600 -> 203,647
965,595 -> 949,611
554,306 -> 554,401
902,438 -> 902,728
864,609 -> 525,270
187,790 -> 187,323
956,950 -> 427,950
847,554 -> 422,554
935,900 -> 701,900
192,854 -> 866,180
512,946 -> 543,915
978,979 -> 491,979
708,61 -> 708,878
738,508 -> 282,52
23,25 -> 841,843
204,750 -> 204,797
703,500 -> 703,419
14,311 -> 694,311
646,301 -> 785,301
397,168 -> 439,168
680,931 -> 561,812
540,448 -> 90,448
706,668 -> 91,53
848,319 -> 318,319
198,948 -> 198,307
686,58 -> 686,541
867,234 -> 867,498
134,125 -> 134,688
824,566 -> 53,566
437,167 -> 276,167
94,65 -> 638,609
36,971 -> 971,36
494,330 -> 494,197
920,438 -> 920,364
698,84 -> 49,733
59,842 -> 59,876
328,577 -> 328,677
757,701 -> 134,78
466,274 -> 135,605
81,925 -> 988,18
40,142 -> 882,984
50,96 -> 882,928
782,47 -> 782,427
247,599 -> 24,599
112,812 -> 191,733
487,198 -> 144,198
327,663 -> 327,756
117,76 -> 688,76
530,71 -> 530,958
558,602 -> 671,489
677,830 -> 677,556
529,669 -> 349,669
336,966 -> 341,971
20,31 -> 851,862
423,880 -> 423,573
521,657 -> 552,657
412,822 -> 18,428
423,311 -> 423,105
381,614 -> 705,614
521,248 -> 394,121
286,47 -> 286,403
286,27 -> 711,452
347,61 -> 489,61
760,454 -> 760,954
746,573 -> 911,573
839,933 -> 839,776
124,815 -> 290,649
577,848 -> 419,848
393,206 -> 410,206
364,755 -> 881,755
788,68 -> 788,215
94,798 -> 192,798
292,250 -> 453,250
601,545 -> 293,237
438,923 -> 438,655
70,757 -> 887,757
184,402 -> 818,402
586,49 -> 103,49
202,315 -> 735,315
534,504 -> 534,523
367,236 -> 367,736
24,163 -> 24,240
185,426 -> 634,875
485,189 -> 39,189
556,30 -> 374,30
969,821 -> 676,528
254,435 -> 254,43
290,615 -> 741,164
345,601 -> 120,826
224,641 -> 887,641
190,716 -> 581,325
552,646 -> 552,393
413,177 -> 413,103
397,900 -> 360,900
138,980 -> 138,55
909,891 -> 909,593
926,986 -> 79,139
954,67 -> 53,968
180,30 -> 595,30
823,165 -> 823,660
285,176 -> 375,176
915,826 -> 184,95
735,230 -> 667,230
934,865 -> 917,865
48,602 -> 737,602
477,319 -> 385,411
981,17 -> 11,987
458,401 -> 24,401
118,415 -> 849,415
176,678 -> 176,852
567,753 -> 567,37
285,868 -> 830,323
555,623 -> 822,623
522,546 -> 674,546
880,21 -> 23,878
591,103 -> 591,407
434,64 -> 434,401
245,968 -> 275,968
726,510 -> 450,786
768,366 -> 768,738
488,745 -> 488,94
675,674 -> 675,705
618,237 -> 265,237
802,709 -> 802,59
144,696 -> 144,542
547,381 -> 547,799
78,667 -> 78,916
409,271 -> 302,271
294,694 -> 938,50
140,571 -> 97,571
682,875 -> 682,534
748,816 -> 748,183
84,622 -> 84,258
485,696 -> 582,599
909,233 -> 954,233
203,711 -> 203,350
335,904 -> 455,904
578,778 -> 578,21
830,954 -> 902,954
78,252 -> 78,682
920,220 -> 684,220
309,301 -> 104,301
270,795 -> 270,919
906,479 -> 304,479
627,164 -> 627,986
122,960 -> 915,167
664,916 -> 770,810
692,810 -> 826,810
981,951 -> 192,162
183,423 -> 809,423
632,464 -> 567,464
94,266 -> 94,587
261,770 -> 569,770
51,403 -> 466,818
631,645 -> 187,645
141,238 -> 141,145
357,21 -> 173,21
138,248 -> 839,949
889,957 -> 807,957
399,431 -> 105,725
548,331 -> 548,821
790,844 -> 43,97
675,671 -> 221,671
874,143 -> 620,397
205,435 -> 205,546
521,434 -> 822,133
141,86 -> 257,86
427,28 -> 290,165
49,694 -> 567,694
846,344 -> 266,924
425,910 -> 433,918
956,498 -> 485,27
798,498 -> 798,634
879,13 -> 766,126
737,475 -> 737,425
338,473 -> 425,386
510,615 -> 214,319
758,415 -> 758,490
969,208 -> 239,938
917,188 -> 917,528
34,820 -> 806,820
85,633 -> 857,633
262,355 -> 262,748
373,784 -> 971,186
146,577 -> 60,663
613,570 -> 613,199
300,319 -> 300,108
764,171 -> 764,17
555,921 -> 555,825
241,197 -> 770,197
600,832 -> 600,807
934,987 -> 20,73
960,730 -> 837,730
976,50 -> 46,980
829,834 -> 153,158
785,835 -> 785,58
586,633 -> 689,736
804,250 -> 348,706
226,539 -> 16,539
411,940 -> 98,940
289,589 -> 893,589
738,616 -> 738,55
225,54 -> 542,54
793,246 -> 303,736
332,752 -> 984,100
413,18 -> 839,444
840,122 -> 840,233
989,970 -> 215,196
329,361 -> 573,605
242,537 -> 242,619
943,898 -> 943,535
469,865 -> 501,833
226,717 -> 196,687
819,803 -> 712,803
532,663 -> 532,672
61,931 -> 940,52
623,218 -> 274,567
281,326 -> 281,790
815,176 -> 679,176
790,862 -> 942,710
18,771 -> 18,514
479,377 -> 309,377
704,402 -> 704,150
961,335 -> 492,335
745,829 -> 745,477
556,543 -> 771,543
832,336 -> 917,251
742,755 -> 742,174
206,735 -> 493,735
151,216 -> 312,55
445,157 -> 615,157
781,143 -> 781,76
833,717 -> 514,398
357,14 -> 357,36
771,405 -> 771,422
662,886 -> 169,886
689,990 -> 22,990
680,445 -> 379,445
92,369 -> 502,779
64,948 -> 64,363
295,957 -> 976,276
113,920 -> 634,399
542,662 -> 305,899
566,514 -> 566,645
528,106 -> 549,106
205,367 -> 821,367
313,105 -> 313,928
532,177 -> 532,664
862,773 -> 905,816
800,796 -> 911,796
870,80 -> 11,939
188,900 -> 154,900
420,509 -> 520,609
540,863 -> 28,863
31,72 -> 78,72
823,648 -> 503,648
879,252 -> 606,252
677,117 -> 677,507
743,303 -> 196,850
220,491 -> 220,891
216,815 -> 577,815
540,819 -> 745,819
152,721 -> 382,721
280,745 -> 985,745
479,367 -> 358,488
913,413 -> 649,413
40,678 -> 817,678
467,533 -> 467,214
132,68 -> 843,779
519,109 -> 669,259
619,791 -> 221,791
114,622 -> 628,622
951,636 -> 866,636
172,569 -> 775,569
244,972 -> 173,972
283,64 -> 739,520
68,604 -> 68,156
529,30 -> 529,925
813,883 -> 137,883
893,231 -> 629,231
673,658 -> 673,389
725,899 -> 218,899
317,318 -> 105,318
82,706 -> 100,688
222,227 -> 440,227
810,371 -> 810,985
414,321 -> 289,446
901,158 -> 260,799
198,967 -> 717,448
928,454 -> 875,454
974,437 -> 974,764
657,13 -> 760,13
498,966 -> 976,966
66,104 -> 66,15
773,569 -> 980,362
420,496 -> 403,513
57,920 -> 85,920
879,551 -> 879,662
98,395 -> 98,398
483,685 -> 483,55
222,935 -> 586,935
89,926 -> 807,208
744,160 -> 744,462
588,973 -> 588,548
312,572 -> 38,298
27,131 -> 552,656
591,935 -> 591,86
907,478 -> 907,279
981,75 -> 981,972
316,947 -> 935,947
906,38 -> 906,216
374,521 -> 345,550
579,29 -> 579,107
444,636 -> 444,557
458,608 -> 830,980
479,839 -> 155,515
766,600 -> 766,71
976,965 -> 31,20
928,49 -> 269,708
787,238 -> 787,983
583,742 -> 112,742
966,268 -> 554,680
671,354 -> 671,966
274,340 -> 274,894
673,185 -> 607,185
73,171 -> 874,171
861,526 -> 861,410
739,591 -> 739,138
209,355 -> 209,146
286,501 -> 887,501
495,902 -> 700,902
192,889 -> 821,260
400,21 -> 154,21
861,301 -> 325,301
552,990 -> 511,990
908,21 -> 11,918
127,724 -> 821,30
935,46 -> 170,811
947,91 -> 374,91
625,420 -> 265,60
214,228 -> 546,228
375,547 -> 715,887
516,350 -> 870,350
610,138 -> 665,193
214,621 -> 678,621
497,248 -> 600,145
549,558 -> 576,558
364,537 -> 364,312
840,324 -> 310,854
441,945 -> 441,458
459,531 -> 459,100
937,113 -> 150,900
277,405 -> 259,405
409,527 -> 409,359
534,766 -> 534,740
534,934 -> 681,934
456,419 -> 83,419
871,986 -> 873,986
14,59 -> 916,961
911,963 -> 971,963
25,325 -> 139,211
937,184 -> 354,767
460,416 -> 289,245
193,171 -> 861,839
840,299 -> 840,911
531,45 -> 531,619
599,315 -> 455,315
455,97 -> 455,811
38,748 -> 392,748
841,79 -> 841,88
105,571 -> 105,545
801,458 -> 344,458
491,535 -> 558,535
835,814 -> 223,202
563,85 -> 405,85
410,396 -> 600,396
273,670 -> 818,125
671,647 -> 817,647
46,892 -> 678,260
456,736 -> 110,736
962,941 -> 619,598
388,406 -> 53,71
558,895 -> 227,564
944,182 -> 807,319
484,898 -> 59,473
808,214 -> 488,534
451,679 -> 155,383
858,931 -> 381,931
723,377 -> 723,281
694,283 -> 182,795
385,191 -> 320,256
33,380 -> 584,931
480,91 -> 817,91
677,91 -> 677,126
291,651 -> 760,182
832,962 -> 153,283
38,60 -> 479,501
249,350 -> 789,350
603,341 -> 266,678
52,303 -> 52,102
911,201 -> 559,201
46,210 -> 46,275
960,212 -> 554,212
375,374 -> 169,580
10,10 -> 989,989
844,140 -> 40,944
916,408 -> 916,815
834,401 -> 834,169
553,479 -> 784,248
543,452 -> 543,848
854,910 -> 334,390
685,491 -> 793,491
552,943 -> 709,943
723,367 -> 124,367
95,55 -> 881,841
155,267 -> 573,267
59,357 -> 84,357
218,435 -> 218,344
491,584 -> 491,649
676,445 -> 676,333
361,618 -> 783,618
220,295 -> 220,267
668,758 -> 299,389
965,845 -> 674,845
285,603 -> 47,603
853,417 -> 853,757
859,906 -> 856,906
55,364 -> 753,364
893,474 -> 978,474
602,32 -> 58,576
171,445 -> 96,370
214,592 -> 214,286
400,946 -> 745,946
559,37 -> 112,484
624,510 -> 90,510
329,714 -> 329,850
458,287 -> 657,287
99,385 -> 99,949
50,736 -> 719,67
273,195 -> 273,306
490,902 -> 490,798
619,131 -> 921,131
266,652 -> 266,730
745,661 -> 745,555
311,878 -> 311,679
491,982 -> 643,830
735,875 -> 816,875
936,353 -> 936,529
792,467 -> 565,467
141,140 -> 141,988
98,171 -> 414,487
257,259 -> 257,484
24,41 -> 969,986
302,453 -> 223,453
807,363 -> 492,678
823,22 -> 835,10
301,94 -> 399,94
946,110 -> 248,808
983,985 -> 21,23
510,145 -> 510,58
13,661 -> 13,639
218,260 -> 218,54
475,846 -> 475,770
458,644 -> 458,529
912,934 -> 912,136
152,823 -> 550,823
136,470 -> 443,470
253,871 -> 905,219
765,212 -> 793,240
11,402 -> 11,42
348,813 -> 348,768
368,321 -> 823,776
343,495 -> 343,809
117,616 -> 117,273
92,92 -> 732,92
914,31 -> 28,917
259,944 -> 214,944
630,759 -> 462,759
134,653 -> 134,610
14,989 -> 988,15
139,181 -> 139,451
598,636 -> 598,442
263,42 -> 686,465

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?_

32
day-06/day-06.py Normal file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
numbers = [int(n) for n in input[0].split(',')]
count = [len([x for x in numbers if x == i]) for i in range(9)]
for _ in range(80):
tmp = count.pop(0)
count[6] += tmp
count.append(tmp)
result = sum(count)
print("Part 1 result:", result)
def part_2(input):
numbers = [int(n) for n in input[0].split(',')]
count = [len([x for x in numbers if x == i]) for i in range(9)]
for _ in range(256):
tmp = count.pop(0)
count[6] += tmp
count.append(tmp)
result = sum(count)
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-06/input.txt Normal file
View File

@@ -0,0 +1 @@
4,1,1,4,1,1,1,1,1,1,1,1,3,4,1,1,1,3,1,3,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,5,1,2,1,1,5,3,4,2,1,1,4,1,1,5,1,1,5,5,1,1,5,2,1,4,1,2,1,4,5,4,1,1,1,1,3,1,1,1,4,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,1,2,1,1,1,1,1,1,1,2,4,4,1,1,3,1,3,2,4,3,1,1,1,1,1,2,1,1,1,1,2,5,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,4,1,5,1,3,1,1,1,1,1,5,1,1,1,3,1,2,1,2,1,3,4,5,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,3,1,1,3,1,1,4,1,1,1,1,1,2,1,1,1,1,3,2,1,1,1,4,2,1,1,1,4,1,1,2,3,1,4,1,5,1,1,1,2,1,5,3,3,3,1,5,3,1,1,1,1,1,1,1,1,4,5,3,1,1,5,1,1,1,4,1,1,5,1,2,3,4,2,1,5,2,1,2,5,1,1,1,1,4,1,2,1,1,1,2,5,1,1,5,1,1,1,3,2,4,1,3,1,1,2,1,5,1,3,4,4,2,2,1,1,1,1,5,1,5,2

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?_

35
day-07/day-07.py Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env python3
from pathlib import Path
def calc_fuel_exp(crabs, h):
fuel = 0
for c in crabs:
d = abs(h - c)
fuel += d * (d + 1) // 2
return fuel
def part_1(input):
result = 0
c = [int(x) for x in input[0].strip().split(',')]
c.sort()
m = c[len(c) // 2]
result = sum([abs(m - h) for h in c])
print("Part 1 result:", result)
def part_2(input):
result = 0
c_pos = [int(x) for x in input[0].strip().split(',')]
result = calc_fuel_exp(c_pos, c_pos[0])
for i in range(min(c_pos), max(c_pos) + 1):
result = min(result, calc_fuel_exp(c_pos, i))
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-07/input.txt Normal file
View File

@@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,485,546,350,100,791,199,115,144,649,41,1656,163,903,71,384,30,2,251,554,210,434,206,546,759,258,54,1478,48,438,601,326,5,1017,165,168,201,622,864,1338,24,1074,545,499,484,264,345,332,869,297,711,674,346,1139,317,875,242,725,250,1619,1408,956,380,366,187,1034,1555,467,170,114,1136,150,183,304,44,37,333,791,34,540,716,1923,342,6,922,18,24,1189,59,1726,636,442,426,1089,526,298,386,296,623,80,272,240,406,628,238,409,302,35,404,92,48,157,1545,409,1382,151,1656,3,76,14,115,566,650,197,448,573,161,86,140,875,128,319,4,822,530,189,247,667,82,316,274,110,206,1012,166,639,579,459,284,200,16,24,147,743,113,1562,387,60,84,797,14,30,1015,508,88,113,685,658,257,1507,348,30,808,416,9,835,671,16,474,885,230,47,463,1324,1263,183,603,739,0,296,789,1411,339,27,1154,31,882,409,646,92,153,147,974,497,308,85,311,135,627,811,295,698,2,20,1170,789,702,1194,1390,432,257,715,958,150,1295,144,1193,607,67,929,383,1051,1231,393,190,380,1203,1090,1238,143,206,210,1004,304,1305,392,143,1379,665,806,452,185,4,1,201,1104,633,274,493,472,141,674,1261,106,587,244,903,91,158,69,137,922,778,143,692,160,474,7,304,824,657,15,1110,806,295,1565,1162,358,725,877,440,690,13,69,111,304,300,493,249,105,746,20,163,561,913,558,252,13,193,508,12,845,120,205,154,1582,349,1471,529,268,23,689,6,776,565,401,0,623,186,62,95,148,275,1,137,320,0,19,1803,10,100,652,750,226,484,180,46,310,446,667,543,277,139,265,74,171,87,1753,337,162,59,1339,1040,1287,1084,192,169,50,1557,81,1120,271,167,977,76,295,12,54,710,36,364,521,989,1634,720,1031,1204,355,380,859,633,223,1207,221,31,138,1305,779,1026,52,92,216,221,0,980,130,1197,585,1213,63,157,213,993,1123,588,450,256,1021,90,1420,47,386,843,1188,1466,807,596,416,23,32,62,1289,317,368,491,907,1386,114,1620,39,344,1342,43,281,12,1202,257,1357,203,465,174,350,833,125,54,390,687,339,628,819,261,1341,840,643,414,82,373,428,1315,570,1070,686,893,70,728,70,358,1233,189,1247,244,1043,1135,42,531,962,35,30,1462,946,856,145,386,1134,1071,379,740,175,1205,234,354,5,1028,506,58,433,1055,749,854,99,298,1248,619,62,181,258,42,130,1698,1313,672,129,222,127,636,846,24,1324,946,622,689,168,329,301,458,173,591,772,93,282,8,320,106,233,412,556,2,522,369,8,1371,899,503,568,667,1199,92,115,899,952,81,629,175,274,763,204,339,236,317,257,731,1082,1724,211,516,165,91,334,1216,101,21,1340,235,336,1351,723,1745,183,841,104,172,1080,180,493,798,1468,45,1627,59,58,368,560,166,1125,136,26,1238,1580,420,1732,155,55,293,751,194,1723,175,11,30,10,307,57,66,704,285,685,241,565,368,50,181,1047,147,420,1341,20,37,400,798,476,1060,642,134,140,502,254,997,910,636,179,22,612,55,237,258,48,205,412,155,910,192,262,9,91,766,1426,71,5,315,285,186,629,422,1289,397,52,860,1390,106,887,1285,1196,684,36,703,199,4,277,151,82,293,1047,455,21,935,630,736,118,13,30,584,453,1446,381,585,810,177,1028,280,281,184,78,673,126,410,872,524,78,188,121,394,201,1764,609,350,706,428,88,783,189,643,305,516,259,582,309,985,338,21,235,73,44,585,71,983,175,1336,1056,10,8,537,701,1653,657,70,1242,442,52,973,203,173,959,964,272,348,3,567,714,1466,382,129,613,1042,686,461,57,523,740,726,149,1490,867,44,379,1270,547,649,1103,912,1354,985,458,887,603,1016,317,499,690,829,1231,364,772,29,57,357,467,484,202,150,109,95,414,444,383,62,124,645,723,772,881,1553,413,123,248,1085,453,260,214,113,1874,482,942,235,899,122,171,127,913,424,406,49,97,1848,295,1152,111,350,54,1160,2,16,156,448,394,740,49,1237,548,206,1206,775,748,728,48,238,148,109,18,56,64,515,163,609,273,301,396,207,51,478,1183,864,772,450,222,1387,269,40,87,426,164,1270,21,347,316,331,408,914,1046,173,48,398,177,431,47,1055,221,513,226,84,285,566,270,333,343,480,1802,101,683,168,1347,582,80,22,329,350,108,379,14,53,349,43,435,195,102,168,338

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?_

63
day-08/day-08.py Normal file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
result = 0
for line in input:
_, out = line.strip().split(' | ')
out = out.split()
for d in out:
if len(d) in [2, 3, 4, 7]:
result += 1
print("Part 1 result:", result)
def part_2(input):
result = 0
for line in input:
inp, out = line.strip().split(' | ')
inp = inp.split()
out = out.split()
digit = ''
code = {}
for d in inp:
if len(d) == 2:
code['1'] = sorted(d)
elif len(d) == 4:
code['4'] = sorted(d)
elif len(d) == 3:
code['7'] = sorted(d)
elif len(d) == 7:
code['8'] = sorted(d)
for d in inp:
if len(d) == 6:
if len(set(code['4']) & set(d)) == 4:
code['9'] = sorted(d)
elif len(set(code['7']) & set(d)) == 3:
code['0'] = sorted(d)
else:
code['6'] = sorted(d)
elif len(d) == 5:
if len(set(code['7']) & set(d)) == 3:
code['3'] = sorted(d)
elif len(set(code['4']) & set(d)) == 3:
code['5'] = sorted(d)
else:
code['2'] = sorted(d)
assert len(code) == 10
for d in out:
for k in code:
if code[k] == sorted(d):
digit += k
break
result += int(digit)
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)

200
day-08/input.txt Normal file
View File

@@ -0,0 +1,200 @@
bgcfda gecbda abdgf aedfbg eda efcbd ae agfe bdefagc fbeda | ae egdafb ea fcdeb
gfadb fbagcd cagf agecdb adg fdbcg bdcfaeg bcgfde ga efbda | cbfdge dfcebga aedcgb dgbfa
bgdeca agdbe gfb fdbgce bf eafb dfgab efbgdca gebafd dgcaf | gfbdea gfb bacedg adcgf
fbc becgdf fcbdg adcgf edafgb bgec dcgfbea gbfed bc cbeafd | gfacd gcbdf bgedfa cbdefa
ad dcafeb adcef feagc egdbaf aed cadb bgcedf cbeafgd cdfeb | dabc gabefd ecfdb ecgfbd
badcgf gdaf gadbc ebfcg fceabd gacdbe gedfbac dbf df dfcgb | df adfg cbfgad fdb
cfdb dca bdgca efgbad bafcgd fgdba cgdfea cd abdfegc bagec | agcbe adc dabcgf adc
egdab gcefd ecbgd gdebcaf eacb cb bagcdf abgfde dcegab bdc | bc gedbc dcb fgdec
gefa gedabf cfdbea bdfea bdegcfa dgbfa gfd gdcab fg fecbdg | fgd gf dbfage afbdec
fagdb be abdfe agfebdc eab aefdcb acfed bdec fceadg afegcb | be aegcfb efbcag eb
fdce fcadeg afegdb adceg cgafdb de ebcga dae cdagf abdfgce | efcd efacgd edfc afgcd
gefc defca aefbd ec gfdceba dcabge fgcad cae fcadeg bagfdc | fgcad bdfea ebadgcf ce
cdabe dcg cgef gc beadgf fegbd gcbde edgcfb gecfabd fdgcba | dcg deacb fdegab bdafgc
aefc ebcdfa eabfgd deacb ac dac gdbce gfbadce fdeba gcfbda | adc afdbe aefc ecdafb
eabfdg fdgba fac fc ebdac bfcg fcdabg bgdface bacdf fgedca | fdagcb cedba cf fc
db dbg debag fcgdeb gfeba dagfec ceadg bdfecga deagcb acbd | gbdae afebg cfedgba ecdagb
cabegd befcd aefdcb defbcag dgfb gd edg bfgecd gfdce eagfc | dbgacfe efcdg gd bdfagce
abde fbecag gcabde cefdg bgedc bgcea bgd becgdaf gdcfba bd | baegc egbdc cedgf gdfec
gedc eagbd bgadcef aebcdg dfbag eacfdb cabde fgecab ge aeg | gcbeda gabdfec dgecba gea
afedgc deabf gbfceda fg cgdea dgf bdgfec edgaf ebacgd gafc | fgd gfac cbefgad cagde
gfcbeda bfecda dgca ebgacd gbfae ecg gc gaebc cfgebd dbeca | dfcbea cegab cg cge
adefc eabdf bcafeg bd edb fcbeadg dabegc fdbg baegf fgaebd | fcead fcdea fbgd ecdbga
ac bedgc acdb gacbed bfgeadc degcfb aec cafdeg eagfb cegab | agecb gdbec abgcde agbef
cfgbead cfgdeb gafb baecd bg cgdba bgc adcfbg agfecd dcagf | dgcefb bgaf fdbcgae agcdf
agc dabc dcbgef efadgcb ebdgc baefg ac abgce ebdcag facdeg | ecbdgf ac afdgbec decfbg
fgdcb cbdeaf eabfcg cefdb gcbeadf cdeg gc afbdg cfg ecdgbf | cg eafbcd dgec cbfdge
da dbae fdaecb cdebgf afgced bedfc adf cbfeadg bdacf bfcag | dfbec egbdfc bdfca cefbd
abgfced cf cfdbe geacfd bfac bdacfe gdebc bfedga dcf bdafe | bcfa dbfce cf egdbc
dcagb feagbc dbef edafg fagebd bf fedgac fba bfagd dbcefga | fbdga dgafe cfabge fdbe
gbfce bfc acgb fbgaec bc acfedbg gface cgefad bfacde defgb | cbgfe bgfeca gacb degacf
cfgeda bac dbafg dbcfa bdfcage ecfb acedf cb ecfadb eacbgd | gafbd faced dacfb bcef
fbgec eca cbag cfegba ac efacb bgcedf caegdbf fecgad fdbea | ca acgb ebdfa agcb
acgbf cdabegf egbfd dc dacbef dacg gecabf fdc dbgfc dfcagb | dgabecf befgd acgd abgdefc
dagbfc bc agcdf cgbd bfc ebfad dfcba befgac gaecdf becdgaf | cdgb dbcg abfcdg cfb
bdcga ebdgca da cdeafg dac acfbg daeb cdbgef egbfcda bdegc | bcgad adgcbe fedacgb cegdb
gacfeb aefbg fbaegd cedgfb dfcba gc cgae abcdegf gacbf gbc | cfabd debfgac cega egca
abcfeg fdbegc ac bdafgec ebfad fcdae dcga fcged gdceaf ace | gbcfaed ebfda ac gefcab
gefbc gdeacf cag ac acgebf ceab adebcfg bfcag cgbfed bgfad | cga cagdef gac fcgba
gcbe gacfdbe dfgaeb bfecd ecdgfb dfcgae bef fcedg bcfad be | fadcb ebgc cbfda dfcab
ebcgfd dafe adcfg gfacde cfabge bcadg dfc fd fbdaceg gefac | fceadbg fcgabe fade fbeacg
fbacg cef cagebf gacefd ef bgced cfbeg dagcbf fbea cegfdab | eabf fce cdgeabf agefcb
bca gcadbfe ca edca fbgade dcageb ecbgf egacb ebadg dgcfba | cgafbd feagdb gabecd cba
fcebdg cgefb fagbce edcbg bfed eacbgfd cegda db bcd dfgcba | becdg cdbafg bedgfc cgbeafd
fcadebg edbafc cgfda decba egab gcadb gb debgfc cgbdae gdb | bgd agbe eadcbf gdb
cedfa gad ebcfda aedgc defagb gdafceb gebca dcfg dg edgacf | cdfg fadec gedac bcgae
dafegbc dcfbae dce fcebgd egdaf ecadf dfcbag acbe ec cafbd | ced dce ce ec
fegb ef bdcefg fabdgce efd fegdac cbdaf bcdef ecbdg acbedg | debgac edf ef dfacb
afgedb caedbg egfba baecf efgdb gbcadef fagd age cfegdb ag | afbec degbafc fceab eagbdc
cebd dfb dbecgaf gefcb afbegd bd fdcgb gfcebd cgdaf gfeacb | abecfg gacbfe cgfda bfceg
acdfgb ecbga ba ecgdafb deafgc aedgc aedb aecgdb bag gfecb | cgaefd gabce cdgbae ab
be fcgdae fcbag efdbcg ceb cfebg degb cdebfa fgdec gdcabfe | eb dfegc debg bce
fdecg dafgebc fgcae fgd bcfd bcged edbgfc aecdgb fbgade df | afbdecg bgfaed fcged bgedaf
cabd egdfa fbgac bcgafd ebdafgc bd eagbfc dgb fdabg cgbdef | bd bd dbg cbad
fgecba bd dgcbea gfeda gdb geacb ecbd bfadcg efcabdg bgade | dgbae befgacd ecbag afcebg
ga abefc afg fecadbg egfcbd dgfbac edgfac adbg fcgdb gbcfa | dbag fag fgdbcea dcbfeg
geacf cadfe bfaced fg fcdgae fged ebagc cfg edbfgac bfdacg | afcbged efadc fg fcg
cbd cdefbag cfadg bd bfdac abed fdabce dbegcf egabcf ecbfa | gcebdf faegcb ecdgbf fbeadcg
fagcde cgdfa daef acgde ecgadb dbcgf fca eacgbf gcafbde fa | cdage cebfga adceg deafcgb
feadgcb efcag dbfge edba dfcabg ebgfa ba gefbad ebgdcf bfa | abdefg daeb bfa fdgaecb
dgfcea becdfg bdefg abgdfec beg cgedf eb decb bgefac fgdab | beg fedgc egfacd fcedg
fcagbd adbfeg gbeda bgfde ebfcdg dfae gaceb gda ad fdbcaeg | agd gdeba fdgeab ebcga
dbcefa bfaegd fdcb fdeca agceb fcabe cfaged beagdfc fab fb | edabgf gcabe adefbc fba
eac cfgeab agdfc cdgbe gdbefc dabe gefdbca abcged ea agecd | baegdcf bfcegd gdbfce gfcebd
cdbeaf cd dcf adcgfeb defab gbfca dgbafe cead fdcab bcefdg | dfcabe fcd dc cdfgeb
gdcfbe bag dcebg dgabce cfgdba geacb bdea ba febcagd faegc | gcfea dgbfec fecag cefbgd
ebfdgc baecdfg gabfe acbg bce dgabef cdeaf gfabce ecafb cb | eabcf gdfebc egfabc gafbed
dbgacfe adgcf cedafb ecbad bdfe bf egcdab abfgec fab cbfad | bdfe fba bcfead abedc
dca afdcb befagc gacedf dbecf cgfabd da dbag ceabfdg afbgc | da adgb gfedcba dabfcg
fgcda bcgf cgfeda bfdcga gbd badfg cebafdg gcadeb bg dfbea | fcbg geacfd abfgcd bafgcde
cf gacbfe feadcb cdgaeb fbcd fcdae efdag ecf gcabedf edacb | ecabgd gbcfaed gebcad efdac
ae abcefdg faedbg agedf dgbfce gabcfe adfcg dfbge efa bdae | dcfebg fae agfdbe egfdba
fegdcab dgcaf bfd fgdab edcgaf eadgb fbcg deacbf fb gbdcfa | acefdb gdcaf fcgda gabde
ca cefa bgcfed facdbe dgbfeca adc dbacfg cabde edabg fcbde | dcbefa bedfc dfcgab dbfacg
ce bgcedf cgdef gdabfe cabefd dec aegfdcb dgcfa gcbe fgbde | cegb dce aedbfc ce
fcgaebd ef febdac feb cafbd edcfb fabecg acbdgf dfae ebgcd | feb bfe fegacb bafdec
fgdec adcb dfbega afcgeb egcbd db bcaeg beagdc dbgfcae deb | bd egcdab fecdg bed
ad dcaeg edcbag fgbdeac acd gcdfeb afegc ebda cadbfg dgbce | bfgadc dca ad cda
bdegaf egfdb afb acbegdf fgdba af feda bcfgea fbdecg abgdc | fdegb fabecg degabf adgbef
gdfbe gcf gbcda cadfbg cfdbg gceafb dfca badegc cf cedgbfa | fc fcg egcabfd bdcag
fcbag ebdcgfa ce fgaced fbgeca gfbec cgdfba fbgde efc aecb | beac ce cgbfe bfegd
dcgaef gedfb fegdabc eabfdg dgc cefgdb bafdc cg gecb gcdbf | debgcf gbcfd gbce baefgcd
bfacdeg cedgf gfdbec eca edgcfa dbeaf ca fcdea dcebga cgfa | cefgd gcedfa bdfegc aefdc
cdfea ebgdf ab adebfgc bdcegf gbda fab dagfeb ecgabf ebfad | defba cfdea ba ab
gbefcd cbedf fcaed dgfb dacgbe ebfgca fb bfecadg cfb gcbde | eabgdc egcbdf dcgbef fb
afgedb cabdefg afcdb efdgca ef abgde edf ebfad agdbec fegb | edagcfb cdfba efdagc dcebga
dcfg afbgedc efgdb agbfe fde dgbce dabegc gdbfec df fadcbe | dbfeg gdfc efd faegb
ebgdf acfge bae gbafe efdabc ba feadbg bgda gfebdc gedacfb | fbdcge dbag ab bafedc
abedcg dcagfb cf dagbc afc fceabd fbcag dfcaebg dcgf abefg | afcdbg dcfabe fcabg adgcefb
fdbc egabc bfegc fc gbfced aefbgd gfebd gfacde bgdafec fec | fbcd fedbg daefcg cef
bgacde fegad febc gfacedb gebca gaebf bfa efbgca gfadcb fb | bf dcgfeba fdcgeab fb
cgdfe fagec fbace afecdb acg fgcbead ga abfcge bcgdfa ebga | gabcef cfgde dfgcba gcbdfa
bgae ae acegfdb dbfeg dbcgfe ebgafd dfcba cgefda adebf eda | aebfdgc bedaf feabd fbadc
dfagcb cbdgea efabdcg ecdab gceaf daecbf df cdefa fad befd | afd gefca adgfcbe fd
dcfeg acgf aec degfbc ca efbcgda fcdea gfdeac faedb cegdab | egcdaf gebdcf bdaegc aefdc
cgebaf cbdagef cadgb efda degbaf fgbea dbe bgfdce edbag de | de feda dagbe agfcdbe
eagbcfd eag efcad acefgb ag gadce fadg bdgec baefcd aefgcd | bgecd edcfab baecfg ga
adbcfge eadfb faecd ab dba aebgdc ebdgf ecbfda fcba gdfaec | efcad febdg bfdgeca dba
adgfc dfegc cdfeag edf dbecagf fdabge fbgce ed dcea cagdfb | agdfbe def efd deac
cgb bfgde bdcfa fgcbd fgeadbc fcabed befcag cg dacg bgafcd | cg acfedgb bcagfe bdfagc
fcbdge egdac acgfe efgcad fc cdaf gacefbd fcg faebg dcebag | ecdag efcgda fdac dgcbea
dgabef fdabg bcdagef dcfbg fgac adfcbg fc gdbec cbf fedcba | bdcagf dfagb fabgd cf
defc cgafed ecafbg fbdag aef cdgbae gecad facdgbe ef dgaef | fe fbecga gfdba adgebc
gbdfac gcfbd dgceb bcgdfe fedabcg gdfe fbceag eg baced gec | gacfbe cdgebf cge cge
acgdbe cfa gfba cfdab cbgadf cefdb bcfadeg fa fedgac agcbd | abfg agcbd gcfeabd dfcbe
dagebf defcb dgecaf edfbac aecbdfg deacf be aceb bfdgc fbe | ebac dcfagbe feb bcae
gbcfad fadgbce ag fdcegb gbda ceagfd agc cfdbg gfcba eabcf | dagbcf gdacfb dafcgeb eabfc
dbgcea ag acg dbag aegcd bgdefca bdgce ebgfac cgbedf acefd | agecfb abdegc dgcefb agbd
dgebfca cgdfa bgafe eda cgedfa dfage fgdbac ed ecbfda egdc | befgdac daefg acfdg acdebf
fgadce dcb abgdefc cedgf fecdb cb dgcfbe bgdeca gcfb defab | fcegbd cb gdfce gfebcd
cfbdgea ec dacgf fdeab adfec dbce dbegaf cfe deafbc ebfgca | ec decb ebagfd gcfaedb
gd bcegad afdec dgbf efcdagb cdg fgdcba fgadc abfgc cgafbe | gacfb bdcgea fabcg gd
dbega afebdg ca ecabdg deca cfgbae bca gebdfac gfcbd adgcb | cab caed abc cead
cgdbfe ab fgade gefcab afcdebg agdbe dcbeg bdca abe cgabed | ba debgc gcefba cegdab
cdfegb dbafe abdgce cbfgea bfg fg cgbed dfgc bfceagd dgbfe | gf fadgcbe aebgcf cafegb
ecafb efgcab gbdecaf fbega egabd gef dfbcge fg dcbfae cafg | dcgfeb bafdceg agfbe gf
efdcbg fgbca dcgbf dbfcag faebcd abegc afc egcdabf fadg fa | fa afc adefgbc fbcedg
dfa gcdbeaf dgbafe gacfbe dcagb adcbfe febca df dbfac cefd | adgefbc daecfbg efbca bagefdc
aec fecbgd aegdc fgcad bcedaf bdcge gbea defacgb ae acbgde | ae deacg edgcb afdecb
cdab bedgf dcbegaf gdfac bc efabgc fbc cdbgf afgcbd efdagc | cfb fgebca cgfdab faedbcg
ab eacbgf bdfa fcaedg fdebagc degba bcdeg afdgeb dgeaf gab | gfdbea afdb afdb gcebd
acbed bfeacd aedgc aeg ag gdbfae egbadc bacg fcdeg gcdbfea | gdaec aefbcd gae bdaegcf
fgadbc geafb dafbgec ecdb ce bcega deacgf egc geadbc cgdab | ecgba cdbe gacedf aecgb
gbea eb cfdbaeg cebad gbdac fcdae dgebcf fcabgd adgbce ceb | dagecb ebc dagbc ebc
fbeda ga aeg aebgd agfd bdgcfae fedgab dfeacb fbaceg begcd | ga ag afcbeg bfecad
efabcg bdc fedgc adebgcf gcabed cfdbg dfba db cafgb bfcgda | dcb bdc agfbc afbegcd
fbade bcagfe cgfea cdag dfc cfade begcfd cd gecbdaf dfceag | cgda gcda fcd dc
dbcfa bg gafcde bdceafg gaecbf cbg bgde bcgad aegdc bceagd | gcdab eacgdf bg cgb
ecfagd fbadcg fgbae bdgae gbf befc facgbe gecbfad fb eagcf | gabed fb egabfcd facbegd
abef dcabf ecfdba afbcgde bgdfce af acf cagdb gaefdc bfcde | cfadb abef bcfde cgfdbe
acedbf abcfdeg bdceag bgade acdfgb aeb bgdfe eacg ea gdabc | begda dfbge ae badcg
fcda abgef da dea dacgeb fbdae cedfab bagfced ebcgfd cdfeb | bcedf edcbfg dbaceg bdcfe
dgcfba cadeb agcdb aebfgc gdaf bcfdg gac ga fgebcad fdcebg | dgacbf ebdca egfdcb bcfdg
afbedg egbd bfcdag edgcaf gbdcfae afegb fgdae gbf fcbea gb | dgbe edbg bg efdag
acb abcfde fcgead efacd eabfc dbfa ab egfcb bedgac fdcaebg | defca acb caefd acbdge
afcde dbe fbcaed aecbfgd bfec be eadbc afgdbe dacbg gcfead | be ecfb fgdabe aedgfb
fdeacg cbgfa gefb bfc bf gbafce bdefac agefc aegcdbf agcdb | bf gceaf efbg bfcaeg
fdce gacfb agbdec cgafd df caegd gdcefa bcgadef agfbde adf | fagbc fcgdea fd bfgca
gdfacb aebcg ga dgae egfcbda cedbg dbegca acg gbedfc ecbfa | ga eagd eagd edgfbc
cgbdf eb caged aegfcd egb badecg gdbce ecba egfdba dfbaecg | cagfdbe geb dgcefa dafegc
cd gbecaf acfdb cdfg cda gbdface gfbac dgacfb bfade dgecab | dfcgaeb dc bgdacef bfacd
geab fecdb gb feabcgd ebgcda gdcae gecdb gbc afegdc gbfdac | debcga bgdaec dceagb cbaged
edgfcba agd bacd afcdg degbfc acefg efdgba bgadcf da gdfbc | gcfbd dbac cgfdb gecaf
bfca fcbged dfgab cgbadf cdfabge degcab adfeg cgfbd ab bad | abcf afbc gafdb acfb
gc fdgac fgeadb afdcb gecdfa gac gecafbd cabged gfce gaefd | ecbdgfa cag bgfeda eafgcd
eagdc efdacbg cdgbe cbfdg egadfc egb aebc be gefabd dbecag | bafcgde beg gbacde dcbge
ac afbgcd gfdba fabecgd ebdafg bac cfag adgcbe abcfd cfdeb | adbegf egacdb bac acb
cafbed gf gfd cgdba eadfbcg gfdcb gbef cfdbe fdgeac gedbcf | facbegd cbdag fg fdg
fbgcd fadb fcd fd dfcbga decabg egbcf gacdb fcgeda aecfdgb | bedcafg cgdbaf fabgdec dcbgf
gb bcgeaf aebfcd cdefbga cbaef cgafb gbf dfacg gbcfed bega | bg bfacg bdfcge geba
cadfe afbgced aedfcg cebfg aeb fdageb bfcade bfaec ab bcad | bacef bcafe bfgeda baefgd
cgbde abcg cea dbegca ac agced befdgc bgafdec efgda badefc | cae eca gdfae dcega
ab fgcbed agb cafge cegba deabfcg dbgec aedgbc gfadbe bdac | ab bdca cgfedb dgceb
bfgadce cfadg fa cagdfb bcgdf fagbec gfa fbad bgcefd degca | fa ecgad gceabf dcfgb
gfd dcabegf fg cefdg cbefgd gefb ebgdc egacbd gcafdb dcfae | ebfgdc cegdabf cbafged fdg
dfecagb fbag bdcega gecdaf fg cgbad gfdacb fbcde fgd dfcgb | gfd gfd dgbeac fg
gfaed gad feacg dfaebcg deba efgbdc gefbad ad agdfbc fbdge | dga bdae fegbcd dgfabc
agdbc egdcafb caegdb afgdec cbadfg fdgba efdba bcgf gf fga | dfabg cagfdb eadbf cdfaeg
bgefac fecdabg cebdaf de aedfb ade bcefa gcafed bcde adgbf | ebcgaf fbadg ead dea
edbgc dcba egcfbad cfdage abfegd caged ebd bd gdaecb cgbef | cdebg badc edgbc ebcdg
cgfabe dce fcdba efbcg bdcef bagfecd eabgcd ed ecgbdf dgfe | fdcgbae egfd edc bfegc
fgecad gebda ebgcd egc dbecgf ce cbef cgdfb egacdfb agdbcf | edbfgc cdgbe edgfcb bcefgad
geafcd cdage ec cdegbaf gec cgafeb abgcd gebfda gafed dcef | ce gdbfea egdabfc gec
be abdgfc daecgfb bae dbfag gfdeba dbafe gbfeac egdb dfcae | bdge eba ecfgba gcaedbf
bcge gbeadcf acg bfcdga bfcaeg cfaeg gc feabc bfaecd gedfa | bfeac begc fgdbca bgfedac
gfbed ce ced fceabd dabegf bcdgef egcf becfagd gcdbe gcadb | ce ec bdfaceg cdeafgb
gfdea db cfdgbe badc gbaec ebfagc bde dgbcae egbda cbgedfa | badegc cbeagdf bd egfbcad
deabcg egbca agfdbc edagfb abe eb afbdceg efcag becd agcdb | bced eb be bea
ebfgda bgade dbcea geaf gcbadf dgfba eg gcedabf geb cgfbed | edabg ebg cedgfb bgdcfa
fc adgbecf fbcd abgfdc cgfab aedcfg dgcab gfeab cfg cbdgae | fcg badegcf bgdac fgc
edg egbda dafbgec begc aedcb cadgeb eg eacdfg gafbd cebadf | dfgba bdegac cbge afdbec
bgfecad gdbaf afdbeg egafcb db dba fdbace egafb gdeb dcgfa | fcbade fdagbe bad cdbefa
bcgedf ecba bagcef beg cgafed eb gfedcab gfbad efgba egafc | bace fbgdec cbgefa beg
dgfecb fcd edfbag bcfade fc caegd bcgedfa defab caefd cbaf | dbeafcg fc fabed dcf
ceafg fbad cfeba edbca afebdc bf cegbad bcdfge cgdefab feb | gdbecf faebdgc dcaeb ebf
cgdfba cdabf gfedca efgcadb baegfd bcfg fc caebd gdafb caf | cgfb dgcfab befgadc debgcfa
ebfgca abcfe gca bagde bgcae dbgcfa efbadc gcef agefbcd cg | abegc cbfae acefbgd cag
dfegac gedc bcdaef abgfd egf ge fagecb agefd fecdbga ecfda | gbafd dacbef gdefac gef
feb begcdf fcab edcafb bf aefdc gbade bfead daegfc feagcbd | bcfeagd gcdbfe fbceda fb
baefgcd fedcb cfbaed dgc abdgf bdfgc bcedgf gc gcfe aegdbc | ceafgbd fbcdge bgdface efbdgc
fd gbdae bfda ebfgdc edf gfdea caegf aedgcfb fadgbe bcagde | abedg adegb efd fcaedgb
befg gecdb dbcgafe adebcf dge ecfbd bcefdg aecgdf ge bgcda | bgfe gdebc dge ge
cfdgb be aecfg gefdbc bgfec afbdgec cdabfe egbd cgabdf cbe | fbdgc eagcf eb faegc
gfdbae aedcfg gbcfed bcde dbefgac gfacb ceg bdgfe ce efcbg | efcbg ce edcb ebgadf
ba fecba dbcgfae gcab gfcade febdc adfegb fab aegfbc gcfae | adfgbce gebdaf cefba fba
eadbf acfde cfdeab bagfcde bgfcae eab gafdce cedb dbgfa eb | bea fgeacd eb fegabc
ecb baedc ec adbfgc cedf faedcb acdfb fdgaebc agbde aegcbf | fdce cbfdage bdacgf cedf
fegc cefgdb fdbge bfg badgcf eabcdf fg begad dafgceb fedcb | bgf fdbec gf ebdacf
gfecdab gbeac aebd be eacgd ebc dfegca cbagf agbdce bcdgfe | bec ecdga ecb gabec
cb bcfg cgdfa adegfc bca aegdb afcebd gcdfab dbgca becfdag | bc badcgf bca fcbg
gabdce dc fbgdc gbfec cfda fgadcb bafgd fbdeag abdcegf cdg | fcda adbfg cfgbead dgacfbe
ecgab gbdecaf bcfa cdegbf eabgdf fgeabc ba egfcb gedca gba | fcbeg gacbfe ecdbafg deacg
agc bgfca cg cgafdbe dcfg dcfagb fbdag cabef gdbace gdefab | dcfg agbfc bcfea cag
cagb ga dfcga fdaebc fdebga cbdafg gfcbdea fcgde cfbad afg | cbga cedgf cdbfa afdbcge
cafgdeb aebfd gfbeda ebcgdf agcbf egf decbaf egda abgfe ge | bgacf bdfea agfbe beadcf
be agbefdc aedfgc gabfc adgfeb egb efdag afgbe deba bgfedc | fedgacb dcafge bdae ebad
cfbae dcaegf cdbfaeg fcebga efgca cdfab ageb gcbfde ebc be | acfgde be be bcfagde
bafdgce cea gdbcfe ae agecd agcdbe ebfcad agbe bcdeg fgadc | cebgad ecbgd cbdeg gaeb
bgce agefcbd egbcfa eg fadgb bfcae dfbcae egfab gae fdcega | age gafcbe eacfbg gea
fgacde gfaedb ebacf gc gdfc dcbeafg fadge gec bdcgea cgfea | fdagcbe gfdc aefgcbd gc
gb acfdgb bga aedgf bafdg abdfecg gacfeb bgdc bfcad fcedba | fgaecbd dabcf bcdg bg

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?_

69
day-09/day-09.py Normal file
View File

@@ -0,0 +1,69 @@
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
result = 0
m = [[int(d) for d in l.rstrip()] for l in input]
for y, l in enumerate(m):
for x, d in enumerate(l):
lowest = True
if 0 < y and d >= m[y-1][x]:
lowest = False
elif (len(m) - 1) > y and d >= m[y+1][x]:
lowest = False
elif 0 < x and d >= m[y][x-1]:
lowest = False
elif (len(l) - 1) > x and d >= m[y][x+1]:
lowest = False
if lowest:
result += d + 1
print("Part 1 result:", result)
def part_2(input):
result = 0
m = [[int(d) for d in l.rstrip()] for l in input]
p_lowest = set()
p_low = set()
for y, l in enumerate(m):
for x, d in enumerate(l):
low = True
if d != 9:
p_low.add((x, y))
if 0 < y and d >= m[y-1][x]:
low = False
elif (len(m) - 1) > y and d >= m[y+1][x]:
low = False
elif 0 < x and d >= m[y][x-1]:
low = False
elif (len(l) - 1) > x and d >= m[y][x+1]:
low = False
if low:
p_lowest.add((x, y))
b_size = []
neighbors = [[-1, 0], [0, -1], [0, 1], [1, 0]]
for k in p_lowest:
to_check = [k]
basin = set()
while len(to_check):
p = to_check.pop(0)
basin.add(p)
for n in neighbors:
tup = (p[0] + n[0], p[1] + n[1])
if tup in p_low and tup not in basin and tup not in to_check:
to_check.append(tup)
b_size.append(len(basin))
b_size.sort()
result = b_size[-3] * b_size[-2] * b_size[-1]
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-09/input.txt Normal file
View File

@@ -0,0 +1,100 @@
9896513457895219985678901987899932101234895432101989013989656789689019778932123456789543210197689542
8764302969994398764568999986798763213356789543319878929876534245998998568943299767897659329986598921
9873219898989579896789987675987654344587897654929767899987321019876876457894987978998998998964497893
7654398767678998987899876554599875558788998979897656789798462198765464348989876799989876976543236789
9867999654569987699965965432101996679899789998788545989519553259874321245678965678978945989532125899
1979898765678976598754398743212987989976599987643634569329879345993210234789984789167896798673034679
0298769896789965439843139654453498998897989998732123458934989457986541345678999896256789989765123578
1987654987896799123921098765577689656789568997841014567896798969987432345678968965347899879897545689
2988663498965678939872129878788794545795456985432323789987987798798973479899459876498999767998957997
9876532679654567898653234989899893234894349876644435692199886665679654567891345987899987659899868976
5987643498767678987654356799923985456789246987655645789098765434579887679920124598987897644678978985
4398759999898989798765456899019876568890166898876876899987654325789999796431235789696689433567989524
0239898788999996569897567898999987679931245699998988999898985416898769896542345898545568921378995435
1545996567899899478987679997578999989549356987659599999769876527797658998743698987323467892989876557
3466987356789788989998789765489767999698999876544459988953988635689767899984567896516689999899987668
4578975238896567899989898654398755878997789999432398876794599545799898989875778965435999998778998979
6989854345789456999878987543239543569986579998910987645689698756892999765986789879569898784567989899
7899766567991245689769998865398754698765459877899876834898799767994987654598896998998789643469876798
8985977678920756789547899976579767987655368966798965423699989878989999743589945987847679432398765656
9764998989321569897636988987679899997644239854387965434589978989678998954678929865436568943987654535
9879869698943458994324567898989932986532198765456896645678967294569897978789546984323459954599653124
4998954567895767989401346799994321397321019876568987659789653123498656999898659875415667895698784234
3987553458976779876532346789875210986542123987879898767899873234998945789998797998734789986789895345
9876432899897897996545557899654349998763445698989799879998764349876735779999986569645678998998986656
5998543798789976987668769998965498989876569789997678989549879656954324567899875498789789999986598767
4459654597679895498779978987899987678989678999876567899929998969875436789979965319999897899875469898
2349765986578689329989989765698898567898799932997688998797997899996545678967894324989986799854345999
1269899997434567912392199894597767477789999891098899987656786799998656989458789455678975798765456789
2356987989323499324999012989986548345678998789129999876545345678998787895345678966989654899879599991
3479896567215989439898929878996433234567988679299799965434235689769998975467789878996543499998987890
5599765432104878998767899967689610123679876558987698764320146899659329696998999999987794989997676799
6989987543223467897656798754596531235698965437899549989432245988998934599899998765699895678986545678
9876987657335598976545897643479932349987654326788957997544556976767895989768999883799976989995436789
9965398767846679765434987621467893567898975214567898998867867895458969878756678954899897899987545894
9878459898987789986557896532356989798949876723678999459978978954349759865532566895999798999987657893
4989569989899895499878987654569878999433997934678995345989989863212349873201345789878679998999798932
3398798678799986598989398767698767998912986545679689256993299954323498764322456896569569987559899991
1239987547689998987891299878987654987923499657892579099892198765434569875438579976452458976443999979
2545998634567899876789987989699643986894998968921399988793019876547678987547679654321347894321298768
3659875323455678965459876593498901975679877899430989876589923989659789698678789763210156987434398657
5667994210134567895320975432987899864698766788959976545467894598767999459889899975321239896595976545
8779987321245679976459876679996987653598654667998765432356789689879768999995978965442398789989996423
9998765452456789989867987798964599769987543546899895421245778999989356789354569898754497654578989534
3239876564567897999978998987543459898798732135699985320134567899992124593253498799966596543365678945
4396997776678946789999989997632349997698644276789876534234778998963246999012349679997989342234568957
5975498989789235998998979898543498887598756987898987665356789987654357898929998598999876210134578968
6989329999892139897987656789654987654349897898987899985468896898799968956798767456889965431234589878
9898910989951029776899887898775699887212998999765321296567945679987899245999842345679876542345679999
8787899876543197655353999979989789976535679999987432987679234999996799458998943659789987986578989321
7656789998954569943212398767499899987646798987976543698789359879765678967987654578896798997689894310
3843458999765898762103569656321998999756987656799684569895479764334567899099898699965329298999765421
1012367899986798543212398743210197989869876545678965678976598743223479964125979789543210129678986632
4124878989897987659654987654323986678979865434567897789989987652101457893234569898798721234569999543
3234799976789998768765698786549765456989877655689998898799876543212345995686689959987642949698998969
4765678945678999899879899897998654346893989766895439987545988754323456789787789543298859898976546798
5876899766799999934989942998909765757892199989989920985434699876434789999899899432109998767995435697
6987989978943989955994321349919876898989019997678799876546892987945697987998998953212987656789521486
7898967899999976899875520457899987949978998987568689987678921099986796895987997894324598845693210245
9949459999987834894987521245678999234567896796434567899989439989987895796796876789495987656789654356
7431298989996325793295432957789654346698934987513678999996598979898954589985435699989998968898765458
6532987878954314679986649899998786467989423598923899798987987658789865679976323589579999879969976569
7643976667895323689997998677899897678978912499535994567899996545699876798765434579468899997657989978
8759765456789534567899877566789998899767893987676789698968997534469989899876556689356789349869898899
9867976578897645679998765434678959987846789998889898789349876212378998998989667793247890123998776799
5989987678987656889999976323489543976434568999999909891236954334489987766598798920124569434987645689
4399998789999968998976985401568964985423667999989212990545695546598765755469899531234568949875534578
3238999899989899357899876512359879897912356789879323989676987957989854332345995432345679698764323567
4346998999876789234999997675468998789895477899968939878997898768976543210236789543459896569995654678
5557897898765799356789898886579987679789568989959998766789959878988654632447898655569943456986789789
7678916987654678967998789997679977578678979577898989655678943989999766543456998776778932768999899892
8999935698543569898987678998798765456568989456987678934899321299879878956788979897889549879126999921
9789896987432689789987567899899984323499994345698789545689210359765989867999864999997698991014697432
5698769875421345679098778976912395544678943234569898787789322498964599988902963489129987889123976549
9989656995434597892129989765423987655789432123978999998995439987895678999219854678998876778945798698
8979937896665689999234799987634599767898543236899989129896798996789799854398765689987654867956789987
7567899987787798798975689999785679879987654345789878998798987895699898765459986792976543456897999876
2347999998998939667896789999897790989098786467898769898689996434578959978567987891297632365779999765
1356789879549023456987999891999991292169897678987658789579875423469543989678998954398721234567899853
2579899765432139567898998792398789965356998989298745689359989212378932399899109795459810125678999761
3458999879549298979919876599987689899769999890129634590298999102347891235994323679598323234789989842
4667899998798987894329875478976599798998898751398523791987898913456789356789434567987654456998976543
9788998969987976789498754357894398687666789942987434899876787894577899868899945878998787669897987655
9899997756796545689569543268943239546544567899876545689975656997689949878999896789999998798786499876
5932986542987323589979985179994998432133456789987656799864346789789431989998799999887659989695321989
4691987431299212567899876799989897657012567999898767899943235689894320197898698998764349876543210199
3589997620198343459979999899876789432123489998769878919874346795975431246789567789853234998754342999
2378998435297654568967988999985679543434599876543989202965457944986532347992345698742123789868769898
4567899949998765979756567998674789754678967989432198929876578923499693467898456989643345678979899776
9689998797899986899643459889543899869889757898953987999987689439698989568986579878967467789698998645
8789987686789997998794569768956789878998648997899876789998996598987678999298699867898988994567976434
9891096575678999459989998956898898999349899986798765678999987987896566789359987648999499123569884321
4999989434567994369879876346789997891235989765659543568999898986954345678969986534894321012498765432
5698778923456789298767995457898976789399879953549852346789799987893237789998758623799469993569878658
7987667994767890198857987768987635899989769892139761456894689998954345899879543712678998789678989767
9876546789878999987845898979996547999767659789019862867932399899865456798765432101567899656989999898
2987634599989998765435689989987657897655545679198654979653456789996587899876583312488996548899987949
1296545689999869984326796594598768987643435689239776789765579899987679935997765469699689437698965437
0987657899875456976457895412569899996432123596467987899876789998798989321098987878789578926567896545
1298769999764322987768954323456921987541012359578998921989895497659799533129898989893459212378998856
2349896598753210198978967764567890199432124568999109430199943298345678954235679999932368901456789768

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?_

63
day-10/day-10.py Normal file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env python3
from pathlib import Path
o_chars = ['(', '[', '{', '<']
c_chars = [')', ']', '}', '>']
missing_val = [3, 57, 1197, 25137]
corrupted_val = [1, 2, 3, 4]
def part_1(input):
result = 0
wrong_chars = []
for line in input:
line = line.rstrip()
seen = []
for c in line:
if c in o_chars:
seen.append(c)
elif o_chars.index(seen[-1]) != c_chars.index(c):
wrong_chars.append(c)
result += missing_val[c_chars.index(c)]
break
else:
del seen[-1]
print("Part 1 result:", result)
def part_2(input):
result = 0
scores = []
for line in input.copy():
seen = []
for c in line.rstrip():
if c in o_chars:
seen.append(c)
elif o_chars.index(seen[-1]) != c_chars.index(c):
input.remove(line)
break
else:
del seen[-1]
for line in input:
seen = []
for c in line.rstrip():
if c in o_chars:
seen.append(c)
elif o_chars.index(seen[-1]) == c_chars.index(c):
del seen[-1]
score = 0
for c in reversed(seen):
score *= 5
score += corrupted_val[o_chars.index(c)]
scores.append(score)
scores.sort()
result = scores[(len(scores) // 2)]
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)

110
day-10/input.txt Normal file
View File

@@ -0,0 +1,110 @@
([[<[[[[<{((([<>][{}<>])[<<>{}>[{}[]]])((({}())({}{}))<<()<>>([]<>)]))<<[[[][]][{}[]]]>[([(){}]((){})){<<>
[(<[<<<{([(<{(<>())}(({}[])<<><>>)>)]){{[<[[<><>]{{}<>}]>[[<()[]>((){})]]]}[<({<<>{}>{[]<>}}(({}())[<><>]))
<<(<<[<{(({[({{}<>})<({}<>)<()[]>>]})<[(<{[]}<{}{}>>)(<{{}()}[<>[]]>)](<<<[]><(){}>>>{{{<>[]}([
([[{[{<{{(([<([]{})[[]{}]>][<({}())[{}()]>{<[]{}><{}[]>}]))<([(({}<>))])>}}>}<<[<{[<<({}[]]([]{})>>]}><{
<({[<({(({{[(<[]()>{{}{}})][<[{}{}]{()<>}>[<()()>]]}}){((<<{[]<>}>(({}<>){[]<>}>>(<[<>[]]{<>}><{[][]}[<><>]>
[(<({([{{{{{([(){}][<><>]){[(){}]{{}<>}}}((<{}{}>(()[]))[<{}()><[]()>])}{<({{}()}<<>{}])[{{}{}}{[]<>}
({<<{{{{(((<({<>()})({{}}{{}{}})>{{([]{})<<>{}>}{<[]{}>{()[]}}}){[<[<>[]}[[][]]>{[{}[]]<<>()>}]<{({}<>)<{}
[({{<<[{<({[<<{}{}>([][])><{(){}}>]{[[[]<>]][(<>()){{}}]}})(<[<{{}()}<<>[]>>([{}]{()<>})]>)>}[(<[({[<>[]]([]
({[([(<([[<(({[]{}}{[]{}})<{()()}{<>()>>)[<{{}<>}>({[][]}(<>[]))]>]{(<{{[]{}}[[][]]}[[()[]][
{([<{<{([<[[[[{}()]]]{<{[]()}((){})><[[]]<<>>>}][<<<{}<>>{()<>}>{<{}()>[{}[]]}>[(([]<>))[<[]{}>[[]]]]]>]<[
<{[{{([[(<<<{({}{})(()<>)}[<{}()><{}<>>]>>>(<{{[{}()][(){}]}([[]()]([]<>))}{{<{}[]>{{}[]}}{[[]{}]{()
{<{[<([{([[{{(<>[])[<>[]]}}][(<{[]()}<{}{}>>{<<>()>((){})])[{[<><>](()<>)}<<{}{}><<>[]>>]]])}])
<[[[{<[<([[({[<>[]][()<>]}(({}[]){[]<>}))]])[([<<([])><<()[]>{<>[]}>><([()()]<[]()>){<<>[]><[]<>>}>]({<({}[]
((<{{{[(({[[([()[]]([][]))(<[]<>>([]()))](((()())([]<>)){(()[])([]<>)})][{<([]{})<[][]>>{<[]{}>{(){}}
(({{<[[{((((<<{}()>[()<>]>){[(()<>)([]())]})<{[<{}()>[[]()]]}<((<>())({}{}))[({}{}){{}<>}]>>))<[(({<
[<{[{{({<[{[(<[]{}>{(){}})({{}<>}{{}{}})]<([[][]][[][]])((<><>)<<>()))>}<({{{}()}{{}{}}}(<[]{}>{()
{{{{[{[{{<[[[{()<>}(<>[])]<{{}[]}[[][]]>]<<<<>>>>]{[[{()<>}<[]{}>]{{{}{})[{}[]]}]<(([]<>){{}()})>}>}[<([[({}
([<<(({(<{{{<{()<>}>}}{(<({}<>){[]{}}>[<<><>><()()>])}}(<(({<>()}<(){}>){(<>{})[<>()]})>[(({<>[]}
<{([[<{(((<(<[{}{}][(){}]>{<{}<>><[]<>>})<((<>())<()[]>)(<{}()>[[]<>])>>[(<[[]{}][[]()]>[<()()>[{}<>]]
<[{{(<{[<(<[{[[][]]<()<>>)<(()<>)[<>()]>][<{{}{}}(<>[])>(<()[]>(()()))]>)<{<[{[]{}}]>[{<[]<>><<>{}>}]}[<{<
[{((({({[<[<[(<>()){()[]}]([<>{}]{<>{}})>{[[[]<>][[][]]]{(()())[<>[]]}}]{{{{<>[]}{{}()}][([]
<[{[<{(<((<{<{[]{}}(()<>)><(()<>){(){}}>}[{[{}[]]}<(()())[()()]>]>{{[<()[]>]{(<>[])<()()>}
{<([<([{{{(({{()[]}}[([][]){(){}}]))}[[{{(<><>)<<>>}<<()()>>}][{[[[]<>]{(){}}]}<{{[]<>}<{}[]>}{{<>[]}<
<<[<{{[<[<<([[{}<>]{[][]}][[{}{}](()<>)])[([{}]({}{}))[([]<>)<<>[]>]])<[<[()<>]>{[[]{}]<{}{
[{[{<[({<((<[<<><>>(()<>)](<{}[]><<>()>)>[{([]())[()<>]}])([<([][])<()<>>>([<><>])]))[[{({<>[]}[()[]])}[[{(){
{[([[<{[<{<{[(<>[])<{}()>](((){})([]<>))}[({<>[]}[()[]])([[]<>][{}{}])]><{{<()()>[()]}(<[]<>>[{}<>])
{[<([(({([<[([()[]]<[]{}>){<{}<>>(()<>>}]([(()<>)([]())]((<><>)({}())))>]){[{(<[[]{}][(){}]>({[]{}
[({<[{[<({<((<<><>>){[{}()]})<<{{}{}}[[]()]>(({}()))>>{[[{{}<>}({}<>)][(()<>)({}{})]]<{[()<>
[([<{<{<{[(<[((){})[<>[]]]{{{}<>}[{}[]]}><[[()()][()()]]>)([[{[][]}{[]()}]]({<[]<>>(()<>)}[[[]<>]({}<>)]))
<([<{[<<[<(<{{(){}}({}{})}[<<>()>(<>())]>[<<[][]>([]())>{[<><>]<{}()>}])>]{{[{[<{}{}>(<>{})][[<>()]<<>[]
<{([<<(<{{[{<[(){}]{[]}>((<>[]){()[]})}(<{<>}(()())>(<<>[]><<><>>))]<[<<[][]>([])>]>}}{[<<(<[]>{<>[]])(<()<>>
(<{([<{[(<<(<[(){}]({}())><{()[]}<[]()>>){[<<><>>[{}[]]]<({}<>)[[]<>]>}>{[[([]{})<{}[]>]][{<[]
<<({[<({<{[((({}))<(<>[])>)<[[<><>]([]())]>]}<{[<{(){}}(()())><<[]<>>{()[]}>]}>>[<{{{<()()>[[]()]}(<
[{[{<{[[{{{[<[(){}]{<><>}>(({}[]))]}<(<[[]<>]{<><>}>{(()){(){}}}>[({()()}<{}[]>)]>}}]{<((<{<(){}><{}<>>}
{([((<<([{<[[([][])<{}[]>][[(){}]{[]<>}]](<{[]{}}<<><>>>)>[{{{<>{}}([]{})}[<[]()>(()())]}{<(<
<{{((<{{({{<(<(){}>({}<>))[[{}[]]{[]{}}]>{[{(){}}{()[]}]({<>[]}<[][]>)}}([(<(){}>[[]<>])[[{}{}]<{}<>>]
[<[<({[<{{{{{<<>[]>}({<>()})}}([[(()<>)<()()>]]>}([(((<>{}))[[<>[]][()[]]])<{([]{})(()[])}>]<{
<<[<{<{({<([({{}[]}){{{}()}{{}{}}}]<[{()()}<<><>>]{(<>())(()())}>)(<{({}<>)([]{})}(<<>{}><[]()>)>{
([{({{<<<{{{((()[])(<>[])){(()[])[()<>]}}(((<>{})<()()>))}<<[{<>{}}{{}[]}]<(<><>){<>[]}>>(<{<>[]}<(){}>>)>
{[[(<({{[<<([<()[]>[(){}])<<[][]>[<><>]>)({({}())([]{})}[{{}()}({})])><<{{[][]}[()[]]}{{{}()}{<>[]}}
<(<[{<<{[{<({{()<>}[{}[]]}[<{}>({}<>)])<[{(){}}(<><>))>>[<{<[]{}>[{}()]}<[()]<<><>>>><{<{}{}>[[]<>]}[<()[]>(
<([{{{([<(<(<[()()]{[]()}>{[()()]{{}{}}}){[[[]<>]{[]{}}]<<()<>>>}>)[[{(({}<>)<<><>>)[[()()]{[]()}]}(<[(){}]{<
{[<{{<(<{<<[<(<>())({}())>[[()]{[]()}]]({{[]()}}<<[][]>{<>{}}>)>>(([[[{}[]]((){})]([[]{}]{
<<<<(<{{<((<[<()()>{<>[]}]>)[[<({}())<(){}>>]{(<(){}><<>()>)[{<>[]}[{}<>]]}])(([<<<>{}>>[{[][]}{{}{}}]]{<[
([[<<<({[[(<([<><>]{<>()})>(([<>][{}<>])<[[][]>([]{})>))({{<[]>{<>()}}{<[][]>[[]{}]}}<[{[]{}}{{}{}}
{[[<<(<<([(([{<>()}<<>()>]{[{}[]]{()[]}}){[[(){}){(){}}]{<<>[]>{[]<>}}})[<([[]]<<>{}>)>{{{<>{}}<<><>>}[[
(<{{<([[[([[(<()()>[[]<>]){[{}]{[][]}>]])[<[{(<>)(()[])}({<>[]}[{}()])]{<[<>{}]{{}()}>{{()}{<>{}}}}>([
{<[{(((([<[{{<<>()>[()()]}<([]())<[]()>>}<((<>))[<[]{}>{[]<>}]>){{([()]<<>[]>)[<[][]>{()}]}[{((){})
((<{<{<(((<(({(){}}([][])){((){})<[]>})({(()[])(()())}{{()<>}[<>{}]})>[(<[()]<[]{}>>(([][]){<><>
{{{({[{(<<<<[[(){}]<<><>>]([[]{}]<{}[]>)>>>>[{{<[{<><>})[(<>[])[{}{}]]>{{<<><>>}({<><>}{()()})
{{[{[(({[(({<[{}{}]>[{[]<>}[{}()]]}(((()<>)<()[]>))){{{<{}[]>{{}<>}}{{{}<>}}}<<<{}{}>>(({}()))>})({<<{
{<{<[<(<{<[(<({}{})[[]{}]>{([]<>)[<>[]]})>([<{<>()}{[][]}><{()[]}<[][]>>][<{{}{}}{[]()}>[<<><>>[{}<>]]]
{({{[<(<[[<[[({}{}){<><>}]](({()<>}{{}<>}){({}<>)<()[]>})>]}><<<{{(({}{})<()()>)([{}[]][<>[]])}
(<<{<<{{[{((({[]{}}))<([<>()]{{}{}})(<{}{}><[]<>>)>)[{({{}{}}<()>)}([<{}[]>([]())]{<<>[]}([]{})})]}<
{{<{([[<[(<[[<{}<>>]({[][]}<<>{}>)][[[()<>][<>[]]]{[[][]]}]>(({([])[<><>]}[(()<>)<(){}>])<<[{}]{<
<{{{[<{[<<[<{[<><>]{[]{}}}[<{}()>{{}[]}]><<<[][]>{{}()}><(<>())[<>[]]>>]<[[<()[]>[<>[]]](({}<>))]>>]]}>]{([<(
[<{({{[{<<{<<[()<>]<<>()>>([<>[]]([]()))><[<<>{}>{{}{}}]>}(<{[()()][()()]}(([]{}))>)>([(({{}()}<[]{}
(<{{({({({<<((()<>)(()()))[(<>[]){<>[]}]>({({}[])[<>{}]})>})}){[[({{(<()<>>{<><>})<{<>[]}<{
[[<<<{<<[<[[{<{}{}>(<>{})}]{([[][]][[]{}])<([]<>)<[]{}>>]]>[[<[[()[]]({}())](([]())[()<>])>
{[(<[[{({<{<[{(){}}]{[[][]]<()[]>}>}}[([<[{}[]]([][])><<[]()>[()()]>]({<()<>>(<>[])}<<(){}><<><>>>)
[{(<<{{[<{{{[[(){}]({}())]<(<>[])>}<{{()<>}(<><>)}>}}[(((([]())[(){}])(({}<>)<()()>))[<(<><>)<[]{}>>
<<<{<([[[({{<([]{})[{}()]>[([]<>)[[]()]]}(<<<><>>>)}[[{<()[]>({}<>)>]([[<><>]][(())[[]()]]
([{{([<<<{{(<[{}[]][{}()]><[<>()]{[]()}>)({{[]<>}[{}()]>({()<>}<{}()>))}[<<<{}<>>{[]()}>>]}>>>
{[<{{<{{<[[<({()[]}{<>{}}){{<>[]}{(){}}}>]]<([[[[]{}]{{}{}}][<{}()><[]{})]])({((<><>)[()[]])<[[]]<{}<>>>}[
[[{(<<{<{<{<((<><>)[<><>])>}>({<(<[]()>[(){}>)>{{[[][]]}{{()()}{()()}}}})}{[{{((()())((){}))(({}
[{[<{[((<<[(<<<>[]>({}[])>(<<>>{<>[]})){{({}())[{}<>]}{{<><>}{<><>}}}][[{({}<>)}({<>()}[(){}])]
<<([[{([{{<<<<{}()>({}())>([()()])>(<([][])><<()[]>>)>}}{<{[<[<>{}][<><>]>]<[(()())[()<>]]<{{}{}}{[]<
{{[<{{[{{{<{<<<>[]><()<>>>(<{}<>><{}{}>)}<[((){})[[]{}]]{<{}>}>>}<[[([{}[]]{[]<>})][([()<>]<{}
[(([(((<{[{{<[[][]](<>{})><{{}[]}[(){}]>}{(({}[])([][]))<[{}[]][[]]>}}([[{[]}(()[])][([]<>){()
{[<{<<[<{([<({[]<>}[()]){[[]()]([]())}>{([<>]{<>{}})}])<(([({}[]){[]<>}]{([]<>)[[]<>]}){{<(){}}[{}[]]}([[]{
[{<<<[<[{(<{({{}{}}[[]()])([[]()])}>)[[(({<>}({}())){{<>[]}<[]<>>})[(<<>{}>)]]]}{<[[[[{}()}(<>())]{<<><>
((([{[<({([[((<><>)[{}()])(({}<>)({}()))]{[<<>{}><<>()>]({()()}[<>])}]<[{[{}()]}([{}{}]<()()>)]>)}<([<[{[]
(<[<{[<[{[[<[<{}()>[[]]]>{{<<>>({}[])}<{{}<>}>}][({<()()>[()[]]>({[]()}))]]}]({<({<[{}()]{{}()}>{[()<
(<<([{<[<(<[[{[][]}[(){}]]]}<{{<{}{}>}{(()[])(()())}}>)>][[{[([([][])(<>())](<()>[[]()]))(<{[]()}{()
[{{<<<<{(({{({{}[]}<()[]>)({{}()}{<>()})}<[(<>())]{(<><>)}>}((([<>()]{{}[]})[<<>[]>(()[])])[(<{
<[[{<([[<[{(({<>[]}({}()))(<[]<>>{<><>}))<[(<>[])(()<>)]{{(){}}{{}<>}}>}([[{()()}(()())]]((<[][]>([]()))(<
(<(<[{<([[({<<(){}>[{}()]>[<[]>[()()]]}(<<{}{}><[]<>>>({{}[]})))<[({[]()}[{}<>])<(()<>){[][]}>]{([
{(<(((<[<<<[[(()())[{}]]({<>()}({}()))]<<{()()}(()())>[{<>[]}[[]<>]]>>>([{(<{}{}>{{}})<[{}[]](<><>)>)[<[<>
<[<([<{<<<{{{<()[]>{<>[]}}[<[][]>[{}()]]}}<{{{[]<>}<[]{}>}<{{}()}[()()]>}>>)>[([<{((<>{})[()(
<<[<<{([[[[<[([][])((){})][{{}()}({})]>([{{}{}}(<>[])])]{{(<<>[]>{()[]}){[<>()]{[][]}}}[{{<>}<{}<>>}<<{}()>((
{{(<{[<<[{[(<<[]>(<><>)>[{[]()}{<>{}}))<{({}[])<[]{}>}{(<>{})[[][]]}>]<(([<>{}]([][]))<[[][]][<>
(({([{({<[<<{([]())(<>())}([<>{}])><({<>})<<<><>>>>>[<{[{}<>]{[][]}}<{()()}<[]{}>>>[{<()<>>{{}}}
<{[{({[(({[((<()>{()<>}))<([()[]]<<>()>)[{(){}}([])]>](({<()[]><{}<>>}({{}}([][]))))}))<<[[[[{(
[{<([[({{{{({([][])[<>{}]})[{({}{}){<>{}}}]}({[{[]{}}([])]({()()}<[]{}>)}<[[{}{}][{}[]]]{<(){}>{<><
(<[{{{{[{(((<[[]())([]())>{<()()><[][]>})){<(<<>{}>[[]()]){{<>{}}[<><>]}>[<{<>()}<{}{}>>{<()[]
({{[{[[[{<{([<{}<>>[{}()]])(({()[]}{()[]}){<{}<>>{()}})}<<{[<><>]}<[()[]>{{}<>}>><((()[]))>>>
{<[(<{[(([({<[<>[]]<<><>>}[[<>()]{{}<>}]}[{<{}>}<[()<>]<[]()>>])(<[[<>()]{{}()}]<({}[])[{}()]>>([[<>{}]<<
{<[<{{(([{[[{<[][]>[<>[]]}<[{}<>]<<>{}>>]]{<(([]<>))((<>{})[<>{}])>{(<{}<>>[<>])([(){}]{<><>})}}}[{[({{}<>
<<({[(<{<[{{[<{}[]>]<[<>{}]{[]<>}>}}<([{[]<>}]{(<>{})}){(<[]<>><()>)[<()()>([]{})]}>](([{{[][]}[{}[]]}[{<>[
[<[[[[<({([[[[<>()]{{}()}][{<>{}}{[]}]]<{<{}>{<>)}>]{[<[<><>]{()()}><((){})(()())>]{(({}[])){[()()](
{<[[[<<<<<{[<[<><>]<()<>>>{{()[]}[{}()]}]<<[<><>]{()()}>>}({{{<>()}([]<>)}[[(){}][()[]]]})>>>>>]
(<[{{[[{({[{<{<>[]}[<>[]]>}]})}{{({{{([][])((){})}(({}[]))}([{<><>}(()[])]({<>{}}<{}>))}{([((
<[[(<{({<<{{[{<><>}]<(<>{})<<>()>>}[<([]<>)(<>[])>[<{}<>><[]<>>]]}<({{{}<>}[()<>]}{[<>[]][[]
{{<[[<<<{{{(<[{}()]>[<(){}}(()[])])[[{(){}}<{}()>][({}[])([]{})]]}}[<<<[()()]{[][]}><<[]>>>{<<{}[]>{<>[]}>[<
((<<{([[([[<([[]{}]<[]{}>)>]](<({{{}{}}([][])}{(<>())[<><>]})[((()())[{}{}])[(<>{})[[]<>]]]>))[{{({{{}{}}[<
<<<(<(<[[<(<[{()[]}<{}{}>]>([{[]()}[()[]]]<(<>())>))[{({<>{}><{}<>>)}{[[{}()](())]<<()[]><<><>>>}]>{{<(
{{{<(({{[(({{<[]()><<>>}{[()[]]{<>{}}}})({<<()]<[]{}>>}([[()()]{[]<>}]))){<(<(<>()){[]<>}>[
[({([{<(<[<{[<<>[]>[[]]]{[{}[]]<(){}>}}{<<[]()>({}())>([[]{}][[]()])]>]<<{{[{}{}][[]{}]}{{(){}
<{{<[[([[(<{{{<>[]}[()<>]}(<[]{}>)}<{[{}{}]({}{})}>><{[{{}[]}(<>{})]<{<><>}<{}{}>>}>)][<{({[(){})<()()>})
<<{{[{{[[{{[({{}[]}[()[]])({[]<>}{{}()})](<<<>[]>{[]<>}>{{()[]}[<>()]})}}](<{{{<<><>><{}[]>}}}({
[<[<<(<[((((<(<>[]}<[]>><{[][]}({}{})>))[<{[[][]]}([{}<>][{}])>[<{[]()}({}{})>{(()()){{}{}
[<<{[([[([{<[{(){}}[{}{}]]><{[[]<>][(){}]}<{[]{}}[()()]>>}({((<>))<{()}<[]{}>>}{[[[]]{<><>}]<[[]<>]
[[[{<([{[<[<{[<>()]<()<>>}<{<>()}<[]<>>>>([(<>{})(<>[])]<<(){}><[]<>>>)]({{[<>{}]}({<>[]}<()[]>)}[(<{}<
{<<[[{([{((({[{}()]{()[]}}[<{}>[{}{}]]))({[<(){}><()[]>]}))}])[<(<([([<>{}))<[[][]]{(){}}>])(
[{(<<{<[[{<[{[{}[]](()<>)}[[{}{}][[]<>]]]><(<(<>[])({}())>[{{}{}}<<>()>>)>}<{(<<{}[]>[[]()]>{<
<{(((([<{[{[<<(){}>[()<>]>{<{}>({}())}]{[{<>()]({}{})]}}][(<<{()()}>({[][]}<{}[]>)>)]}[(<[{(
([<((<<[([[[<[()()]<{}()>>]<(<()()>[[]])[<{}<>>((){})]>}{({((){}){[]{}}}[[[]]<{}<>>])(<{()[]}[()[
{((<[({<<({(<{<>[]}{[][]}>){<<()()>{[][]}><{[]{}}>}}<<[<[]>(<>())]<<<>{}>{{}()}>>[[[<>{}]<()()}]<
[[<{{{({([((<[()<>]<{}>><(()[])[{}()]>)[({<><>}({}))<<{}<>><[]{}>>]){[([{}<>](()()))]<[{()
{<<[[{[[[[[[[[<>()][{}]]<(<>())(()[])>]]<{[<[]<>>({}{})]}<[({}())][{{}()}{{}[]}]>>]<[[(<()()>{[]{}})

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?_

80
day-11/day-11.py Normal file
View File

@@ -0,0 +1,80 @@
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
result = 0
m = {}
for y, line in enumerate(input):
for x, d in enumerate(line.rstrip()):
m[(x, y)] = int(d)
for _ in range(100):
pending_flash = set()
flashed = set()
for k in m:
m[k] += 1
if m[k] > 9:
pending_flash.add(k)
while len(pending_flash):
k = pending_flash.pop()
flashed.add(k)
result += 1
min_x = max(k[0] - 1, 0)
max_x = min(k[0] + 2, 10)
min_y = max(k[1] - 1, 0)
max_y = min(k[1] + 2, 10)
for y in range(min_y, max_y):
for x in range(min_x, max_x):
m[(x, y)] += 1
if m[(x, y)] > 9 and (x, y) not in pending_flash and (x, y) not in flashed:
pending_flash.add((x, y))
for k in flashed:
m[k] = 0
# for y in range(10):
# for x in range(10):
# print(m[(x, y)], end='')
# print()
# print()
print("Part 1 result:", result)
def part_2(input):
result = 0
m = {}
for y, line in enumerate(input):
for x, d in enumerate(line.rstrip()):
m[(x, y)] = int(d)
s = 0
while not result:
s += 1
pending_flash = set()
flashed = set()
for k in m:
m[k] += 1
if m[k] > 9:
pending_flash.add(k)
while len(pending_flash):
k = pending_flash.pop()
flashed.add(k)
min_x = max(k[0] - 1, 0)
max_x = min(k[0] + 2, 10)
min_y = max(k[1] - 1, 0)
max_y = min(k[1] + 2, 10)
for y in range(min_y, max_y):
for x in range(min_x, max_x):
m[(x, y)] += 1
if m[(x, y)] > 9 and (x, y) not in pending_flash and (x, y) not in flashed:
pending_flash.add((x, y))
for k in flashed:
m[k] = 0
if len(flashed) == 100:
result = s
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)

10
day-11/input.txt Normal file
View File

@@ -0,0 +1,10 @@
2682551651
3223134263
5848471412
7438334862
8731321573
6415233574
5564726843
6683456445
8582346112
4617588236

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?_

69
day-12/day-12.py Normal file
View File

@@ -0,0 +1,69 @@
#!/usr/bin/env python3
from pathlib import Path
def process_node(node, edges, visited):
num_paths = 0
if node == "end":
return 1
if node.islower():
visited.add(node)
neighbors = [n for e in edges if node in e for n in e if n != node]
for n in neighbors:
if n not in visited:
num_paths += process_node(n, edges, visited)
if node in visited:
visited.remove(node)
return num_paths
def process_node_twice(node, edges, visited, visited_twice):
num_paths = 0
if node == "end":
return 1
if node.islower():
if node in visited and visited_twice:
return num_paths
elif node in visited and node != "start":
visited_twice = node
else:
visited.add(node)
neighbors = [n for e in edges if node in e for n in e if n != node]
for n in neighbors:
if n not in visited or visited_twice != node and n != "start":
num_paths += process_node_twice(n, edges, visited, visited_twice)
if visited_twice == node:
visited_twice = None
elif node in visited:
visited.remove(node)
return num_paths
def part_1(input):
result = 0
edges = set()
for line in input:
n_start, n_end = line.strip().split('-')
e = (n_start, n_end)
edges.add(e)
result = process_node("start", edges, set())
print("Part 1 result:", result)
def part_2(input):
result = 0
edges = set()
for line in input:
n_start, n_end = line.strip().split('-')
e = (n_start, n_end)
edges.add(e)
result = process_node_twice("start", edges, set(), None)
print("Part 2 result:", result)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)

24
day-12/input.txt Normal file
View File

@@ -0,0 +1,24 @@
pf-pk
ZQ-iz
iz-NY
ZQ-end
pf-gx
pk-ZQ
ZQ-dc
NY-start
NY-pf
NY-gx
ag-ZQ
pf-start
start-gx
BN-ag
iz-pf
ag-FD
pk-NY
gx-pk
end-BN
ag-pf
iz-pk
pk-ag
iz-end
iz-BN

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

67
day-13/day-13.py Normal file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/env python3
from pathlib import Path
def fold(axis, fold_at, dots):
new_dots = set()
for (x, y) in dots:
if axis == 'x':
new_x = fold_at - abs(x - fold_at)
if new_x != fold_at:
new_dots.add((new_x, y))
elif axis == 'y':
new_y = fold_at - abs(y - fold_at)
if new_y != fold_at:
new_dots.add((x, new_y))
return new_dots
def part_1(input):
result = 0
dots = set()
instructions = []
for line in input:
if ',' in line:
x, y = line.strip().split(',')
dots.add((int(x), int(y)))
elif 'fold along' in line:
d, c = line.strip().split()[-1].split('=')
instructions.append((d, int(c)))
axis, fold_at = instructions.pop(0)
dots = fold(axis, fold_at, dots)
result = len(dots)
print("Part 1 result:", result)
def part_2(input):
result = 0
dots = set()
instructions = []
for line in input:
if ',' in line:
x, y = line.strip().split(',')
dots.add((int(x), int(y)))
elif 'fold along' in line:
d, c = line.strip().split()[-1].split('=')
instructions.append((d, int(c)))
while len(instructions):
axis, fold_at = instructions.pop(0)
dots = fold(axis, fold_at, dots)
max_x = max([x for x, _ in dots])
max_y = max([y for _, y in dots])
print("Part 2 result:")
for y in range(max_y + 1):
for x in range(max_x + 1):
if (x, y) in dots:
print('', end='')
else:
print('', end='')
print()
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)

971
day-13/input.txt Normal file
View File

@@ -0,0 +1,971 @@
218,593
1176,617
57,515
806,674
517,740
402,553
1118,53
596,577
473,147
166,817
149,154
209,228
170,159
124,376
960,481
107,73
646,313
708,393
1215,696
706,780
969,149
686,189
552,771
523,322
1058,789
155,205
616,782
113,154
1017,280
15,352
774,623
649,633
435,591
353,638
273,327
1139,180
763,821
360,506
219,432
656,770
499,124
1081,691
1079,68
1012,204
1146,665
669,421
192,558
92,873
626,477
1250,435
65,99
790,36
1208,795
1290,634
344,229
1171,43
179,154
75,316
398,788
725,80
793,694
1146,217
179,740
848,841
972,100
1010,313
495,399
410,4
442,315
1222,75
458,768
10,100
683,537
994,570
771,91
1240,330
417,201
1280,828
462,725
795,204
833,697
1206,357
164,217
438,312
161,621
1154,497
910,49
236,704
428,350
338,100
1215,870
589,219
956,245
1299,752
1139,409
736,750
70,564
1266,847
20,219
1056,78
688,488
326,376
445,666
1086,829
1155,205
715,138
484,455
1010,649
226,544
460,710
1032,616
811,385
756,126
644,655
518,693
97,451
671,800
624,705
336,303
468,294
356,652
651,495
869,546
174,642
1068,705
708,365
566,733
950,164
266,843
435,562
391,873
391,294
75,764
38,112
388,771
149,201
375,635
972,794
343,542
1299,142
1103,525
192,753
1063,441
989,709
8,329
316,794
524,96
666,794
822,705
256,442
708,281
338,207
31,273
994,239
1203,73
170,567
231,68
1028,259
1138,102
1079,480
944,289
714,129
935,635
977,261
443,54
723,508
376,651
1032,894
845,807
237,621
775,530
393,123
1006,413
465,87
579,578
22,600
580,341
284,23
328,128
244,627
10,794
433,597
684,240
875,751
994,155
137,304
984,742
441,702
1079,414
776,142
656,670
1163,819
321,633
524,315
65,219
15,94
1196,782
1250,883
136,649
977,709
709,887
338,879
1144,77
801,210
1196,392
1057,241
391,425
602,393
643,590
669,429
1039,544
433,297
1198,281
408,638
1220,190
910,221
254,845
430,829
790,277
758,381
758,323
323,467
500,291
520,858
951,808
114,782
186,600
391,469
1176,57
894,628
1176,277
179,787
8,113
795,690
290,707
10,465
1170,93
301,591
982,766
1120,649
813,633
1174,649
887,869
806,884
361,301
383,350
336,79
236,515
473,528
333,521
1258,689
1203,521
875,487
498,371
300,313
244,267
972,330
770,782
644,476
976,887
641,17
149,714
208,355
902,638
887,826
1302,815
518,439
803,292
932,429
124,638
997,730
459,98
1243,609
505,180
623,796
539,257
694,179
1056,721
641,473
416,628
1284,537
1280,66
172,792
402,789
1252,544
1174,439
11,752
1158,291
1240,620
723,386
244,4
892,621
58,765
818,236
1017,301
753,651
1148,0
1235,764
527,816
115,637
999,787
1006,481
500,739
137,416
1000,229
997,25
313,25
508,229
1156,326
639,516
672,411
328,376
930,134
508,341
226,350
1161,180
458,763
900,420
825,37
728,704
1084,544
792,649
808,575
872,312
113,516
48,488
831,640
298,92
23,379
381,663
674,315
402,105
417,649
663,462
500,313
304,824
514,861
810,774
669,249
107,704
1092,593
969,297
492,336
147,819
792,693
1278,276
107,821
997,205
1285,396
728,67
704,894
1140,735
1121,292
423,205
102,430
408,644
194,184
294,612
810,333
582,827
1158,603
1262,488
874,719
728,827
318,143
492,457
336,43
940,871
515,466
293,525
1115,86
611,364
509,646
418,379
462,753
987,287
639,110
982,152
464,693
1086,571
917,123
1089,297
1121,87
238,628
370,870
1180,350
169,814
694,715
411,621
826,775
134,617
641,249
887,733
874,271
410,474
796,481
1300,465
21,399
606,198
837,147
16,411
975,14
694,826
638,483
475,107
1000,236
622,406
1031,707
279,707
666,655
837,528
763,143
418,256
957,254
864,427
668,296
1141,485
318,781
1124,801
207,369
1103,369
805,714
333,709
514,353
1289,399
820,889
1294,859
504,674
974,751
509,658
1196,868
1193,676
893,693
323,159
508,105
1196,726
962,186
957,638
1176,53
1037,215
1074,190
908,553
818,620
806,436
1297,197
671,107
152,603
147,75
930,760
917,347
682,357
13,136
956,649
1094,666
927,544
1203,780
1049,323
589,99
90,190
279,315
80,108
278,224
343,339
872,393
812,371
304,413
423,752
536,623
632,152
1277,299
1156,120
808,689
1047,152
380,312
1102,243
1280,466
786,756
606,616
1121,864
1171,690
1176,858
102,571
463,705
805,421
88,523
507,75
808,700
1208,717
1260,653
966,859
649,261
408,698
1208,8
276,465
623,124
48,539
186,93
852,518
1066,267
1039,246
688,618
341,205
731,578
624,369
716,11
509,864
925,735
313,369
1123,242
1154,103
237,273
793,154
458,824
1156,581
835,107
1280,156
703,539
310,236
1210,495
966,229
192,165
738,26
192,865
801,236
694,292
776,590
1115,98
134,841
328,152
1020,707
306,708
1300,239
1302,49
402,509
400,49
154,781
1294,411
202,627
801,478
137,694
840,555
801,248
252,105
354,581
666,239
48,691
1091,407
894,852
1170,801
624,189
770,840
666,877
934,24
504,884
194,812
502,351
1186,289
22,152
117,666
1210,47
669,17
917,508
428,768
540,782
48,0
812,343
1118,841
300,649
509,248
492,218
1020,187
869,254
20,260
1010,581
802,105
1051,485
1282,169
140,413
1146,453
238,42
540,502
842,801
1002,126
107,521
3,847
316,564
557,451
1002,19
783,78
929,663
462,296
1170,481
892,414
1146,441
622,40
736,556
1156,113
944,605
418,862
351,291
540,616
1154,819
149,869
1180,586
17,80
997,369
509,684
795,466
972,739
48,355
418,173
452,70
848,764
669,473
596,544
1148,894
32,58
567,81
169,528
274,582
156,551
1144,817
848,729
114,726
360,282
366,605
1054,452
117,676
970,212
554,126
758,464
190,439
316,15
294,724
760,357
1231,49
107,780
465,200
370,471
393,508
1056,49
934,870
336,781
1081,891
13,249
1238,221
682,693
114,294
1133,19
602,582
31,546
616,68
624,817
478,10
735,49
490,889
1295,110
637,366
1295,784
162,218
790,150
520,501
425,651
416,852
1279,796
867,840
162,889
803,75
477,19
443,502
55,544
410,627
410,715
880,795
186,189
79,397
1066,362
549,297
1124,413
815,831
391,742
616,715
247,441
935,259
974,113
1097,546
518,201
335,14
266,11
418,36
269,603
846,455
867,54
863,138
52,666
490,376
899,441
1034,465
1108,376
840,107
468,600
186,801
949,301
256,620
267,84
818,218
507,448
328,770
1081,443
1151,831
458,394
266,65
130,579
557,203
190,201
934,651
1196,698
316,155
171,572
535,530
753,443
801,684
932,465
1255,544
632,742
294,164
820,5
28,110
788,514
320,833
1017,614
1044,65
1310,222
1293,80
656,124
316,17
293,593
212,894
892,379
641,429
566,371
957,256
687,124
344,658
304,600
300,245
328,681
994,291
721,219
276,632
908,789
714,544
1191,532
929,231
676,675
852,70
927,350
882,544
244,420
1136,709
1009,143
964,126
912,106
1180,544
1210,847
1176,281
1102,355
1200,228
552,878
788,626
8,565
967,339
44,399
927,96
792,245
22,294
477,697
1118,878
974,815
316,330
136,21
468,93
1297,136
835,292
507,292
679,515
208,651
398,106
1252,129
960,152
587,386
502,82
445,228
1072,42
1051,857
647,462
919,21
114,698
8,49
698,442
242,705
694,68
994,330
0,661
1174,887
498,523
815,63
530,518
1161,197
1192,411
192,617
972,463
1196,294
55,256
134,822
146,665
426,514
639,94
547,73
1144,189
1146,336
196,192
636,875
758,8
1215,472
408,704
520,764
833,427
972,291
1200,540
629,807
900,250
190,649
802,341
954,802
622,488
1223,81
1215,422
231,689
177,19
1251,756
90,173
311,784
0,670
974,781
1049,508
149,473
923,362
273,231
646,329
505,421
1064,480
293,369
239,701
952,290
642,598
48,616
1154,551
736,338
589,795
115,803
1238,133
994,794
981,565
987,875
1268,136
418,858
1044,323
808,319
152,767
793,620
919,425
790,501
1043,362
110,540
616,292
1054,620
520,277
599,798
1052,836
836,217
659,47
887,434
417,693
1108,32
393,771
42,136
1091,880
146,546
48,406
666,17
331,399
338,291
336,403
1064,644
164,441
359,546
628,357
616,112
154,774
171,322
385,735
843,652
28,784
1173,304
930,312
1073,273
899,273
0,672
636,539
266,571
992,591
709,551
192,29
775,362
485,37
313,730
994,15
651,47
970,682
254,78
1064,414
552,886
313,205
858,267
130,138
687,796
112,281
1310,770
107,114
1300,794
644,346
1208,571
793,416
673,366
832,884
1074,484
962,708
1206,537
1136,642
552,323
721,99
359,348
676,708
154,313
1148,218
499,385
30,66
678,600
1028,635
502,689
932,17
1305,709
1076,508
435,143
1191,362
708,529
1041,603
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6

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

29
day-14/day-14.py Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env python3
from pathlib import Path
from collections import Counter
def solve(input, part, num_cycles):
result = 0
template = input[0].strip()
rules = {k: v
for line in input[2:] for k, v in [line.strip().split(' -> ')]}
counts = Counter([template[i: i + 2] for i in range(len(template) - 1)])
char_counts = Counter(template)
for _ in range(num_cycles):
for k, c in counts.copy().items():
if k in rules:
counts[k] -= c
counts[k[0] + rules[k]] += c
counts[rules[k] + k[1]] += c
char_counts[rules[k]] += c
result = char_counts.most_common(1)[0][1] - char_counts.most_common()[-1][1]
print("Part", part, "result:", result)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
solve(input, 1, 10)
solve(input, 2, 40)

102
day-14/input.txt Normal file
View File

@@ -0,0 +1,102 @@
NCOPHKVONVPNSKSHBNPF
ON -> C
CK -> H
HC -> B
NP -> S
NH -> H
CB -> C
BB -> H
BC -> H
NN -> C
OH -> B
SF -> V
PB -> H
CP -> P
BN -> O
NB -> B
KB -> P
PV -> F
SH -> V
KP -> S
OF -> K
BS -> V
PF -> O
BK -> S
FB -> B
SV -> B
BH -> V
VK -> N
CS -> V
FV -> F
HS -> C
KK -> O
SP -> N
FK -> B
CF -> C
HP -> F
BF -> O
KC -> C
VP -> O
BP -> P
FF -> V
NO -> C
HK -> C
HV -> B
PK -> P
OV -> F
VN -> H
PC -> K
SB -> H
VO -> V
BV -> K
NC -> H
OB -> S
SN -> B
HF -> P
VF -> B
HN -> H
KS -> S
SC -> S
CV -> B
NS -> P
KO -> V
FS -> O
PH -> K
BO -> C
FH -> B
CO -> O
FO -> F
VV -> N
CH -> V
NK -> N
PO -> K
OK -> K
PP -> O
OC -> P
FC -> N
VH -> S
PN -> C
VB -> C
VS -> P
HO -> F
OP -> S
HB -> N
CC -> K
KN -> S
SK -> C
OS -> N
KH -> B
FP -> S
NF -> S
CN -> S
KF -> C
SS -> C
SO -> S
NV -> O
FN -> B
PS -> S
HH -> C
VC -> S
OO -> C
KV -> P

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

107
day-15/day-15.py Normal file
View File

@@ -0,0 +1,107 @@
#!/usr/bin/env python3
from pathlib import Path
from heapq import heappush, heappop
def part_1(input):
"""The solution for Part 1 is an implementation of dijkstra algorithm. The input data is stored
in a dictionary 'nodes' where the keys are a tuple of the coordinates and the value is the cost
(risk level) to travel to this node.
The total cost for each node will be stored in a dictionary named 'cost'. We start with a queue,
that will contain all the nodes we have to check. Initially there is only the starting node
known. The queue also contains the cost for each node to be able to prioritize them in an heapq.
The 'done' set is used to remember the already processed nodes.
In each iteration we take the node with lowest cost known so far from the queue and set it as
the active node. If this node is already stored in 'done' we can skip it and start the next
iteration immediately. Nodes can be pushed to the queue multiple times but we only need to
process them once, hence we always use the node with the lowest cost from the queue.
If the node hasn't been processed yet we check all of its four neighbors if they have been
processed. If not we calculate the total cost for this node by adding the risk level to the cost
of the active node. The total cost will then be compared to the current known cost for this new
node. If it is lower or not yet existing the cost will be updated in the 'cost' dictionary and
the new node will be pushed to the queue. At this point we could also push the active node to a
dictionary containing the previous node for each node to be able to construct the path. But
since only the cost is asked we don't store this information.
Finally the active node will be pushed to the 'done' set and start the next iteration if there
are elements in the queue.
"""
result = 0
x_size = len(input[0].strip())
y_size = len(input)
nodes = {(x, y): int(v) for y, line in enumerate(input)
for x, v in enumerate(line.strip())}
start = (0, 0)
end = (x_size - 1, y_size - 1)
cost = {start: 0}
queue = [(0, start)]
done = set()
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
while len(queue):
cur_cost, current = heappop(queue)
if current in done:
continue
(xc, yc) = current
next = set()
for (xn, yn) in neighbors:
nbr = (xc + xn, yc + yn)
if nbr in nodes and not nbr in done:
next.add(nbr)
for n in next:
next_cost = cur_cost + nodes[n]
if not n in cost or next_cost < cost[n]:
cost[n] = next_cost
heappush(queue, (next_cost, n))
done.add(current)
result = cost[end]
print("Part 1 result:", result)
def part_2(input):
"""The part 2 solution is essentially the same as the solution for part 1. The only difference
is that the grid now repeats itself five times to the the right and downwards. The grid itself
is only stored once and the new boundaries and costs for new nodes are calculated on the fly
while the iteration across the nodes happen.
"""
result = 0
x_size = len(input[0].strip())
y_size = len(input)
repeat = 5
nodes = {(x, y): int(v) for y, line in enumerate(input)
for x, v in enumerate(line.strip())}
start = (0, 0)
end = ((x_size * repeat) - 1, (y_size * repeat) - 1)
cost = {start: 0}
queue = [(0, start)]
done = set()
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
while len(queue):
cur_cost, current = heappop(queue)
if current in done:
continue
(xc, yc) = current
next = set()
for (xn, yn) in neighbors:
x = xc + xn
y = yc + yn
if 0 <= x < (x_size * repeat) and 0 <= y < (y_size * repeat):
if not (x, y) in done:
next.add((x, y))
for (x, y) in next:
next_node_cost = nodes[(x % x_size, y % y_size)] + \
(x // x_size) + (y // y_size)
if next_node_cost > 9:
next_node_cost -= 9
if not (x, y) in cost or (cur_cost + next_node_cost) < cost[(x, y)]:
cost[(x, y)] = cur_cost + next_node_cost
heappush(queue, (cur_cost + next_node_cost, (x, y)))
done.add(current)
result = cost[end]
print("Part 2 result:", result)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)

100
day-15/input.txt Normal file
View File

@@ -0,0 +1,100 @@
4249856395422795894919869133487611581179923326874763428673979547991221931142777981153991369468629849
5812974178739823463799939791688998895568796557798392761499941349143539572865883254186633218867928826
3699989976298596286299499129934993241824395574879938998946914116375199242199151918863674914554714898
5682435936794718871685718386458294198391116125679589438794914499278679393779734596558953699438589518
7681197997388219696918569664119968498599547892968929425479817979816979144947916716989874825679487436
9981166198272997899142698141878643123757515999788822988261499197559193945291512682763935126815448215
8849481991861599951293183728419792414164347979985169641698899853377259811688489269959429131918919179
3146684963669199195628973847379928251333566129941616877139631993381755512697185555441659879412994594
3547126819874919985836685298322994247998729919239243539198191229787198622919819681997288193865811343
9349351519955698988787949741387799819991699489867797184914918979814262987979129782223984139929928681
8556723973779719572644889927179188961591428979299692841637999459259999966917347419979917999989799429
3999593212698784221858498479516759888388689669747143292191959989949179914898181617598363288863529673
5428799869888883819715648841341382669887652713779547744299157935892529712964887994849791591489974817
4165839572399389794126943784626997119389378141921327523621283992924955951342126919499984987328936781
5918974268979888998966997691965798988519797198467567586265175937858739845974399727152913189899879868
9918963885878999715999979758376917418219222841971748992311279797791196671188181557777929939979528569
8799414739499371349792499832971671794937933561999619313535199597778778121557962319178399272824346194
8129974849671628977826913868589128781917962416787192199363828395129743811293383873718935942898484543
3291978181898819892998112489871391737197991911931592663875989291645713999792932894185783865998135615
9769786394195196445418935989967399989919987914119824796623857976492949168247992292316817898188699298
9695549579996919793895372134169684298121551587731119812488691243992189122596899959128989294926989913
9731949899392599399916551967299296355733976428888499681995484999191298894166112999923884965685649798
1379132844766698587968789875949841895175974769991975485998386847244993897999111588556816596847214788
9995799248928516994491995979579982981536188627577229466616615274772189816183363386951765979225171476
8119319184142479165692338899796498149599699117488698945689444844778989694876998388519355148181719697
3969928779117129139766611778112739189981889899228317887833728779584723699986312572133539799467285917
2828571791973431936329179952568811618879743616915382919867751789598712817617446745199253856716911941
7877882851638951395858496388153888963667598177681792531864174354491939941593119389999293999633917577
1741348787717975984768578798922563489939392171468619989248936559712179995116534277937799235981547597
8994795857858489999558911549196942878698886391795462739582595987979187765281397182149953478248998829
8612529258832189244164522719918668148697754189678792826892293447659399729274986899475955657297984999
7979418978468149867435687251779984869732288683757891423519948382875198942891979899776225467769271611
3818925865589486991983914829611968896197719999391296298299521911997968172695633992575991389714563593
9939187896519664586181929999697892324492792718919161519713861732615919297932929685698963679515326392
9189845839678777775364697497289839263197942629799278637458319429822229529989343196991899576448897282
9999639983298888593994869989699624152112558163996649649924665617939567974367965998835369235385845781
8485297496991559289188422661599882518896282381858287969539917799964977429172882227811329528518187389
9923859471976698253169673595135694645862699839964421741799449165978835991198979991891434697214818847
9948337779794835971121969995653787569496217989663848398739231628499247383942859555527489595679787897
9614621899899158259613995889586798538683918139138198111781714698794348571485366962113966939699643589
1886264968913257167733217995794996991471499861875887994593877487186697874771821988536581746639569567
9996998152929759225939499689998449399761985591467834853824558991322485728674388927966892986878819319
9898162778195791515947756898474977179296647585999979756996981928598799139892651743929891711498489794
2521299845948789489447298917175981779999984794295846999894374299918878915896995183978239599211199661
3595238712479964219979978796399416285395878319927814389893184733214219892965998974989825195778431936
2235456515733797949374988899892819815718692811711999741978953215792979895857589889485595198499933267
1197979995691548749958849994592898117171521989543481689999898771166569678234761678582598848227891693
6216285194657981924762962276999868669495239816738592141587889187963869774297156591399718859932485599
4729519687582299378618117855237999797646997991843389649799259193169319238188911375949387726963679678
9699419763993869239747599293979686119998329461424981594878199858799991786943919119924945895985515374
5917996721981481869846954978898329942115828969756258675677679711875328179436699932532229987171169929
7229157819969968259887444794288923399898267871298685198291217856595548347212555983111245182519289622
9941195813129393544872257827938956788461327756591766941724841646918922553881862784891489959867389928
8479286666256699729946931949971218817964289363968557999375893759943919891576159694877195239993489148
5189737855542763417865693998141987771991218843271999857384919975699659994988779213888396912127939755
5829249497186735715873851983779438136179792549877615899657346513899789927819449917872492988915513982
6893461933919197988858287721313611812883946878115312372236989924698848988818298419696991695398125941
6981819519985951199181939989475291951792185935616749128365399541588872686782591994295847959591392931
7988629179489397799986811211539191291919253927863868887948841837793918643181962412676745991232121578
5889916119249763577671931536771981885869515736589522848999798153991769949974999631378632167199887678
9893498949812711525997272863949911589719196319429992823161587263989111998263931884594518988198746153
6167489848782461731786755137947748897385171725523188729519395821564894154311747791864617427287926885
1284879221869544119521638879871365523816995929979939483888749839719816469311148686844482791986117755
4617558896994997356988295927288939978311852269226941898983267192735959576936969989579628535528783519
5328888897937299985799899978919859582992981919818841999199494771499878669927759686618478946895944119
6799938769339968858662394789696395769769575687756577828218986197652972952814799386756962996181892597
9396911993551199899798629969782451326725698246878288812846813423295351841296547341338696917479175299
8998397898488795919859127962748999922875998559197478977986192197254779199892998169999599176629148878
1698547699914798869767429873945711593683922969939799919274119396933311939635974962783917832779918579
8773297884249519189999647199932138888596519893944669261146627181688998288918962987398761891458879884
5661719599479729158986561758999787569799247991581291493981747289118938848919798944839816176679395936
9819441791329173937924877935925271972592729118224733234158273767899274195899119793821988798684414696
1976289898791262299218117985854188291999988166561946723974852936794841371689495873895293179648555216
9723987778468774982999879135869177783838973792981148981488979249865929898395999487197993391716979759
1177899997181866344793594917459353978963997881815188848687916378799676376889949425869917561752649758
9791868299666371298719945725279668482196145817963941526858953389928439799189979599987117288975594221
9359479864494253699797539169919312739992899782984267865929872973278316996551116989646898578889798559
2891458316689974817533795738749516416557382959989231868945595215936673184172899784699156791788186319
3197871588419391799499768999958793419827795378639897913112989895282789729517678369821971988591168789
3128895611274175995969789557139191499997198995898179681198268479688712943414819511197717969357964626
8894829141789688146813983655599262175354512492811217287197518971727198974788999775881793393394664199
6989187141894459991973787674231495975898898321231239587159329169888699482823813357919299199727591361
5291418199993963928598629179855771924331332887583129988192175911928891699934755817621873135693191816
9859979117149771714936925976542992919833772828551725982321627568959639165547556486133816386958767691
7859228193989921397351284929914697967294416985799189145628697417412211943669121428893746928296129853
9898681974164499972995857666974959138563814594959416796689799455999579812615141516926348999849199192
3718388972799194318442969794235896693965351819955961988597649825892827718431384296684438269296581369
6622814526191793264982158196814969278991591173839699792937584466991496599918911981477179793167467286
9979379866996693519416698914822874331128813255572798589585921254251617789813794989743747235329888595
1797898211694565884562549271126691989581781692389727679449696138934914999198975329972191479272399949
8799526921898877188579896888615687919221936817161219171496896799791784217996429736635848912899357959
3998179987191983936537841259615889646999196799579578519955782169872772733999838687699675879992385199
4629983158488678918339598918897184289885199997966199238946856787429929894529981431432825472899789379
2648419335213961853594831348897955886326266994947879975892927267713419578719291698994997922172724295
4791456616384992968619169989914284879576442418663919488293715173289682167949877799818988158798898697
8231746913997992473892883523293984991882222989389687618931693321291896977659253682566976938919319772
8791786476374176864813358771936196161691499499725886199548984199739722733758439858858114991899439768
7864649397847188893939876129989155438478439599981116718616269385899323813687798117158931299417619263
1516286662577862992677589421596899929673339521868597254888585791795752635967517418681822599565577716
6187189898988162537924787542999557639266889281629557986986981493311772812199877994518195163178939239

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

109
day-16/day-16.py Normal file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env python3
from pathlib import Path
from math import prod
def get_next_packet(msg, idx, packets, res=0):
"""The function is called recursively and will read a single packet starting at index 'idx' in
'msg'. The packet will be appended to the 'packets' list. For each packet the type_id and a
payload is stored. The payload is a literal for packets of type 4 or will be filled with
subsequent packages for other types by recursion of this function. The version will be added to
the result.
At the end the updated index and result will be returned.
"""
if (idx+11) >= len(msg):
idx = -1
return idx, res
version = int(msg[idx:idx + 3], 2)
idx += 3
res += version
type_id = int(msg[idx:idx + 3], 2)
idx += 3
payload = None
match type_id:
case 4:
end = False
literal = 0
while not end:
if not int(msg[idx:idx + 1], 2):
end = True
idx += 1
literal = (literal << 4) | int(msg[idx:idx + 4], 2)
idx += 4
payload = literal
case _:
length_id = int(msg[idx:idx + 1], 2)
idx += 1
length = 0
payload = []
if length_id:
length = int(msg[idx:idx + 11], 2)
idx += 11
for _ in range(length):
idx, res = get_next_packet(msg, idx, payload, res)
else:
length = int(msg[idx:idx + 15], 2)
idx += 15
next_idx = idx + length
while idx < next_idx:
idx, res = get_next_packet(msg, idx, payload, res)
packets.append({'type_id': type_id, 'payload': payload})
return idx, res
def calc_result(packets):
"""The function recursively steps through the packets and performs the defined calculations."""
res = 0
for p in packets:
match p['type_id']:
case 4:
res = p['payload']
case _:
sub_packets = []
for i in range(len(p['payload'])):
sub_packets.append(calc_result([p['payload'][i]]))
match p['type_id']:
case 0:
res = sum(sub_packets)
case 1:
res = prod(sub_packets)
case 2:
res = min(sub_packets)
case 3:
res = max(sub_packets)
case 5:
res = 1 if sub_packets[0] > sub_packets[1] else 0
case 6:
res = 1 if sub_packets[0] < sub_packets[1] else 0
case 7:
res = 1 if sub_packets[0] == sub_packets[1] else 0
return res
def solve(input):
"""Part 1 and part 2 are solved in one go so we don't have to parse the bitstream twice. We
start by stripping newline and zeros at the end of the input line, convert it to an integer
with base 16 (hexadecimal) and back to a binary string. The bin() function adds '0b' at the
beginning so we only store te actual binary data. Afterwards we need to add zeros in the
beginning until the binary string has a length of four times the hexadecimal representation
(Each hex digit is four binary digits).
"""
result_p1 = 0
line = input[0].rstrip().strip('0')
msg = bin(int(line, 16))[2:]
while len(msg) < len(line) * 4:
msg = '0' + msg
packets = []
next = 0
while -1 != next:
next, result_p1 = get_next_packet(msg, next, packets, result_p1)
result_p2 = calc_result(packets)
print("Part 1 result:", result_p1)
print("Part 2 result:", result_p2)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
solve(input)

1
day-16/input.txt Normal file
View File

@@ -0,0 +1 @@
E058F79802FA00A4C1C496E5C738D860094BDF5F3ED004277DD87BB36C8EA800BDC3891D4AFA212012B64FE21801AB80021712E3CC771006A3E47B8811E4C01900043A1D41686E200DC4B8DB06C001098411C22B30085B2D6B743A6277CF719B28C9EA11AEABB6D200C9E6C6F801F493C7FE13278FFC26467C869BC802839E489C19934D935C984B88460085002F931F7D978740668A8C0139279C00D40401E8D1082318002111CE0F460500BE462F3350CD20AF339A7BB4599DA7B755B9E6B6007D25E87F3D2977543F00016A2DCB029009193D6842A754015CCAF652D6609D2F1EE27B28200C0A4B1DFCC9AC0109F82C4FC17880485E00D4C0010F8D110E118803F0DA1845A932B82E200D41E94AD7977699FED38C0169DD53B986BEE7E00A49A2CE554A73D5A6ED2F64B4804419508B00584019877142180803715224C613009E795E58FA45EA7C04C012D004E7E3FE64C27E3FE64C24FA5D331CFB024E0064DEEB49D0CC401A2004363AC6C8344008641B8351B08010882917E3D1801D2C7CA0124AE32DD3DDE86CF52BBFAAC2420099AC01496269FD65FA583A5A9ECD781A20094CE10A73F5F4EB450200D326D270021A9F8A349F7F897E85A4020CF802F238AEAA8D22D1397BF27A97FD220898600C4926CBAFCD1180087738FD353ECB7FDE94A6FBCAA0C3794875708032D8A1A0084AE378B994AE378B9A8007CD370A6F36C17C9BFCAEF18A73B2028C0A004CBC7D695773FAF1006E52539D2CFD800D24B577E1398C259802D3D23AB00540010A8611260D0002130D23645D3004A6791F22D802931FA4E46B31FA4E4686004A8014805AE0801AC050C38010600580109EC03CC200DD40031F100B166005200898A00690061860072801CE007B001573B5493004248EA553E462EC401A64EE2F6C7E23740094C952AFF031401A95A7192475CACF5E3F988E29627600E724DBA14CBE710C2C4E72302C91D12B0063F2BBFFC6A586A763B89C4DC9A0

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

27
run_all.py Executable file
View File

@@ -0,0 +1,27 @@
#!/usr/bin/python3
from os import listdir
from os.path import isdir, isfile
from subprocess import Popen, PIPE, CalledProcessError
from time import time
total_run_time = 0
for dir in [x for x in sorted(listdir('.')) if isdir(x)]:
file = dir + '/' + dir + '.py'
input = dir + '/input.txt'
if isfile(file) and isfile(input):
print('--------------------------------')
print(dir, ':', sep='')
start_time = time()
with Popen(["python3", file], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for b in p.stdout:
print(b, end='')
end_time = time()
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
run_time = end_time-start_time
total_run_time += run_time
print(f'Runtime: {run_time:.3f} s')
print('--------------------------------')
print(f'Total runtime: {total_run_time:.3f} s')

View File

@@ -1,4 +1,5 @@
#!/usr/bin/python3
#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
result = 0
@@ -11,12 +12,12 @@ def part_2(input):
result = 0
for line in input:
pass
print("Part 1 result:", result)
print("Part 2 result:", result)
input = list()
with open('input.txt') as fp:
input = fp.readlines()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)