Skip to content

Commit

Permalink
Invert vectors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Dec 18, 2023
1 parent 11dc19e commit 21f1e51
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 39 deletions.
188 changes: 149 additions & 39 deletions src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5696,27 +5696,25 @@
" \n",
" for point, dirn in self.path_taken:\n",
" value = self.value_at_point(point)\n",
" if value not in infra_mappings:\n",
" for dir_set, arrow in zip(dir_sets, (\"^\", \">\", \"v\", \"<\")):\n",
" if LightGrid.VECTORS_TO_ARROWS[dirn] == arrow:\n",
" dir_set.add(point)\n",
" continue\n",
"\n",
" # Batched scatter operations\n",
" for infra_type, marker, color in [(vert_splitters, r'$\\vert$', 'yellow'), \n",
" (horz_splitters, r'$-$', 'yellow'), \n",
" (forw_mirrors, r'$\\slash$', 'yellow'), \n",
" (back_mirrors, r'$\\backslash$', 'yellow')]:\n",
" \n",
" if infra_type: # check not empty\n",
" x, y = zip(*((point.x, point.y) for point in infra_type))\n",
" # axes.text(x, y, marker, color=color, ha='center', va='center', fontsize=mkr_size/10)\n",
" axes.scatter(x, y, marker=marker, s=mkr_size, color=color) \n",
" \n",
" # if value not in infra_mappings:\n",
" for dir_set, arrow in zip(dir_sets, (\"^\", \">\", \"v\", \"<\")):\n",
" if LightGrid.VECTORS_TO_ARROWS[dirn] == arrow:\n",
" dir_set.add(point)\n",
" continue\n",
"\n",
" for dir_set, arrow in zip(dir_sets, (\"^\", \">\", \"v\", \"<\")):\n",
" if dir_set:\n",
" dir_set_x, dir_set_y = zip(*((point.x, point.y) for point in dir_set))\n",
" axes.scatter(dir_set_x, dir_set_y, marker=arrow, s=mkr_size, color=\"white\") \n",
" axes.scatter(dir_set_x, dir_set_y, marker=arrow, s=mkr_size*0.5, color=\"white\") \n",
"\n",
" for infra_type, marker in [(vert_splitters, r'$\\vert$'), \n",
" (horz_splitters, r'$-$'), \n",
" (forw_mirrors, r'$\\slash$'), \n",
" (back_mirrors, r'$\\backslash$')]:\n",
" \n",
" if infra_type: # check not empty\n",
" x, y = zip(*((point.x, point.y) for point in infra_type))\n",
" axes.scatter(x, y, marker=marker, s=mkr_size, color=\"xkcd:azure\") \n",
" \n",
" # save the plot as a frame; store the frame in-memory, using a BytesIO buffer\n",
" frame = BytesIO()\n",
Expand Down Expand Up @@ -6159,20 +6157,54 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/markdown": [
"#### See [Day 18](https://adventofcode.com/2023/day/18)."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"DAY = \"n\" # replace with actual number (without leading digit)\n",
"DAY = \"18\" # replace with actual number (without leading digit)\n",
"day_link = f\"#### See [Day {DAY}](https://adventofcode.com/{YEAR}/day/{DAY}).\"\n",
"display(Markdown(day_link))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m06:57:25.688:aoc2023 - INF: Session cookie retrieved: 53616c...ff33e0\u001b[39m\n",
"\u001b[34m06:57:26.549:aoc2023 - DBG: Writing input file input.txt\u001b[39m\n",
"\u001b[32m06:57:26.552:aoc2023 - INF: Input data:\n",
" 1: L 7 (#2ac8e2)\n",
" 2: D 5 (#14b771)\n",
" 3: L 14 (#302602)\n",
" 4: U 5 (#14b773)\n",
" 5: L 4 (#55b782)\n",
"...\n",
"690: U 4 (#043ac3)\n",
"691: L 8 (#49ca22)\n",
"692: U 7 (#057783)\n",
"693: L 6 (#57e6a2)\n",
"694: U 12 (#2d7e63)\u001b[39m\n"
]
}
],
"source": [
"d_name = \"d\" + str(DAY).zfill(2) # e.g. d01\n",
"script_name = \"aoc\" + str(YEAR) + d_name # e.g. aoc2017d01\n",
Expand All @@ -6199,31 +6231,109 @@
"source": [
"### Day 18 Part 1\n",
"\n",
"Overview..."
"We're going to dig a lagoon. We've been given a dig plan that looks like this:\n",
"\n",
"```text\n",
"R 6 (#70c710)\n",
"D 5 (#0dc571)\n",
"L 2 (#5713f0)\n",
"D 2 (#d2c081)\n",
"R 2 (#59c680)\n",
"D 2 (#411b91)\n",
"L 5 (#8ceee2)\n",
"U 2 (#caa173)\n",
"L 1 (#1b58a2)\n",
"U 2 (#caa171)\n",
"R 2 (#7807d2)\n",
"U 3 (#a77fa3)\n",
"L 2 (#015232)\n",
"U 2 (#7a21e3)\n",
"```\n",
"\n",
"The digger starts in a 1 meter cube hole in the ground and can move the specified number of metres up, down, left or right. These directions are all in the same plane (as seen from above), so we're in a 2D grid. Having followed the instructions to dig a tunnel that represents the edge of the lagoon, we then dig out the interior.\n",
"\n",
"Each trench is also listed with the color that the edge of the trench should be painted as an RGB hexadecimal color code.\n",
"\n",
"**If they follow their dig plan, how many cubic meters of lava could it hold?**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"def solve_part1(data):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
" plan = []\n",
" for line in data:\n",
" dirn, path_len, hex_code = line.split()\n",
" hex_code = hex_code.replace(\"(\", \"\")\n",
" hex_code = hex_code.replace(\")\", \"\")\n",
" plan.append((dirn, int(path_len), hex_code))\n",
" \n",
" for instr in plan:\n",
" logger.debug(f\"{instr}\")"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m07:07:54.781:aoc2023 - DBG: ('R', 6, '#70c710')\u001b[39m\n",
"\u001b[34m07:07:54.782:aoc2023 - DBG: ('D', 5, '#0dc571')\u001b[39m\n",
"\u001b[34m07:07:54.782:aoc2023 - DBG: ('L', 2, '#5713f0')\u001b[39m\n",
"\u001b[34m07:07:54.783:aoc2023 - DBG: ('D', 2, '#d2c081')\u001b[39m\n",
"\u001b[34m07:07:54.783:aoc2023 - DBG: ('R', 2, '#59c680')\u001b[39m\n",
"\u001b[34m07:07:54.783:aoc2023 - DBG: ('D', 2, '#411b91')\u001b[39m\n",
"\u001b[34m07:07:54.784:aoc2023 - DBG: ('L', 5, '#8ceee2')\u001b[39m\n",
"\u001b[34m07:07:54.784:aoc2023 - DBG: ('U', 2, '#caa173')\u001b[39m\n",
"\u001b[34m07:07:54.784:aoc2023 - DBG: ('L', 1, '#1b58a2')\u001b[39m\n",
"\u001b[34m07:07:54.784:aoc2023 - DBG: ('U', 2, '#caa171')\u001b[39m\n",
"\u001b[34m07:07:54.785:aoc2023 - DBG: ('R', 2, '#7807d2')\u001b[39m\n",
"\u001b[34m07:07:54.785:aoc2023 - DBG: ('U', 3, '#a77fa3')\u001b[39m\n",
"\u001b[34m07:07:54.785:aoc2023 - DBG: ('L', 2, '#015232')\u001b[39m\n",
"\u001b[34m07:07:54.786:aoc2023 - DBG: ('U', 2, '#7a21e3')\u001b[39m\n"
]
},
{
"ename": "AssertionError",
"evalue": "None != 62",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
"File \u001b[1;32m<timed exec>:19\u001b[0m\n",
"Cell \u001b[1;32mIn[9], line 8\u001b[0m, in \u001b[0;36mvalidate\u001b[1;34m(test, answer)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124;03mArgs:\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;124;03m test: the answer given by our solution\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03m answer: the expected answer, e.g. from instructions\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m test \u001b[38;5;241m!=\u001b[39m answer:\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAssertionError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtest\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m != \u001b[39m\u001b[38;5;132;01m{\u001b[39;00manswer\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mAssertionError\u001b[0m: None != 62"
]
}
],
"source": [
"%%time\n",
"sample_inputs = [\"abcdef\"]\n",
"sample_answers = [\"uvwxyz\"]\n",
"sample_inputs = []\n",
"sample_inputs.append(\"\"\"R 6 (#70c710)\n",
"D 5 (#0dc571)\n",
"L 2 (#5713f0)\n",
"D 2 (#d2c081)\n",
"R 2 (#59c680)\n",
"D 2 (#411b91)\n",
"L 5 (#8ceee2)\n",
"U 2 (#caa173)\n",
"L 1 (#1b58a2)\n",
"U 2 (#caa171)\n",
"R 2 (#7807d2)\n",
"U 3 (#a77fa3)\n",
"L 2 (#015232)\n",
"U 2 (#7a21e3)\"\"\")\n",
"sample_answers = [62]\n",
"\n",
"for curr_input, curr_ans in zip(sample_inputs, sample_answers):\n",
" validate(solve_part1(curr_input), curr_ans) # test with sample data\n",
" validate(solve_part1(curr_input.splitlines()), curr_ans) # test with sample data\n",
"\n",
"logger.info(\"Tests passed!\")\n",
"\n",
Expand Down Expand Up @@ -6398,9 +6508,9 @@
"toc_visible": true
},
"kernelspec": {
"display_name": "aca_aoc",
"display_name": "ana-aoc",
"language": "python",
"name": "aca_aoc"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand Down
Binary file added src/AoC_2023/d18/input/input.txt
Binary file not shown.

0 comments on commit 21f1e51

Please sign in to comment.