Add files via upload

This commit is contained in:
Peter Norvig
2024-12-12 12:42:49 -08:00
committed by GitHub
parent d22d25dd36
commit 832974caca

View File

@@ -262,7 +262,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 2.1: .0012 seconds, answer 257 ok" "Puzzle 2.1: .0011 seconds, answer 257 ok"
] ]
}, },
"execution_count": 7, "execution_count": 7,
@@ -312,7 +312,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 2.2: .0069 seconds, answer 328 ok" "Puzzle 2.2: .0072 seconds, answer 328 ok"
] ]
}, },
"execution_count": 9, "execution_count": 9,
@@ -403,7 +403,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 3.1: .0013 seconds, answer 156388521 ok" "Puzzle 3.1: .0014 seconds, answer 156388521 ok"
] ]
}, },
"execution_count": 12, "execution_count": 12,
@@ -542,7 +542,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 4.1: .0737 seconds, answer 2401 ok" "Puzzle 4.1: .0706 seconds, answer 2401 ok"
] ]
}, },
"execution_count": 17, "execution_count": 17,
@@ -580,7 +580,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 4.2: .0602 seconds, answer 1822 ok" "Puzzle 4.2: .0597 seconds, answer 1822 ok"
] ]
}, },
"execution_count": 18, "execution_count": 18,
@@ -878,7 +878,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 6.1: .0044 seconds, answer 5329 ok" "Puzzle 6.1: .0040 seconds, answer 5329 ok"
] ]
}, },
"execution_count": 26, "execution_count": 26,
@@ -961,7 +961,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 6.2: 5.2239 seconds, answer 2162 ok" "Puzzle 6.2: 5.2261 seconds, answer 2162 ok"
] ]
}, },
"execution_count": 28, "execution_count": 28,
@@ -1107,7 +1107,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 7.1: .0395 seconds, answer 1985268524462 ok" "Puzzle 7.1: .0415 seconds, answer 1985268524462 ok"
] ]
}, },
"execution_count": 32, "execution_count": 32,
@@ -1139,7 +1139,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 7.2: 2.5654 seconds, answer 150077710195188 ok" "Puzzle 7.2: 2.7765 seconds, answer 150077710195188 ok"
] ]
}, },
"execution_count": 33, "execution_count": 33,
@@ -1235,7 +1235,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 8.1: .0066 seconds, answer 220 ok" "Puzzle 8.1: .0070 seconds, answer 220 ok"
] ]
}, },
"execution_count": 36, "execution_count": 36,
@@ -1255,7 +1255,7 @@
"source": [ "source": [
"### Part 2: How many unique locations within the bounds of the map contain an updated antinode?\n", "### Part 2: How many unique locations within the bounds of the map contain an updated antinode?\n",
"\n", "\n",
"For Part 2, the updated definition of antinodes means that they can now occur at *any* point that is exactly on line with two antennas of the same frequency, regardless of distance. So if the two antennas are *A* and *B* then the antinbodal points are of the form *A* + *k*(*A* - *B*) for any scalar value *k* (positive or negative) that makes the resulting point fall within the grid.\n", "For Part 2, the updated definition of antinodes means that they can now occur at *any* point that is exactly on line with two antennas of the same frequency, regardless of distance. So if the two antennas are *A* and *B* then the antinodal points can be found by starting at *A* and going step by step in the direction of the vector *A* - *B* and also in the direction *B* - *A*, going as far as you can while staying on the grid. The `Grid.follow_line` method facilitates that.\n",
"\n", "\n",
"I'll parametrize `updated_antinodes` so it can handle both parts:" "I'll parametrize `updated_antinodes` so it can handle both parts:"
] ]
@@ -1277,16 +1277,8 @@
"\n", "\n",
"def updated_antinodes2(A: Point, B: Point, antennas: Grid) -> Set[Point]:\n", "def updated_antinodes2(A: Point, B: Point, antennas: Grid) -> Set[Point]:\n",
" \"\"\"The set of updated antinodal points for two antenna points, A and B.\"\"\"\n", " \"\"\"The set of updated antinodal points for two antenna points, A and B.\"\"\"\n",
" antinodes = set()\n", " return (set(antennas.follow_line(A, sub(A, B))) | \n",
" D = sub(A, B)\n", " set(antennas.follow_line(A, sub(B, A))))"
" for step in (+1, -1):\n",
" for k in count_from(0, step):\n",
" P = add2(A, mul(D, k))\n",
" if P in antennas:\n",
" antinodes.add(P)\n",
" else:\n",
" break\n",
" return antinodes"
] ]
}, },
{ {
@@ -1320,7 +1312,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 8.2: .0074 seconds, answer 813 ok" "Puzzle 8.2: .0075 seconds, answer 813 ok"
] ]
}, },
"execution_count": 39, "execution_count": 39,
@@ -1436,7 +1428,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 9.1: .0415 seconds, answer 6332189866718 ok" "Puzzle 9.1: .0438 seconds, answer 6332189866718 ok"
] ]
}, },
"execution_count": 42, "execution_count": 42,
@@ -1517,7 +1509,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 9.2: 6.1664 seconds, answer 6353648390778 ok" "Puzzle 9.2: 6.2652 seconds, answer 6353648390778 ok"
] ]
}, },
"execution_count": 44, "execution_count": 44,
@@ -1626,7 +1618,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 10.1: .0143 seconds, answer 744 ok" "Puzzle 10.1: .0182 seconds, answer 744 ok"
] ]
}, },
"execution_count": 47, "execution_count": 47,
@@ -1677,7 +1669,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 10.2: .0222 seconds, answer 1651 ok" "Puzzle 10.2: .0218 seconds, answer 1651 ok"
] ]
}, },
"execution_count": 49, "execution_count": 49,
@@ -1787,7 +1779,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 11.1: .1567 seconds, answer 194482 ok" "Puzzle 11.1: .1609 seconds, answer 194482 ok"
] ]
}, },
"execution_count": 52, "execution_count": 52,
@@ -1848,7 +1840,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 11.1: .0035 seconds, answer 194482 ok" "Puzzle 11.1: .0034 seconds, answer 194482 ok"
] ]
}, },
"execution_count": 54, "execution_count": 54,
@@ -1870,7 +1862,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 11.2: .1301 seconds, answer 232454623677743 ok" "Puzzle 11.2: .1312 seconds, answer 232454623677743 ok"
] ]
}, },
"execution_count": 55, "execution_count": 55,
@@ -1942,7 +1934,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 64, "execution_count": 57,
"id": "79f91f38-e325-44f2-9e53-b64ce12d9d35", "id": "79f91f38-e325-44f2-9e53-b64ce12d9d35",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -1995,17 +1987,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 65, "execution_count": 59,
"id": "cdaf655b-d12c-4973-b19b-3132e5e691c6", "id": "cdaf655b-d12c-4973-b19b-3132e5e691c6",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 12.1: .0804 seconds, answer 1402544 ok" "Puzzle 12.1: .0803 seconds, answer 1402544 ok"
] ]
}, },
"execution_count": 65, "execution_count": 59,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -2045,7 +2037,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 74, "execution_count": 60,
"id": "38c30e15-3a33-40c2-b734-163a15af7a8a", "id": "38c30e15-3a33-40c2-b734-163a15af7a8a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -2068,17 +2060,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 71, "execution_count": 61,
"id": "72175812-dcd0-4f1b-9efa-0dceeeafa609", "id": "72175812-dcd0-4f1b-9efa-0dceeeafa609",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 12.1: .0962 seconds, answer 1402544 ok" "Puzzle 12.1: .0783 seconds, answer 1402544 ok"
] ]
}, },
"execution_count": 71, "execution_count": 61,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -2090,17 +2082,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 72, "execution_count": 62,
"id": "9defcd35-91bc-41d4-a16f-bb7a4ede75e7", "id": "9defcd35-91bc-41d4-a16f-bb7a4ede75e7",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Puzzle 12.2: .1155 seconds, answer 862486 ok" "Puzzle 12.2: .1110 seconds, answer 862486 ok"
] ]
}, },
"execution_count": 72, "execution_count": 62,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -2122,7 +2114,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 62, "execution_count": 63,
"id": "34813fc9-a000-4cd8-88ae-692851b3242c", "id": "34813fc9-a000-4cd8-88ae-692851b3242c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -2132,28 +2124,28 @@
"text": [ "text": [
"Puzzle 1.1: .0002 seconds, answer 1830467 ok\n", "Puzzle 1.1: .0002 seconds, answer 1830467 ok\n",
"Puzzle 1.2: .0002 seconds, answer 26674158 ok\n", "Puzzle 1.2: .0002 seconds, answer 26674158 ok\n",
"Puzzle 2.1: .0012 seconds, answer 257 ok\n", "Puzzle 2.1: .0011 seconds, answer 257 ok\n",
"Puzzle 2.2: .0069 seconds, answer 328 ok\n", "Puzzle 2.2: .0072 seconds, answer 328 ok\n",
"Puzzle 3.1: .0013 seconds, answer 156388521 ok\n", "Puzzle 3.1: .0014 seconds, answer 156388521 ok\n",
"Puzzle 3.2: .0009 seconds, answer 75920122 ok\n", "Puzzle 3.2: .0009 seconds, answer 75920122 ok\n",
"Puzzle 4.1: .0737 seconds, answer 2401 ok\n", "Puzzle 4.1: .0706 seconds, answer 2401 ok\n",
"Puzzle 4.2: .0602 seconds, answer 1822 ok\n", "Puzzle 4.2: .0597 seconds, answer 1822 ok\n",
"Puzzle 5.1: .0010 seconds, answer 5762 ok\n", "Puzzle 5.1: .0010 seconds, answer 5762 ok\n",
"Puzzle 5.2: .0015 seconds, answer 4130 ok\n", "Puzzle 5.2: .0015 seconds, answer 4130 ok\n",
"Puzzle 6.1: .0044 seconds, answer 5329 ok\n", "Puzzle 6.1: .0040 seconds, answer 5329 ok\n",
"Puzzle 6.2: 5.2239 seconds, answer 2162 ok\n", "Puzzle 6.2: 5.2261 seconds, answer 2162 ok\n",
"Puzzle 7.1: .0395 seconds, answer 1985268524462 ok\n", "Puzzle 7.1: .0415 seconds, answer 1985268524462 ok\n",
"Puzzle 7.2: 2.5654 seconds, answer 150077710195188 ok\n", "Puzzle 7.2: 2.7765 seconds, answer 150077710195188 ok\n",
"Puzzle 8.1: .0063 seconds, answer 220 ok\n", "Puzzle 8.1: .0063 seconds, answer 220 ok\n",
"Puzzle 8.2: .0074 seconds, answer 813 ok\n", "Puzzle 8.2: .0075 seconds, answer 813 ok\n",
"Puzzle 9.1: .0415 seconds, answer 6332189866718 ok\n", "Puzzle 9.1: .0438 seconds, answer 6332189866718 ok\n",
"Puzzle 9.2: 6.1664 seconds, answer 6353648390778 ok\n", "Puzzle 9.2: 6.2652 seconds, answer 6353648390778 ok\n",
"Puzzle 10.1: .0143 seconds, answer 744 ok\n", "Puzzle 10.1: .0182 seconds, answer 744 ok\n",
"Puzzle 10.2: .0222 seconds, answer 1651 ok\n", "Puzzle 10.2: .0218 seconds, answer 1651 ok\n",
"Puzzle 11.1: .0035 seconds, answer 194482 ok\n", "Puzzle 11.1: .0034 seconds, answer 194482 ok\n",
"Puzzle 11.2: .1301 seconds, answer 232454623677743 ok\n", "Puzzle 11.2: .1312 seconds, answer 232454623677743 ok\n",
"Puzzle 12.1: .0773 seconds, answer 1402544 ok\n", "Puzzle 12.1: .0783 seconds, answer 1402544 ok\n",
"Puzzle 12.2: .1090 seconds, answer 862486 ok\n" "Puzzle 12.2: .1110 seconds, answer 862486 ok\n"
] ]
} }
], ],