AoC update

This commit is contained in:
Peter Norvig
2022-12-11 00:45:43 -08:00
committed by GitHub
parent 55eb6ed407
commit 1968a9b8c0
2 changed files with 304 additions and 176 deletions

File diff suppressed because one or more lines are too long

View File

@@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
@@ -31,6 +31,7 @@
"import heapq\n",
"import string\n",
"import functools\n",
"import operator\n",
"import pathlib\n",
"import re"
]
@@ -59,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
@@ -67,13 +68,13 @@
"lines = '\\n' # For inputs where each record is a line\n",
"paragraphs = '\\n\\n' # For inputs where each record is a paragraph \n",
"\n",
"def parse(day_or_text:Union[int, str], parser:Callable=str, sep:Callable=lines, show=6) -> tuple:\n",
"def parse(day_or_text:Union[int, str], parser:Callable=str, sep:str=lines, show=6) -> tuple:\n",
" \"\"\"Split the input text into items separated by `sep`, and apply `parser` to each.\n",
" The first argument is either the text itself, or the day number of a text file.\"\"\"\n",
" text = get_text(day_or_text)\n",
" print_parse_items('Puzzle input', text.splitlines(), show, 'line')\n",
" records = mapt(parser, text.rstrip().split(sep))\n",
" if parser != str:\n",
" if parser != str or sep != lines:\n",
" print_parse_items('Parsed representation', records, show, f'{type(records[0]).__name__}')\n",
" return records\n",
"\n",
@@ -109,7 +110,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
@@ -144,7 +145,7 @@
" x = float(text)\n",
" return round(x) if x.is_integer() else x\n",
" except ValueError:\n",
" return text\n",
" return text.strip()\n",
" \n",
"def mapt(function: Callable, sequence) -> tuple:\n",
" \"\"\"`map`, with the result as a tuple.\"\"\"\n",
@@ -189,7 +190,7 @@
"# `answers` is a dict of {puzzle_number_id: message_about_results}\n",
"answers = {} \n",
"\n",
"def answer(puzzle, correct, code: callable) -> str:\n",
"def answer(puzzle, correct, code: callable):\n",
" \"\"\"Verify that calling `code` computes the `correct` answer for `puzzle`. \n",
" Record results in the dict `answers`. Prints execution time.\"\"\"\n",
" def pretty(x): return f'{x:,d}' if is_int(x) else truncate(x)\n",
@@ -201,7 +202,7 @@
" f'correct answer: {ans}' if (got == correct) else\n",
" f'WRONG!! ANSWER: {ans}; EXPECTED {pretty(correct)}')\n",
" answers[puzzle] = msg\n",
" return msg"
" print(msg)"
]
},
{