Skip to content

Commit

Permalink
feat: gmacario day04 Part 2 solved
Browse files Browse the repository at this point in the history
Signed-off-by: Gianpaolo Macario <[email protected]>
  • Loading branch information
gmacario committed Dec 4, 2024
1 parent f68e67e commit d233e9e
Showing 1 changed file with 202 additions and 35 deletions.
237 changes: 202 additions & 35 deletions solutions/gmacario/day04-jupyter/day04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 3,
"id": "992b746b-12f1-4576-ae26-f3c5d379c844",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -90,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 4,
"id": "2672708d-3eec-4410-80bd-2e5cb40f6ab3",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -121,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 5,
"id": "8d23c8b5-785f-4bfb-8702-f16a064d607b",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -149,12 +149,116 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 6,
"id": "5bd62597-fcb7-47ea-87c7-b926c93630b3",
"metadata": {},
"outputs": [],
"source": [
"def char_at(board, position):\n",
" \"\"\"\n",
" Find a character on board\n",
" Return a string with the character\n",
" \"\"\"\n",
"\n",
" height = len(board)\n",
" width = len(board[0])\n",
" \n",
" assert position[0] >= 0 and position[0] < width, f\"{ic(word, board, position)}\"\n",
" assert position[1] >= 0 and position[1] < height, f\"{ic(word, board, position)}\"\n",
"\n",
" board_char = board[height - position[1] - 1][position[0]]\n",
" \n",
" return board_char"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "38ba741d-7b92-474b-bf73-f479cffdb44d",
"metadata": {},
"outputs": [],
"source": [
"def find_cross_at_pos(board, position):\n",
"\n",
" width = len(board[0])\n",
" height = len(board)\n",
"\n",
" total = 0\n",
"\n",
" x = position[0]\n",
" y = position[1]\n",
"\n",
" # ic(\"find_cross_at_pos\", x, y)\n",
"\n",
" if x < 1 or x+1 > width - 1:\n",
" return 0\n",
" if y < 1 or y+1 > height - 1:\n",
" return 0\n",
" \n",
" if char_at(board, (x, y)) != \"A\":\n",
" return 0\n",
" \n",
" # ic(f\"A found at ({x},{y})\")\n",
" \n",
" if char_at(board, (x-1, y+1)) == \"M\" and char_at(board, (x+1, y-1)) == \"S\":\n",
" if char_at(board, (x-1, y-1)) == \"M\" and char_at(board, (x+1, y+1)) == \"S\":\n",
" return 1\n",
" elif char_at(board, (x-1, y-1)) == \"S\" and char_at(board, (x+1, y+1)) == \"M\":\n",
" return 1\n",
" else:\n",
" return 0\n",
"\n",
" if char_at(board, (x-1, y+1)) == \"S\" and char_at(board, (x+1, y-1)) == \"M\":\n",
" if char_at(board, (x-1, y-1)) == \"M\" and char_at(board, (x+1, y+1)) == \"S\":\n",
" return 1\n",
" elif char_at(board, (x-1, y-1)) == \"S\" and char_at(board, (x+1, y+1)) == \"M\":\n",
" return 1\n",
" else:\n",
" return 0\n",
" \n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8e7df71f-c2c7-411b-8f16-4f7b4d30522a",
"metadata": {},
"outputs": [],
"source": [
"def find_cross_in_board(board):\n",
"\n",
" width = len(board[0])\n",
" height = len(board)\n",
" \n",
" # ic(width, height, board)\n",
" \n",
" total = 0\n",
"\n",
" for x in range(1, width-1):\n",
" for y in range(1, height-1):\n",
" total += find_cross_at_pos(board, (x, y)) \n",
" ...\n",
" \n",
" return total"
]
},
{
"cell_type": "markdown",
"id": "77466af9-01cd-45e3-b37c-bcaf0e716aab",
"metadata": {},
"source": [
"## Test Patterns"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a8c2b044-3430-42b6-bb94-95d56950bd2c",
"metadata": {},
"outputs": [],
"source": [
"test1 = \"\"\"..X...\n",
"part1_test1 = \"\"\"..X...\n",
".SAMX.\n",
".A..A.\n",
"XMAS.S\n",
Expand All @@ -164,12 +268,12 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 10,
"id": "96c152a4-182b-463e-8dcc-42b20ab9916a",
"metadata": {},
"outputs": [],
"source": [
"test2 = \"\"\"MMMSXXMASM\n",
"part1_test2 = \"\"\"MMMSXXMASM\n",
"MSAMXMSMSA\n",
"AMXSXMAAMM\n",
"MSAMASMSMX\n",
Expand All @@ -184,7 +288,48 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 11,
"id": "0cb79ad0-9479-46cf-aaa3-2d68152e3f77",
"metadata": {},
"outputs": [],
"source": [
"part2_test1 = \"\"\"M.S\n",
".A.\n",
"M.S\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "dd72bd6a-eb4e-4799-8c44-5e0721fd368e",
"metadata": {},
"outputs": [],
"source": [
"part2_test2 = \"\"\".M.S......\n",
"..A..MSMS.\n",
".M.S.MAA..\n",
"..A.ASMSM.\n",
".M.S.M....\n",
"..........\n",
"S.S.S.S.S.\n",
".A.A.A.A..\n",
"M.M.M.M.M.\n",
"..........\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"id": "3c8038fc-40c1-4442-bd9d-59270c178ed1",
"metadata": {},
"source": [
"## Parse input"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9cf5cd67-c278-4037-8002-bc6b9308193d",
"metadata": {},
"outputs": [],
Expand All @@ -196,8 +341,12 @@
"with open(FILENAME, 'r') as file:\n",
" input_text = file.read()\n",
"\n",
"# input_text = test1 # DEBUG\n",
"# input_text = test2 # DEBUG\n",
"# input_text = part1_test1 # DEBUG\n",
"# input_text = part1_test2 # DEBUG\n",
"\n",
"# input_text = part2_test1 # DEBUG\n",
"# input_text = part2_test2 # DEBUG\n",
"\n",
"# ic(input_text)\n",
"\n",
"lineno = 0\n",
Expand All @@ -209,7 +358,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 14,
"id": "fb02256a-b564-4242-9635-6f6e276a9577",
"metadata": {
"scrolled": true
Expand All @@ -221,14 +370,22 @@
"# ic(checkerboard)"
]
},
{
"cell_type": "markdown",
"id": "a7d266d2-d9c7-4a79-9df3-3574da1d0ba3",
"metadata": {},
"source": [
"## Part 1: Find XMAS"
]
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 15,
"id": "7fe4596c-be12-4002-824a-3836cb351d5c",
"metadata": {},
"outputs": [],
"source": [
"# test1\n",
"# part1_test1\n",
"#\n",
"# find_word_across(\"XMAS\", checkerboard, (0,1), \"E\")\n",
"#\n",
Expand All @@ -237,25 +394,14 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 16,
"id": "db67d781-8902-44a2-97c3-d9402a8686eb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# test2\n",
"# part1_test2\n",
"#\n",
"find_word_across(\"XMAS\", checkerboard, (3,0), \"NW\")\n",
"# find_word_across(\"XMAS\", checkerboard, (3,0), \"NW\")\n",
"# find_word_across(\"XMAS\", checkerboard, (9,6), \"S\")\n",
"# find_word_across(\"XMAS\", checkerboard, (5,9), \"E\")\n",
"# find_word_across(\"XMAS\", checkerboard, (9,6), \"S\")\n",
Expand All @@ -266,7 +412,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 17,
"id": "f3764b52-eec3-44b4-baee-90fff6e6a837",
"metadata": {},
"outputs": [
Expand All @@ -283,7 +429,7 @@
"2642"
]
},
"execution_count": 43,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -293,17 +439,38 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b646299e-fb4e-4b39-bd1e-cf0a15421ebe",
"cell_type": "markdown",
"id": "e87a9ec5-652a-4168-8fbf-9eb0fca73e3d",
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": []
"source": [
"## Part 2: Find X-MAS"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "6848e4af-67c7-4289-8f28-0a227aaa670d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1974"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"find_cross_in_board(checkerboard)"
]
}
],
"metadata": {
Expand Down

0 comments on commit d233e9e

Please sign in to comment.