Skip to content

Instantly share code, notes, and snippets.

@algal
Created December 4, 2024 01:52
Show Gist options
  • Save algal/9f64a2144134b4a22b4ccdfdcb2b0d32 to your computer and use it in GitHub Desktop.
Save algal/9f64a2144134b4a22b4ccdfdcb2b0d32 to your computer and use it in GitHub Desktop.
My Dialog
Display the source blob
Display the rendered blob
Raw
{"cells": [{"cell_type": "code", "metadata": {}, "source": "from aocd import get_data\ninp = get_data(day=3, year=2024)", "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": "It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a result of 2024. Similarly, mul(123,4) would multiply 123 by 4.\n\nHowever, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or mul ( 2 , 4 ) do nothing.\n\nFor example, consider the following section of corrupted memory:\n\n```\nxmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))\n```\n\nOnly the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 (2*4 + 5*5 + 11*8 + 8*5).\n\nScan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?"}, {"cell_type": "code", "metadata": {}, "source": "sample = \"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))\"", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "import re\npat = r'mul\\((?P<x>[0-9]{1,3}),(?P<y>[0-9]{1,3})\\)'", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "?re.match", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstring\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Try to apply the pattern at the start of the string, returning\n", "a Match object, or None if no match was found.\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/re/__init__.py\n", "\u001b[0;31mType:\u001b[0m function\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m = re.match(pat,'mul(12,123)')\nm", "outputs": [{"data": {"text/plain": ["<re.Match object; span=(0, 11), match='mul(12,123)'>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m.group('x')", "outputs": [{"data": {"text/plain": ["'12'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m.group('y')", "outputs": [{"data": {"text/plain": ["'123'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "?re.findall", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfindall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstring\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Return a list of all non-overlapping matches in the string.\n", "\n", "If one or more capturing groups are present in the pattern, return\n", "a list of groups; this will be a list of tuples if the pattern\n", "has more than one group.\n", "\n", "Empty matches are included in the result.\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/re/__init__.py\n", "\u001b[0;31mType:\u001b[0m function\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "?re.finditer", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31mSignature:\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfinditer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstring\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Return an iterator over all non-overlapping matches in the\n", "string. For each match, the iterator returns a Match object.\n", "\n", "Empty matches are included in the result.\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.11/re/__init__.py\n", "\u001b[0;31mType:\u001b[0m function\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "import functools\ndef score_match(m):\n factors = [int(m.group(v)) for v in ['x','y']]\n return factors[0] * factors[1]\nscore_match(m)", "outputs": [{"data": {"text/plain": ["1476"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "import operator, functools\n\ndef score(s:str) -> int:\n matches = re.finditer(pat,s)\n scores = (score_match(m) for m in matches)\n return sum(scores)", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "score(sample)", "outputs": [{"data": {"text/plain": ["161"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "score(inp)", "outputs": [{"data": {"text/plain": ["180233229"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": "## prompt 2\n\nThere are two new instructions you'll need to handle:\n\nThe do() instruction enables future mul instructions.\nThe don't() instruction disables future mul instructions.\nOnly the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.\n\nFor example:\n\n```\nxmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))\n```\n\nThis corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.\n\nThis time, the sum of the results is 48 (`2*4` + `8*5`)."}, {"cell_type": "code", "metadata": {}, "source": "sample2 = \"\"\"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))\"\"\"", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "print(sample2)", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "pat", "outputs": [{"data": {"text/plain": ["'mul\\\\((?P<x>[0-9]{1,3}),(?P<y>[0-9]{1,3})\\\\)'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "import re\npat = r'(?P<mul>mul\\((?P<x>[0-9]{1,3}),(?P<y>[0-9]{1,3})\\))'\npat_do = r'(?P<do>do\\(\\))'\npat_dont = r\"(?P<dont>don't\\(\\))\"", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "print(pat_dont)", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["(?P<dont>don't\\(\\))\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "bigpat = f\"{pat}|{pat_do}|{pat_dont}\"", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m = re.match(bigpat,\"do()\")\nm", "outputs": [{"data": {"text/plain": ["<re.Match object; span=(0, 4), match='do()'>"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m.group('do')", "outputs": [{"data": {"text/plain": ["'do()'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m.group('dont')", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m = re.match(bigpat,\"mul(123,45)\")\nm.group('mul')", "outputs": [{"data": {"text/plain": ["'mul(123,45)'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "m.group('x')", "outputs": [{"data": {"text/plain": ["'123'"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "def score_matches(matches):\n total = 0\n enabled = True\n for m in matches:\n if m.group('dont'):\n enabled = False\n elif m.group('do'):\n enabled = True\n elif m.group('mul') and enabled:\n score = score_match(m)\n total += score\n return total", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "score_matches(sample2)", "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)\n", "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n", "\u001b[0;32m----> 1\u001b[0m \u001b[43mscore_matches\u001b[49m\u001b[43m(\u001b[49m\u001b[43msample2\u001b[49m\u001b[43m)\u001b[49m\n", "\n", "Cell \u001b[0;32mIn[1], line 5\u001b[0m, in \u001b[0;36mscore_matches\u001b[0;34m(matches)\u001b[0m\n", "\u001b[1;32m 3\u001b[0m enabled \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m m \u001b[38;5;129;01min\u001b[39;00m matches:\n", "\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgroup\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdont\u001b[39m\u001b[38;5;124m'\u001b[39m):\n", "\u001b[1;32m 6\u001b[0m enabled \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m m\u001b[38;5;241m.\u001b[39mgroup(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdo\u001b[39m\u001b[38;5;124m'\u001b[39m):\n", "\n", "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'group'\n"]}, {"ename": "AttributeError", "evalue": "'str' object has no attribute 'group'", "output_type": "error", "traceback": ["Traceback (most recent call last):\n", " File \"/usr/local/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n", " File \"<ipython-input-1-b92b43bc4a28>\", line 1, in <module>\n score_matches(sample2)\n", " File \"<ipython-input-1-c1857ab80c38>\", line 5, in score_matches\n if m.group('dont'):\n ^^^^^^^\n", "AttributeError: 'str' object has no attribute 'group'\n"]}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "matches = re.finditer(bigpat,sample2)\nscore_matches(matches)", "outputs": [{"data": {"text/plain": ["48"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "def score_corrupted(s:str) -> int:\n matches = re.finditer(bigpat,s)\n return score_matches(matches)", "outputs": [], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "score_corrupted(sample2)", "outputs": [{"data": {"text/plain": ["48"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}, {"cell_type": "code", "metadata": {}, "source": "score_corrupted(inp)", "outputs": [{"data": {"text/plain": ["95411583"]}, "metadata": {}, "output_type": "execute_result", "execution_count": null}], "execution_count": null}], "metadata": {"kernelspec": {"display_name": "python3", "language": "python", "name": "python3"}}, "nbformat": 4, "nbformat_minor": 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment