Add files via upload

This commit is contained in:
Peter Norvig 2024-12-12 11:48:54 -08:00 committed by GitHub
parent 20fdde239f
commit d22d25dd36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1942,18 +1942,19 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 64,
"id": "79f91f38-e325-44f2-9e53-b64ce12d9d35",
"metadata": {},
"outputs": [],
"source": [
"Region = Set[Point]\n",
"region_area = len\n",
"\n",
"def fence_price(farm: Grid) -> int:\n",
" \"\"\"Total price of fences for all the regions in the farm.\"\"\"\n",
" area = len\n",
" return sum(area(region) * perimeter_length(region)\n",
" for region in regions(farm))\n",
" return sum(map(region_price, regions(farm)))\n",
"\n",
"def region_price(region) -> int: return region_area(region) * perimeter_length(region)\n",
"\n",
"def perimeter_length(region: Region) -> int:\n",
" \"\"\"The number of sides on the perimeter of the region.\"\"\"\n",
@ -1994,17 +1995,17 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 65,
"id": "cdaf655b-d12c-4973-b19b-3132e5e691c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Puzzle 12.1: .0773 seconds, answer 1402544 ok"
"Puzzle 12.1: .0804 seconds, answer 1402544 ok"
]
},
"execution_count": 59,
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
@ -2037,16 +2038,24 @@
" -X-.X-\n",
" -XXXX-\n",
" ..XXX.\n",
" ...--.\n"
" ...--.\n",
"\n",
"Again, I'll parameterize `fence_price` to take a `region_price` parameter:"
]
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 74,
"id": "38c30e15-3a33-40c2-b734-163a15af7a8a",
"metadata": {},
"outputs": [],
"source": [
"def fence_price(farm: Grid, region_price=region_price) -> int:\n",
" \"\"\"Total price of fences for all the regions in the farm, given the price function for a region.\"\"\"\n",
" return sum(map(region_price, regions(farm)))\n",
"\n",
"def discount_region_price(region) -> int: return region_area(region) * region_sides(region)\n",
" \n",
"def region_sides(region):\n",
" \"\"\"How many straight-line sides does this region have?\"\"\"\n",
" def has_edge(p: Point, d: Vector): return p in region and add2(p, d) not in region\n",
@ -2054,35 +2063,51 @@
" subtract = quantify(has_edge(p, d) and has_edge(neighbor(p, d), d)\n",
" for p in region\n",
" for d in directions4)\n",
" return perimeter_length(region) - subtract\n",
"\n",
"def discount_fence_price(farm: Grid) -> int:\n",
" \"\"\"Total price of fences for all the regions in the farm, with the bulk discount applied.\"\"\"\n",
" area = len\n",
" return sum(area(region) * region_sides(region)\n",
" for region in regions(farm))"
" return perimeter_length(region) - subtract"
]
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 71,
"id": "72175812-dcd0-4f1b-9efa-0dceeeafa609",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Puzzle 12.1: .0962 seconds, answer 1402544 ok"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer(12.1, 1402544, lambda:\n",
" fence_price(farm))"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "9defcd35-91bc-41d4-a16f-bb7a4ede75e7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Puzzle 12.2: .1090 seconds, answer 862486 ok"
"Puzzle 12.2: .1155 seconds, answer 862486 ok"
]
},
"execution_count": 61,
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer(12.2, 862486, lambda: \n",
" discount_fence_price(farm))"
" fence_price(farm, discount_region_price))"
]
},
{