8000 Clean up 2022 · mjpieters/adventofcode@35f606b · GitHub
[go: up one dir, main page]

Skip to content

Commit 35f606b

Browse files
committed
Clean up 2022
- Reformatted using ruff - Warnings addressed No outputs have changed (but matplotlib plots are a little larger, timeit results are faster)
1 parent ea21bd6 commit 35f606b

25 files changed

+30392
-455
lines changed

2022/Day 01.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"source": [
77
"# Calorie Counting\n",
88
"\n",
9-
"* https://adventofcode.com/2022/day/1\n",
9+
"- https://adventofcode.com/2022/day/1\n",
1010
"\n",
11-
"Welcome back for another round of Advent of Code :-) As always, the first day is just a warm-up. We need to read the AoC puzzle data, split it into blocks per Elf, convert the lines to integers, sum them and find the highest value. Basic stuff."
11+
"Welcome back for another round of Advent of Code :-) As always, the first day is just a warm-up. We need to read the AoC puzzle data, split it into blocks per Elf, convert the lines to integers, sum them and find the highest value. Basic stuff.\n"
1212
]
1313
},
1414
{
@@ -50,7 +50,7 @@
5050
"\n",
5151
"Part two asks for the sum of the top three calorie sums. You could sum and sort, but that'd be inefficient, even if only in theory. You don't need to know the exacty ordering of all the calorie sums, only which 3 are bigger than all other calorie sums!\n",
5252
"\n",
53-
"What you want to use instead is a [_heap queue_](https://en.wikipedia.org/wiki/Binary_heap) to track the top 3 sums. The Python standard library includes the [`heapq` module](https://docs.python.org/3/library/heapq.html) that lets us do just that. In fact, picking the top N items from a sequence is common enough that the module has a dedicated function for it, [`heapq.nlargest()`](https://docs.python.org/3/library/heapq.html#heapq.nlargest), so we use that here."
53+
"What you want to use instead is a [_heap queue_](https://en.wikipedia.org/wiki/Binary_heap) to track the top 3 sums. The Python standard library includes the [`heapq` module](https://docs.python.org/3/library/heapq.html) that lets us do just that. In fact, picking the top N items from a sequence is common enough that the module has a dedicated function for it, [`heapq.nlargest()`](https://docs.python.org/3/library/heapq.html#heapq.nlargest), so we use that here.\n"
5454
]
5555
},
5656
{
@@ -89,7 +89,7 @@
8989
"name": "python",
9090
"nbconvert_exporter": "python",
9191
"pygments_lexer": "ipython3",
92-
"version": "3.11.0"
92+
"version": "3.12.0"
9393
},
9494
"orig_nbformat": 4,
9595
"vscode": {

2022/Day 02.ipynb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
"source": [
77
"# Playing Rock, Paper, Scissors\n",
88
"\n",
9-
"* https://adventofcode.com/2022/day/2\n",
9+
"- https://adventofcode.com/2022/day/2\n",
1010
"\n",
1111
"I like to model things like Rock, Paper and Scissors as [Python enums](https://docs.python.org/3/library/enum.html); you can give each enum member more than just a simple value and so I encoded in them their strategy guide letter (`A`, `B`, `C`), their scores, and what they defeat. A second enum models the outcome of a game; here the values are the score for winning, losing or pulling a draw.\n",
1212
"\n",
13-
"I strongly suspect that the interpretation of `X`, `Y` and `Z` is going to change in part 2, so I kept that as a separate mapping, `responses`."
13+
"I strongly suspect that the interpretation of `X`, `Y` and `Z` is going to change in part 2, so I kept that as a separate mapping, `responses`.\n"
1414
]
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 2,
18+
"execution_count": 1,
1919
"metadata": {},
2020
"outputs": [],
2121
"source": [
@@ -84,7 +84,7 @@
8484
},
8585
{
8686
"cell_type": "code",
87-
"execution_count": 3,
87+
"execution_count": 2,
8888
"metadata": {},
8989
"outputs": [
9090
{
@@ -98,7 +98,6 @@
9898
"source": [
9999
"import aocd\n",
100100
"\n",
101-
"\n",
102101
"strategy_guide: list[tuple[str, str]] = [\n",
103102
" tuple(line.split()) for line in aocd.get_data(day=2, year=2022).splitlines()\n",
104103
"]\n",
@@ -116,7 +115,7 @@
116115
"source": [
117116
"## Part 2\n",
118117
"\n",
119-
"And indeed, as expected, the interpretation for the `X`, `Y` and `Z` letters was more complex, but because I already had a mapping, it was trivial to extend that with a second level mapping the letters `A`, `B` and `C` to the correct choices for each (e.g. if you have to win, map each option to the winning letter)."
118+
"And indeed, as expected, the interpretation for the `X`, `Y` and `Z` letters was more complex, but because I already had a mapping, it was trivial to extend that with a second level mapping the letters `A`, `B` and `C` to the correct choices for each (e.g. if you have to win, map each option to the winning letter).\n"
120119
]
121120
},
122121
{
@@ -177,7 +176,7 @@
177176
"name": "python",
178177
"nbconvert_exporter": "python",
179178
"pygments_lexer": "ipython3",
180-
"version": "3.11.0"
179+
"version": "3.12.0"
181180
},
182181
"orig_nbformat": 4,
183182
"vscode": {

2022/Day 03.ipynb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"source": [
77
"# The set of all misplaced items\n",
88
"\n",
9-
"* https://adventofcode.com/2022/day/3\n",
9+
"- https://adventofcode.com/2022/day/3\n",
1010
"\n",
11-
"We are asked to find the single common element between two compartments in a rucksack. This is what _sets_ are really great for, the intersection of the two sets of letters."
11+
"We are asked to find the single common element between two compartments in a rucksack. This is what _sets_ are really great for, the intersection of the two sets of letters.\n"
1212
]
1313
},
1414
{
@@ -19,7 +19,6 @@
1919
"source": [
2020
"import string\n",
2121
"\n",
22-
"\n",
2322
"priorities = {letter: i for i, letter in enumerate(string.ascii_letters, 1)}\n",
2423
"\n",
2524
"\n",
@@ -58,7 +57,6 @@
5857
"source": [
5958
"import aocd\n",
6059
"\n",
61-
"\n",
6260
"rucksacks = aocd.get_data(day=3, year=2022).splitlines()\n",
6361
"print(\"Part 1:\", sum(priorities[misplaced_item(r)] for r in rucksacks))"
6462
]
@@ -69,7 +67,7 @@
6967
"source": [
7068
"## Part 2, intersecting more than 2\n",
7169
"\n",
72-
"The problem hasn't really changed, we now have to intersect 3 rucksacks to find the single item. Since the [`set().intersection()` method](https://docs.python.org/3/library/stdtypes.html#frozenset.intersection) can take multiple inputs, this is trivial to achieve."
70+
"The problem hasn't really changed, we now have to intersect 3 rucksacks to find the single item. Since the [`set().intersection()` method](https://docs.python.org/3/library/stdtypes.html#frozenset.intersection) can take multiple inputs, this is trivial to achieve.\n"
7371
]
7472
},
7573
{
@@ -129,7 +127,7 @@
129127
"name": "python",
130128
"nbconvert_exporter": "python",
131129
"pygments_lexer": "ipython3",
132-
"version": "3.11.0"
130+
"version": "3.12.0"
133131
},
134132
"orig_nbformat": 4,
135133
"vscode": {

2022/Day 04.ipynb

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@
66
"source": [
77
"# Contain your intervals\n",
88
"\n",
9-
"* https://adventofcode.com/2022/day/4\n",
9+
"- https://adventofcode.com/2022/day/4\n",
1010
"\n",
11-
"Today's puzzle is about finding the overlap between [intervals](https://en.wikipedia.org/wiki/Interval_(mathematics)). In theory there are (at least) 5 different kinds of overlap between two intervals $a$ and $b$:\n",
11+
"Today's puzzle is about finding the overlap between [intervals](<https://en.wikipedia.org/wiki/Interval_(mathematics)>). In theory there are (at least) 5 different kinds of overlap between two intervals $a$ and $b$:\n",
1212
"\n",
1313
"1. $a$ lies _below_ $b$; the end value for $a$ is smaller than the start value for $b$:\n",
14-
" ```\n",
15-
" |---A---|\n",
16-
" |----B----|\n",
17-
" ```\n",
14+
" ```\n",
15+
" |---A---|\n",
16+
" |----B----|\n",
17+
" ```\n",
1818
"2. $a$ _overlaps below_ with $b$, where the end value falls between the start and end values of $b$, but its start is still lower than the of start of $b$:\n",
19-
" ```\n",
20-
" |---A---|\n",
21-
" |----B----|\n",
22-
" ```\n",
19+
" ```\n",
20+
" |---A---|\n",
21+
" |----B----|\n",
22+
" ```\n",
2323
"3. $a$ is _contained within_ $b$, so both the start and end of $a$ lie between the start and end of $b$:\n",
24-
" ```\n",
25-
" |---A---|\n",
26-
" |----B----|\n",
27-
" ```\n",
28-
" You could further classify this case as $a$ being smaller and wholly contained within $b$, being the same as $b$, or $a$ lying over $b$ (so $b$ is contained within $a$).\n",
24+
" ```\n",
25+
" |---A---|\n",
26+
" |----B----|\n",
27+
" ```\n",
28+
" You could further classify this case as $a$ being smaller and wholly contained within $b$, being the same as $b$, or $a$ lying over $b$ (so $b$ is contained within $a$).\n",
2929
"4. $a$ _overlaps above_ with $b$, where the start value falls between the start and end values of $b$, but its end is greater than the end of $b$:\n",
30-
" ```\n",
31-
" |---A---|\n",
32-
" |----B----|\n",
33-
" ```\n",
34-
"5. $a$ lies _above_ $b$; the start value of $a$ is greater than the end value of $b$:\n",
35-
" ```\n",
36-
" |---A---|\n",
30+
" ```\n",
31+
" |---A---|\n",
3732
" |----B----|\n",
38-
" ```\n",
33+
" ```\n",
34+
"5. $a$ lies _above_ $b$; the start value of $a$ is greater than the end value of $b$:\n",
35+
" ```\n",
36+
" |---A---|\n",
37+
" |----B----|\n",
38+
" ```\n",
3939
"\n",
40-
"For part one, we only need to worry about the 3rd case, with either interval being contained within the other. In my `Assignment` implentation, I overloaded `__contains__` to let you test if the _other_ assignment is contained within _this_ assignment, and then just test if `a1 in a2 or a2 in a1` is true to catch either case."
40+
"For part one, we only need to worry about the 3rd case, with either interval being contained within the other. In my `Assignment` implentation, I overloaded `__contains__` to let you test if the _other_ assignment is contained within _this_ assignment, and then just test if `a1 in a2 or a2 in a1` is true to catch either case.\n"
4141
]
4242
},
4343
{
@@ -112,7 +112,6 @@
112112
"source": [
113113
"import aocd\n",
114114
"\n",
115-
"\n",
116115
"assignments = [\n",
117116
" Assignment.pair_from_line(line)\n",
118117
" for line in aocd.get_data(day=4, year=2022).splitlines()\n",
@@ -126,14 +125,14 @@
126125
"source": [
127126
"## Part 2, a case of intersecting intervals\n",
128127
"\n",
129-
"For the second part, we now need to look for cases 2, 3 and 4, *indiscriminately*. Or, inversely, check that the either interval doesn't lie below or above the other interval, which is easier to check for: just test if the end of one is lower than the start of the other, or that the start of one is greater than the end of the other, and invert the result.\n",
128+
"For the second part, we now need to look for cases 2, 3 and 4, _indiscriminately_. Or, inversely, check that the either interval doesn't lie below or above the other interval, which is easier to check for: just test if the end of one is lower than the start of the other, or that the start of one is great 10000 er than the end of the other, and invert the result.\n",
130129
"\n",
131-
"I added a `__and__` method to the `Assignment` class that does just that."
130+
"I added a `__and__` method to the `Assignment` class that does just that.\n"
132131
]
133132
},
134133
{
135134
"cell_type": "code",
136-
"execution_count": 5,
135+
"execution_count": 3,
137136
"metadata": {},
138137
"outputs": [],
139138
"source": [
@@ -174,7 +173,7 @@
174173
"name": "python",
175174
"nbconvert_exporter": "python",
176175
"pygments_lexer": "ipython3",
177-
"version": "3.11.0"
176+
"version": "3.12.0"
178177
},
179178
"orig_nbformat": 4,
180179
"vscode": {

2022/Day 05.ipynb

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
"source": [
88
"# Stacking about\n",
99
"\n",
10-
"* https://adventofcode.com/2022/day/5\n",
10+
"- https://adventofcode.com/2022/day/5\n",
1111
"\n",
12-
"Today we are moving things between stacks, so naturally, this can be implemented using [stacks](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).\n",
12+
"Today we are moving things between stacks, so naturally, this can be implemented using [stacks](<https://en.wikipedia.org/wiki/Stack_(abstract_data_type)>).\n",
1313
"\n",
14-
"Most of the code went into parsing the puzzle input and being able to output it again. :-) Parsing the input involved reversing the input lines (so the bottoms of the stacks are in the first line, skipping the stack numbers), taking every 4th character starting at the 2nd column (these are all the letters on the crates, or spaces when past the top-most crate), and then [transposing the rows to columns using `zip()`](https://stackoverflow.com/questions/6473679/transpose-list-of-lists). Viola, lists of each of the input stack sequence of letters, from bottom-most to top-most crate."
14+
"Most of the code went into parsing the puzzle input and being able to output it again. :-) Parsing the input involved reversing the input lines (so the bottoms of the stacks are in the first line, skipping the stack numbers), taking every 4th character starting at the 2nd column (these are all the letters on the crates, or spaces when past the top-most crate), and then [transposing the rows to columns using `zip()`](https://stackoverflow.com/questions/6473679/transpose-list-of-lists). Viola, lists of each of the input stack sequence of letters, from bottom-most to top-most crate.\n"
1515
]
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 1,
19+
"execution_count": 2,
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
@@ -100,17 +100,17 @@
100100
" stacks.move_crates(move.source, move.target)\n",
101101
"\n",
102102
"\n",
103-
"example: str = \"\"\"\\\n",
104-
" [D] \n",
105-
"[N] [C] \n",
106-
"[Z] [M] [P]\n",
107-
" 1 2 3 \n",
108-
"\n",
109-
"move 1 from 2 to 1\n",
110-
"move 3 from 1 to 3\n",
111-
"move 2 from 2 to 1\n",
112-
"move 1 from 1 to 2\n",
113-
"\"\"\"\n",
103+
"example: str = (\n",
104+
" \" [D] \\n\"\n",
105+
" \"[N] [C] \\n\"\n",
106+
" \"[Z] [M] [P]\\n\"\n",
107+
" \" 1 2 3 \\n\"\n",
108+
" \"\\n\"\n",
109+
" \"move 1 from 2 to 1\\n\"\n",
110+
" \"move 3 from 1 to 3\\n\"\n",
111+
" \"move 2 from 2 to 1\\n\"\n",
112+
" \"move 1 from 1 to 2\\n\"\n",
113+
")\n",
114114
"\n",
115115
"test_stacks = Stacks.from_text(example.partition(\"\\n\\n\")[0])\n",
116116
"Crane9000Moves.from_text(example.partition(\"\\n\\n\")[-1]).rearrange(test_stacks)\n",
@@ -119,7 +119,7 @@
119119
},
120120
{
121121
"cell_type": "code",
122-
"execution_count": 2,
122+
"execution_count": 3,
123123
"metadata": {},
124124
"outputs": [
125125
{
@@ -133,7 +133,6 @@
133133
"source": [
134134
"import aocd\n",
135135
"\n",
136-
"\n",
137136
"starting_positions, _, procedure = aocd.get_data(day=5, year=2022).partition(\"\\n\\n\")\n",
138137
"\n",
139138
"stacks = Stacks.from_text(starting_positions)\n",
@@ -152,12 +151,12 @@
152151
"There were two ways to handle the new requirements to move whole substacks:\n",
153152
"\n",
154153
"- Continuing using stacks, by popping the crates one by one to an _intermediary_ stack and then popping from there to the target stack.\n",
155-
"- Refactor and switch to lists that I can just slice. I did the latter."
154+
"- Refactor and switch to lists that I can just slice. I did the latter.\n"
156155
]
157156
},
158157
{
159158
"cell_type": "code",
160-
"execution_count": 3,
159+
"execution_count": 4,
161160
"metadata": {},
162161
"outputs": [],
163162
"source": [
@@ -175,7 +174,7 @@
175174
},
176175
{
177176
"cell_type": "code",
178-
"execution_count": 4,
177+
"execution_count": 5,
179178
"metadata": {},
180179
"outputs": [
181180
{
@@ -210,7 +209,7 @@
210209
"name": "python",
211210
"nbconvert_exporter": "python",
212211
"pygments_lexer": "ipython3",
213-
"version": "3.11.0"
212+
"version": "3.12.0"
214213
},
215214
"orig_nbformat": 4,
216215
"vscode": {

2022/Day 06.ipynb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"source": [
88
"# Efficient handling of streams of data\n",
99
"\n",
10-
"* https://adventofcode.com/2022/day/6\n",
10+
"- https://adventofcode.com/2022/day/6\n",
1111
"\n",
1212
"The task asks us to find the first position where a stream of data contained 4 consecutive unique strings. How to best handle this efficiently?\n",
1313
"\n",
1414
"The best way to to do this is to only hold 4 characters at a time, and a [`deque`](https://docs.python.org/3/library/collections.html#collections.deque) makes it very efficient to add characters at the end and automatically drop the no-longer-needed 5th character from the other side.\n",
1515
"\n",
16-
"How to track that they are unique then? While we could pass the `deque` to a `set()` each step and check if the length is still 4, you can better use a [`Counter()`](https://docs.python.org/3/library/collections.html#collections.Counter) as that can be used to keep track of all the characters currently being tracked and only have to update the counts for the new character added and the old character dropped. If there are then 4 counts in the Counter, we know they must all be unique."
16+
"How to track that they are unique then? While we could pass the `deque` to a `set()` each step and check if the length is still 4, you can better use a [`Counter()`](https://docs.python.org/3/library/collections.html#collections.Counter) as that can be used to keep track of all the characters currently being tracked and only have to update the counts for the new character added and the old character dropped. If there are then 4 counts in the Counter, we know they must all be unique.\n"
1717
]
1818
},
1919
{
@@ -22,7 +22,7 @@
2222
"metadata": {},
2323
"outputs": [],
2424
"source": [
25-
"from collections import deque, Counter\n",
25+
"from collections import Counter, deque\n",
2626
"from itertools import islice\n",
2727
"from typing import Iterable\n",
2828
"\n",
@@ -68,7 +68,6 @@
6868
"source": [
6969
"import aocd\n",
7070
"\n",
71-
"\n",
7271
"datastream = aocd.get_data(day=6, year=2022)\n",
7372
"print(\"Part 1:\", find_n_unique(datastream))"
7473
]
@@ -80,7 +79,7 @@
8079
"source": [
8180
"## Part 2, do it more!\n",
8281
"\n",
83-
"Part two was very easy, as I *already had made the number of characters a variable*. Plus, the approach was already sound and efficient, so all I had to do was pass in an extra argument to solve part 2."
82+
"Part two was very easy, as I _already had made the number of characters a variable_. Plus, the approach was already sound and efficient, so all I had to do was pass in an extra argument to solve part 2.\n"
8483
]
8584
},
8685
{
@@ -135,7 +134,7 @@
135134
"name": "python",
136135
"nbconvert_exporter": "python",
137136
"pygments_lexer": "ipython3",
138-
"version": "3.11.0"
137+
"version": "3.12.0"
139138
},
140139
"orig_nbformat": 4,
141140
"vscode": {

0 commit comments

Comments
 (0)
0