Skip to content

Commit 84a64d9

Browse files
committed
Clean up 2020
- Reformatted using ruff - Warnings addressed No outputs have changed (but matplotlib plots are a little larger, timeit results are faster)
1 parent 440650f commit 84a64d9

25 files changed

+81290
-1133
lines changed

2020/Day 01.ipynb

+46-36
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
{
2-
"metadata": {
3-
"language_info": {
4-
"codemirror_mode": {
5-
"name": "ipython",
6-
"version": 3
7-
},
8-
"file_extension": ".py",
9-
"mimetype": "text/x-python",
10-
"name": "python",
11-
"nbconvert_exporter": "python",
12-
"pygments_lexer": "ipython3",
13-
"version": "3.9.0-final"
14-
},
15-
"orig_nbformat": 2,
16-
"kernelspec": {
17-
"name": "python3",
18-
"display_name": "Python 3"
19-
}
20-
},
21-
"nbformat": 4,
22-
"nbformat_minor": 2,
232
"cells": [
243
{
4+
"cell_type": "markdown",
5+
"metadata": {},
256
"source": [
267
"# Welcome to AoC 2020, time for a holiday!\n",
278
"\n",
28-
"* https://adventofcode.com/2020/day/1\n",
9+
"- https://adventofcode.com/2020/day/1\n",
2910
"\n",
3011
"Another year, another set of puzzles! This time, we are 'not doing christmas'; Eric Wastl has torn up the [AoC Bingo card](https://www.reddit.com/r/adventofcode/comments/k3q7tr/my_advent_of_code_2020_bingo_card_fun_little_side/) and decided to send us to a tropical island instead. :-P\n",
3112
"\n",
32-
"As always, we start with a warm-up. Rather than iterate over all combinations ($O(n^2)$), put the numbers in a set, loop over the set for the first number, and see if `2020 - first` is a member of the set. That gives us a $O(n)$ runtime!"
33-
],
34-
"cell_type": "markdown",
35-
"metadata": {}
13+
"As always, we start with a warm-up. Rather than iterate over all combinations ($O(n^2)$), put the numbers in a set, loop over the set for the first number, and see if `2020 - first` is a member of the set. That gives us a $O(n)$ runtime!\n"
14+
]
3615
},
3716
{
3817
"cell_type": "code",
@@ -41,6 +20,7 @@
4120
"outputs": [],
4221
"source": [
4322
"import aocd\n",
23+
"\n",
4424
"coins = set(map(int, aocd.get_data(day=1, year=2020).splitlines()))"
4525
]
4626
},
@@ -52,25 +32,33 @@
5232
"source": [
5333
"from operator import mul\n",
5434
"\n",
35+
"\n",
5536
"def find_sum(coins, target=2020):\n",
5637
" for first in coins:\n",
5738
" second = target - first\n",
5839
" if second in coins:\n",
5940
" return first, second\n",
60-
" \n",
41+
"\n",
6142
" raise ValueError(\"Not solvable\")\n",
6243
"\n",
44+
"\n",
6345
"def sum_coins(coins):\n",
6446
" return mul(*find_sum(coins))\n",
6547
"\n",
66-
"test = set(map(int, '''\\\n",
48+
"\n",
49+
"test = set(\n",
50+
" map(\n",
51+
" int,\n",
52+
" \"\"\"\\\n",
6753
"1721\n",
6854
"979\n",
6955
"366\n",
7056
"299\n",
7157
"675\n",
7258
"1456\n",
73-
"'''.splitlines()))\n",
59+
"\"\"\".splitlines(),\n",
60+
" )\n",
61+
")\n",
7462
"assert sum_coins(test) == 514579"
7563
]
7664
},
@@ -80,8 +68,8 @@
8068
"metadata": {},
8169
"outputs": [
8270
{
83-
"output_type": "stream",
8471
"name": "stdout",
72+
"output_type": "stream",
8573
"text": [
8674
"Part 1: 73371\n"
8775
]
@@ -92,13 +80,13 @@
9280
]
9381
},
9482
{
83+
"cell_type": "markdown",
84+
"metadata": {},
9585
"source": [
9686
"# Part 2\n",
9787
"\n",
9888
"To find the third coin, just pop one of the values of the set of coins, run the first puzzle solution to see if there is a combination that sums to `2020 - selected`, and continue until we found a combination. That's an $O(N^2)$ solution:\n"
99-
],
100-
"cell_type": "markdown",
101-
"metadata": {}
89+
]
10290
},
10391
{
10492
"cell_type": "code",
@@ -115,6 +103,7 @@
115103
" except ValueError:\n",
116104
" continue\n",
117105
"\n",
106+
"\n",
118107
"assert sum_three(test) == 241861950"
119108
]
120109
},
@@ -124,8 +113,8 @@
124113
"metadata": {},
125114
"outputs": [
126115
{
127-
"output_type": "stream",
128116
"name": "stdout",
117+
"output_type": "stream",
129118
"text": [
130119
"Part 2: 127642310\n"
131120
]
@@ -135,5 +124,26 @@
135124
"print(\"Part 2:\", sum_three(coins))"
136125
]
137126
}
138-
]
139-
}
127+
],
128+
"metadata": {
129+
"kernelspec": {
130+
"display_name": "Python 3",
131+
"name": "python3"
132+
},
133+
"language_info": {
134+
"codemirror_mode": {
135+
"name": "ipython",
136+
"version": 3
137+
},
138+
"file_extension": ".py",
139+
"mimetype": "text/x-python",
140+
"name": "python",
141+
"nbconvert_exporter": "python",
142+
"pygments_lexer": "ipython3",
143+
"version": "3.12.0"
144+
},
145+
"orig_nbformat": 2
146+
},
147+
"nbformat": 4,
148+
"nbformat_minor": 2
149+
}

2020/Day 02.ipynb

+50-42
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
{
2-
"metadata": {
3-
"language_info": {
4-
"codemirror_mode": {
5-
"name": "ipython",
6-
"version": 3
7-
},
8-
"file_extension": ".py",
9-
"mimetype": "text/x-python",
10-
"name": "python",
11-
"nbconvert_exporter": "python",
12-
"pygments_lexer": "ipython3",
13-
"version": "3.9.0-final"
14-
},
15-
"orig_nbformat": 2,
16-
"kernelspec": {
17-
"name": "python3",
18-
"display_name": "Python 3"
19-
}
20-
},
21-
"nbformat": 4,
22-
"nbformat_minor": 2,
232
"cells": [
243
{
4+
"cell_type": "markdown",
5+
"metadata": {},
256
"source": [
267
"# Counting letters\n",
278
"\n",
28-
"* https://adventofcode.com/2020/day/2\n",
9+
"- https://adventofcode.com/2020/day/2\n",
2910
"\n",
30-
"I like to use a dataclass for parsing tasks like these. A single regex to read out each line, and methods on the class to implement the password rule checks."
31-
],
32-
"cell_type": "markdown",
33-
"metadata": {}
11+
"I like to use a dataclass for parsing tasks like these. A single regex to read out each line, and methods on the class to implement the password rule checks.\n"
12+
]
3413
},
3514
{
3615
"cell_type": "code",
@@ -41,7 +20,10 @@
4120
"import re\n",
4221
"from dataclasses import dataclass\n",
4322
"\n",
44-
"_line = re.compile(r'^(?P<min_>\\d+)-(?P<max_>\\d+) (?P<letter>[a-z]):\\s*(?P<password>[a-z]+)$')\n",
23+
"_line = re.compile(\n",
24+
" r\"^(?P<min_>\\d+)-(?P<max_>\\d+) (?P<letter>[a-z]):\\s*(?P<password>[a-z]+)$\"\n",
25+
")\n",
26+
"\n",
4527
"\n",
4628
"@dataclass\n",
4729
"class PWRule:\n",
@@ -51,27 +33,31 @@
5133
" password: str\n",
5234
"\n",
5335
" @classmethod\n",
54-
" def from_line(cls, line: str) -> 'PWRule':\n",
36+
" def from_line(cls, line: str) -> \"PWRule\":\n",
5537
" match = _line.search(line)\n",
56-
" min_, max_ = int(match['min_']), int(match['max_'])\n",
57-
" return cls(min_, max_, match['letter'], match['password'])\n",
38+
" min_, max_ = int(match[\"min_\"]), int(match[\"max_\"])\n",
39+
" return cls(min_, max_, match[\"letter\"], match[\"password\"])\n",
5840
"\n",
5941
" def is_valid(self) -> bool:\n",
6042
" return self.min_ <= self.password.count(self.letter) <= self.max_\n",
6143
"\n",
6244
" def is_valid_toboggan_policy(self) -> bool:\n",
63-
" return (self.password[self.min_ - 1], self.password[self.max_ - 1]).count(self.letter) == 1\n",
45+
" return (self.password[self.min_ - 1], self.password[self.max_ - 1]).count(\n",
46+
" self.letter\n",
47+
" ) == 1\n",
6448
"\n",
6549
"\n",
6650
"def read_passwords(lines):\n",
67-
" return [PWRule.from_line(l) for l in lines]\n",
51+
" return [PWRule.from_line(line) for line in lines]\n",
6852
"\n",
6953
"\n",
70-
"test = read_passwords('''\\\n",
54+
"test = read_passwords(\n",
55+
" \"\"\"\\\n",
7156
"1-3 a: abcde\n",
7257
"1-3 b: cdefg\n",
7358
"2-9 c: ccccccccc\n",
74-
"'''.splitlines())\n",
59+
"\"\"\".splitlines()\n",
60+
")\n",
7561
"\n",
7662
"assert sum(pwr.is_valid() for pwr in test) == 2\n",
7763
"assert sum(pwr.is_valid_toboggan_policy() for pwr in test) == 1"
@@ -83,8 +69,9 @@
8369
"metadata": {},
8470
"outputs": [],
8571
"source": [
86-
" import aocd\n",
87-
" rules = read_passwords(aocd.get_data(day=2, year=2020).splitlines())"
72+
"import aocd\n",
73+
"\n",
74+
"rules = read_passwords(aocd.get_data(day=2, year=2020).splitlines())"
8875
]
8976
},
9077
{
@@ -93,15 +80,15 @@
9380
"metadata": {},
9481
"outputs": [
9582
{
96-
"output_type": "stream",
9783
"name": "stdout",
84+
"output_type": "stream",
9885
"text": [
9986
"Part 1: 591\n"
10087
]
10188
}
10289
],
10390
"source": [
104-
"print('Part 1:', sum(pwr.is_valid() for pwr in rules))"
91+
"print(\"Part 1:\", sum(pwr.is_valid() for pwr in rules))"
10592
]
10693
},
10794
{
@@ -110,16 +97,37 @@
11097
"metadata": {},
11198
"outputs": [
11299
{
113-
"output_type": "stream",
114100
"name": "stdout",
101+
"output_type": "stream",
115102
"text": [
116103
"Part 2: 335\n"
117104
]
118105
}
119106
],
120107
"source": [
121-
"print('Part 2:', sum(pwr.is_valid_toboggan_policy() for pwr in rules))"
108+
"print(\"Part 2:\", sum(pwr.is_valid_toboggan_policy() for pwr in rules))"
122109
]
123110
}
124-
]
125-
}
111+
],
112+
"metadata": {
113+
"kernelspec": {
114+
"display_name": "Python 3",
115+
"name": "python3"
116+
},
117+
"language_info": {
118+
"codemirror_mode": {
119+
"name": "ipython",
120+
"version": 3
121+
},
122+
"file_extension": ".py",
123+
"mimetype": "text/x-python",
124+
"name": "python",
125+
"nbconvert_exporter": "python",
126+
"pygments_lexer": "ipython3",
127+
"version": "3.12.0"
128+
},
129+
"orig_nbformat": 2
130+
},
131+
"nbformat": 4,
132+
"nbformat_minor": 2
133+
}

0 commit comments

Comments
 (0)