From 84148b1472e19813833a4cb9f543108f3428fdbb Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Fri, 6 Dec 2024 07:43:59 +0100 Subject: [PATCH] feat(solutions/gmacario): day06 Part 1 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day06-jupyter/day06.ipynb | 484 ++++++++++++++++++ .../day06-jupyter/input_day06_gmacario.txt | 130 +++++ 2 files changed, 614 insertions(+) create mode 100644 solutions/gmacario/day06-jupyter/day06.ipynb create mode 100644 solutions/gmacario/day06-jupyter/input_day06_gmacario.txt diff --git a/solutions/gmacario/day06-jupyter/day06.ipynb b/solutions/gmacario/day06-jupyter/day06.ipynb new file mode 100644 index 0000000..7c56d60 --- /dev/null +++ b/solutions/gmacario/day06-jupyter/day06.ipynb @@ -0,0 +1,484 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673", + "metadata": {}, + "source": [ + "# AoC 2024 - Day 6\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "from icecream import ic" + ] + }, + { + "cell_type": "markdown", + "id": "f3458754-aa32-44e5-b229-4f519108459e", + "metadata": {}, + "source": [ + "## Part 1" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b8ac6332-ebd1-424b-a781-df1771e81be8", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "test_input = \"\"\"....#.....\n", + ".........#\n", + "..........\n", + "..#.......\n", + ".......#..\n", + "..........\n", + ".#..^.....\n", + "........#.\n", + "#.........\n", + "......#...\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "with open(\"input_day06_gmacario.txt\", 'r') as file:\n", + " input_text = file.read()\n", + "\n", + "# input_text = test_input # DEBUG\n", + "\n", + "# ic(input_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6938a781-bdb9-4c95-b56a-b990c68c448f", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| map_width: 130, map_height: 130\n" + ] + } + ], + "source": [ + "lab_map = list()\n", + "for l in input_text.split():\n", + " # ic(l)\n", + " # l = [l[ch:ch+1] for ch in range(len(l))]\n", + " map_row = []\n", + " for k in range(len(l)):\n", + " ch = l[k:k+1]\n", + " assert ch in [\".\", \"^\", \"#\"], ic(\"ERROR: Unknown ch\", ch)\n", + " map_row.append(ch)\n", + " lab_map.append(map_row)\n", + "\n", + "map_width = len(lab_map[0])\n", + "map_height = len(lab_map)\n", + "# ic(lab_map)\n", + "ic(map_width, map_height)\n", + "assert all([len(row) == map_width for row in lab_map])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "aa1580c1-3f81-4779-ad48-d6b12e9fddac", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "def count_element(el):\n", + " \"\"\"\n", + " Returns how many times el is found in lab_map\n", + " \"\"\"\n", + " return sum([row[c] == el for row in lab_map for c in range(map_width)])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "541d53ed-3b6c-46e5-9fb1-77792ee3fd16", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| [count_element(ch) for ch in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"]]: [16082, 1, 0, 0, 0, 817, 0]\n" + ] + }, + { + "data": { + "text/plain": [ + "[16082, 1, 0, 0, 0, 817, 0]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ic([count_element(ch) for ch in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"]])" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "42eb54db-e0d9-4aa1-9e94-96ae895e9386", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "def sanity_checks(lab_map):\n", + " map_width = len(lab_map[0])\n", + " map_height = len(lab_map)\n", + " # ic(lab_map)\n", + " # ic(map_width, map_height)\n", + " assert all([len(row) == map_width for row in lab_map])\n", + " assert all([row[c] in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"] for row in lab_map for c in range(map_width)])\n", + " assert sum([row[c] in [\"^\", \"V\", \"<\", \">\"] for row in lab_map for c in range(map_width)]) == 1\n", + "\n", + "sanity_checks(lab_map)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "dd102a37-15be-4067-ab1e-e2f749904754", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "def find_guard(lab_map):\n", + " \"\"\"\n", + " Return (rownum, colnum, orientation)\n", + " \"\"\"\n", + " r = 0\n", + " for row in lab_map:\n", + " c = 0\n", + " for ch in row:\n", + " if ch == \"^\":\n", + " return (r, c, \"UP\")\n", + " elif ch == \"V\":\n", + " return (r, c, \"DOWN\")\n", + " elif ch == \"<\":\n", + " return (r, c, \"LEFT\")\n", + " elif ch == \">\":\n", + " return (r, c, \"RIGHT\")\n", + " c += 1\n", + " r += 1\n", + " assert False" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "34bfe1f0-fceb-401e-8e27-50be34dcddaa", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "def move_one_step(lab_map):\n", + " \"\"\"\n", + " Return True if moving out of map\n", + " \"\"\"\n", + " map_width = len(lab_map[0])\n", + " map_height = len(lab_map)\n", + " (r, c, d) = find_guard(lab_map)\n", + " (new_r, new_c, new_d) = (r, c, d)\n", + " if d == \"UP\":\n", + " if r == 0:\n", + " return True\n", + " new_r = r - 1\n", + " if lab_map[new_r][new_c] == \"#\":\n", + " ic(\"Block found in front of\", (r, c), \"move RIGHT\")\n", + " lab_map[r][c] = \">\"\n", + " new_d = \"RIGHT\"\n", + " else:\n", + " lab_map[r][c] = \"X\"\n", + " lab_map[new_r][new_c] = \"^\"\n", + " elif d == \"RIGHT\":\n", + " if c == map_width-1:\n", + " return True\n", + " new_c = c + 1\n", + " if lab_map[new_r][new_c] == \"#\":\n", + " ic(\"Block found in front of\", (r, c), \"move DOWN\")\n", + " lab_map[r][c] = \"V\"\n", + " new_d = \"DOWN\"\n", + " else:\n", + " lab_map[r][c] = \"X\"\n", + " lab_map[new_r][new_c] = \">\"\n", + " elif d == \"DOWN\":\n", + " if r == map_height-1:\n", + " return True\n", + " new_r = r + 1\n", + " if lab_map[new_r][new_c] == \"#\":\n", + " ic(\"Block found in front of\", (r, c), \"move LEFT\")\n", + " lab_map[r][c] = \"<\"\n", + " new_d = \"LEFT\"\n", + " else:\n", + " lab_map[r][c] = \"X\"\n", + " lab_map[new_r][new_c] = \"V\"\n", + " ...\n", + " elif d == \"LEFT\":\n", + " if c == 0:\n", + " return True\n", + " new_c = c - 1\n", + " if lab_map[new_r][new_c] == \"#\":\n", + " ic(\"Block found in front of\", (r, c), \"move UP\")\n", + " lab_map[r][c] = \"^\"\n", + " new_d = \"UP\"\n", + " else:\n", + " lab_map[r][c] = \"X\"\n", + " lab_map[new_r][new_c] = \"<\"\n", + "\n", + " # ic(\"Moving from\", (r, c, d), \"to\", (new_r, new_c, new_d))\n", + " sanity_checks(lab_map)\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d2116e3e-1a3c-4173-9371-6ede3dbe62fa", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| find_guard(lab_map): (52, 72, 'UP')\n" + ] + }, + { + "data": { + "text/plain": [ + "(52, 72, 'UP')" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ic(find_guard(lab_map))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "98afd4ce-4cc8-4da7-89d7-4a89901cb02e", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "# Part 1: " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "97e0ec3c-528f-4982-a2a1-88a93a534dee", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "# ic(lab_map)\n", + "# move_one_step(lab_map)\n", + "# ic(lab_map)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b81767bf-570e-4cfb-873f-9cc1ac535658", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'PART1 RESULT', count_element(\"X\") + 1: 4977\n" + ] + }, + { + "data": { + "text/plain": [ + "('PART1 RESULT', 4977)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "while not move_one_step(lab_map):\n", + " # ic(\"ONE STEP FURTHER:\", count_element(\"X\"))\n", + " ...\n", + "\n", + "ic(\"PART1 RESULT\", count_element(\"X\") + 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a366015-d692-4bf7-aa35-1f250d022cb2", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "ic(lab_map)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "627da66e-1d88-46c0-ad51-873856aca8a9", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "count_element(\"X\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "40e268ba-dd02-4197-ad83-fb66316f35a3", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [ + "# TODO" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "caeced21-0e4c-4ac2-8e08-d6941238066e", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", + "metadata": {}, + "source": [ + "## Part 2" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/solutions/gmacario/day06-jupyter/input_day06_gmacario.txt b/solutions/gmacario/day06-jupyter/input_day06_gmacario.txt new file mode 100644 index 0000000..be34e79 --- /dev/null +++ b/solutions/gmacario/day06-jupyter/input_day06_gmacario.txt @@ -0,0 +1,130 @@ +................................##..........................................................................#.......#............. +...#.......................................................................................................#...................... +.#.....................#.............................................#................#..................#...#..#.#..#...#........ +.#........................#....#.#.#..........................................#...............................................#... +..................#.......................................................#.......#.............#................................. +........................................#...................................#.........#......#...............#......#......#...... +......#...#.......#..............#.....#..#....................#.................................................#......#......... +......................#...................#....#....................................#.#........................#.........#...#.... +....................#..........................................................................................#.................. +..#.........................#.............#...............#....................................................................... +.#...............................#...............................#...#................#.........#......................#.......... +...#.....................................#.........#.............#...................#.......#........................#......#.... +#...........................#............................#.............................#........................................#. +.........................#................#...........#..........................#.#.............................................. +...........#.#.......#...#.............#.................................#............................................#...#....... +....#.......................#.......#..........#....................................#...............................#.#..#........ +....#............#.......#.............................#..................#......#......#..............#.............#.....#.#.... +...................................#..............#............#...#..............................................#............... +..............................#....#.#........#......................#...................#........#..#............................ +....................................#.#....#.............................................#....#......#...................#........ +..................##..................#........#.....#.........................................#.......#............#............. +.............#......................................................##...................................................#......#. +#......#................#.......................##...............#.....#.......................#..#.........#.............#...#... +.............................................................................................................#.....#.............. +.......#..........#...............#.....##......#.......#.................#............#.......................................... +.......#.............#.........................................................#................................................#. +...........#.......................#.....#...............#..............#........#.....#.........#.........................#...... +.........#........#...............#................................................................#......#..........#............ +..............#..................#...#...............................#.#.................................#.............#........#. +.....................#.......#..............................#........................#...........#....#.................#.......#. +..........#..................................#......#..#................##.........................................#..........#... +.....................#.....#.........#.....#...............#..........................................#.........................#. +..........#...#..............................#...................#.........#........................................#............. +.......#.........................................................................#..........#............#..#..........#.......... +.....#..#..#...............#....................#..............#...........#.....#.....................#.......................... +................................................#.............................................................................#... +................#.......................................................................................#......................... +.............#...#.......................#..................................................#....#................................ +...............................#.......#.#.............................................#....................#....#................ +......#....................................................#.......#......#.................................................#..... +...........................................................#..........................#.#.....#................#.................# +....#....#............#.........................#..#.........................#..................#......#...#.........##........... +...#..................#................................#......#.............#.#............................#...................... +..........................#.............................................................................##........................ +..............................................................................#.................#..........##..................... +....#.....................#.................#......................#...........#.........................................#........ +..................................#..#....#.................................................................#............#.....#.# +.#.........................#.........................#....................#......................................#................ +................##...........................#.................................................................................... +.........#........................................#...........................#........#.........#.......#......#.......#...#...#. +.#..................................................................................#.........#..................................# +....#...............#..........................#.....#....................................................#....................... +.......................#.............................#......#...........^........#................................................ +#.#..#.....#...................##..........#...................................................................................#.. +..#....#.....................................................#...#.............#....#............#..#............................# +........................#..........#.....#............#...........#...............#............#....#.................#........#.. +.................................#.............#...#..................#................................................#.......... +...........#............................................................................#................#....................#... +..#.........#.............................#............................#....................#....................#................ +.................................#..#................#................#........................................................... +.......................#.......................................#........................#................#......#...........#..... +#..............#.......#.........#...#.....#......#.....................#...#....#.......................................#........ +.........#.#.............................#....#.......#..........................................................#................ +...#............................................................................................#................................. +....................#...................#....................#..............#......#..#.............#............................. +...........#.....................................................##............................................................... +.....#......#...............................................................................#........#...................#........ +..............................................#.............#....#................................................................ +......................#.....................#........................................................#...#.........#..#.....#..... +..#...............................................................#..#....................#.#..................................... +............................................#...............#.....#.....#...#..#.................................................. +.#...........................................................................#.................................................... +.............................................................................................................#.................... +#....................................................#..............##.......................................#.................... +#..#....................#.......#...................................................................#...#......................... +..#........................................#............#...............................................................#......... +...............#...........................#...................#....#......#..................................#....#...........#.# +..........##........#.........................#.......#..#.........#...............#........#........#..........#................. +....##.......................................#...........#...................#......#............................................. +..............................#.........#............................#.............................................#.............. +......#........#...........................#..................................#.....#............................................. +...............#.........#.....#.............#.............#...............#.#...............................................#.... +........................................#...............................................................................##.#...#.. +................#.......#.....#........................................................................#........................#. +....................................................................................#.........................................#... +........................................................................................#...#..................................... +........................#.#.........................................................................................#......#....## +......#.........................#.............#................................................................................... +................#...........................#..............#...#..........................#.......#......##.................#..#.. +....................................................#.#.................................#....#.................................... +#..................................#...............................................#......#.#.............#....................... +.....#..........#.....................................................................................#.#.......#............#.... +..#.........................................................#...................#..................................#..###.#.....#. +...#.#..........#.........#..#......................#..##...#.......#..#.#.#...............#.....#....#........................... +...........#.....#...........#........................................................#..................#........................ +.................#........#.......#...................#...#.......#.....#........#..#............................................. +...............................................#.......................#..................#....................................... +....#...................#.#......#............#...................#..........#..##................................................ +....#........................#...........................................................#..................#..#.............#.... +............#...............................................#..................................................................... +.........#.......#...................#...........#...............#....................#................#...#..#.......#........... +...............#...........................................................................#.....................#................ +.............#....#.........................................................#............#....#.#....#.....#..#................#.. +..#...............#.........................................................#........##.....#...#.........................#....... +..........................#.....................................#.................................#............................... +..#.............#...#...............................#.................................................................##.......... +......#........#........................#..........................#..............................................#............... +......#.........................#..#.............................................................................................# +..................................................#..................#.....#...........#....#...#..........#................#..... +....#.....................................#..........#.....#......#..........#.................................................... +...............................#...#...###.#.............................................................#...................#.... +...#...................................#.....#...#....................#........................#..#.......................#......# +....#...#........#...................#..........#......................................#............#............................. +#.................................#.....................#.................#................#...#.#................................ +.................................#.......#...........#..#...#......#.......#.........................#............................ +.......................................##..........................#..................................................#........... +......#............#.##.........#...............#..........#....#.......................................#..#.................#.... +.....#............................#.....#..................................................#.......#......................#....... +.............#.......................................................#...#...#....#...................#........................... +......#.................#.#................................#..#...............#.............................#....#...........#.... +...............#...##......#.............................#.......................................#................................ +#..................................................#.........#....#....#.....#...................#.#....#.....#................... +......#...#...##.................................................................#............................#...............#... +.#..................................#.#...............#................................#...#...................................... +.........#.......#...#.......................#..................................................#.#................#.............. +..................#............................#.....#.....#........#.....................#........................#.......#..#... +..#.............................#.............#.#................................................#.#.##....................#...... +.......#.#......................#.#.....................................................##.............#.......................... +..............#...#........................#................................#.........#.......................#..........#........ +.......................#...............................#..#.........#.............................................................