From 02136326b334b3b37319696e271cb52653cbee5b Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Wed, 4 Dec 2024 17:55:16 +0000 Subject: [PATCH 01/13] wip: gmacario solution for day04 Signed-off-by: Gianpaolo Macario --- .../day04-jupyter/day04-input-gmacario.txt | 140 +++++++ solutions/gmacario/day04-jupyter/day04.ipynb | 347 ++++++++++++++++++ 2 files changed, 487 insertions(+) create mode 100644 solutions/gmacario/day04-jupyter/day04-input-gmacario.txt create mode 100644 solutions/gmacario/day04-jupyter/day04.ipynb diff --git a/solutions/gmacario/day04-jupyter/day04-input-gmacario.txt b/solutions/gmacario/day04-jupyter/day04-input-gmacario.txt new file mode 100644 index 0000000..f3d8fde --- /dev/null +++ b/solutions/gmacario/day04-jupyter/day04-input-gmacario.txt @@ -0,0 +1,140 @@ +SAMMSSSSXSAMXMSMASMMSMSSMMSSXMASMAMMAMSSMSMSAXSSXMXMSASXMSAMXSMMXAMXMMSMSSMXSAMXMMASMSMSMMMAXAAXXASXMMXMSSXMASMMSMXAMSMMMMMAMXSASAMXMMXSAMXM +SAAXAASAASXAAAAAMAAXMAAAMSAAAXAXMAXMSASAASAXAMAMXMSMMAXMAXAMXXAASMSMMAAXXAASASXSXSAMXSAMXAXMSMSMSMMASAMXMSXXAAMSAMMMSMMMAAMAMAXAMXSAMXMAMMAM +SMMMMSMMMMMSMSXSSSSMMMMMMMMSMMASMXXAMXSXMMMMSAMXAMAAMMMAMSSMMSMMSAAXMSSSSMXMAAXSAMXSAMMMMSXXAAAAAAMMMSSMAMMSSMMSASXMAAMSSXSASMMXMASASMAMMMAM +SAAXXMASAXAXMMMXAAXASAXXAMXXXXMMASMXMASAMXXAMSXSSSSSMAXAMXXAAAAAMXMMAAAAXXXMMSAMXMMMMMSAAMXSMSMSMSMMAXXMAXMAMAASXMASXSMAMXSASMSSMASXMXSXSSSS +SSMSSXASXMXSASAMMMMMAAXSAMXXMASMAMMAMXXXMMMMSMXAAMAAXXSSSMSMMSAXSA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MMASXSMSXXXAMMMMASAAMXAXSAMMMXAAAMXXASASMMSXXMAXAAAXMAMMMMMAMAMXSMAMXMMSXMMAAMMMASXMAM +MSMMAMMMMMXAAXMMXAXAXXAAAAXAMXXAXAAAAXAASAMXMAMAAMAMAMASMMAXAMXMAMXAXXAMMSAMXMSASXXSMSMSXMMMASXAAASMMMSMSMSASXASAMMSSSXMASMSMMASMMSSSMMSMXAA +AAAXXMASXSMSSSSMSXSMMMMMMMMSMMMMSXMMSMSMXMXXXSXMXMXMXSXMXXXMAMXMASMSSMSSXMXSXASAMMXAMXMXXAXMXMMMMXMAMXXAXAXAXMAXAASXMMASXMXXASAMMXAAXAMXASMS +SSSMSMASAAAXMMMASMSASASMSMAMAMMXSMMXXAMXAMSMMMXMASXMAMMMMMMSXMXSAAAAAMAMXXAMMMMAMXMMMSASMSMSMMASXMSMMAMMMMMMMSMSSMMASMMMMMSMMMMMSMMSSSMSMMAA +MAAAXMAMMMMMAAMMMASASASAAMASAMAAMASAMSSSXSAAAXASXSAMXSAAAAMMMAAMAMMMSMASMMXSAASAMAAXXMAMMAAAASASAAAAMASAAAAAAAXXAASAMXAAAAAAAXAAAXAAMAMXAMXM +MSMMMMMSAMXSSMSXMMMXMXMMMSMSMSXXSSMXSAMXSSMSMSMSASMMSSMSSSXAXMXSXMASMXMSXXASXMSXMAMSAMXMSXSSXMSSMMMASMSXSSSMSSSSSMMMSXSSSSSSMSMSSSMXSSMSMMAX diff --git a/solutions/gmacario/day04-jupyter/day04.ipynb b/solutions/gmacario/day04-jupyter/day04.ipynb new file mode 100644 index 0000000..02d065e --- /dev/null +++ b/solutions/gmacario/day04-jupyter/day04.ipynb @@ -0,0 +1,347 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673", + "metadata": {}, + "source": [ + "# AoC 2024 - Day 4\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", + "metadata": {}, + "outputs": [], + "source": [ + "from icecream import ic" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8c9a122d-1ba9-4bfd-b131-ee8e69c7d09f", + "metadata": {}, + "outputs": [], + "source": [ + "movements = {\n", + " \"N\": ( 0, 1), # Move UP\n", + " \"S\": ( 0, -1), # Move DOWN\n", + " \"E\": ( 1, 0), # Move EAST\n", + " \"W\": (-1, 0), # Move WEST\n", + " \"NE\": ( 1, 1),\n", + " \"NW\": (-1, 1),\n", + " \"SE\": ( 1, -1),\n", + " \"SW\": (-1, -1),\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "992b746b-12f1-4576-ae26-f3c5d379c844", + "metadata": {}, + "outputs": [], + "source": [ + "def find_word_across(word, board, position, orientation):\n", + " \"\"\"\n", + " position: (x, y) - (0, 0) is lower left\n", + " \n", + " Return 1 if word was found, otherwise 0\n", + " \"\"\"\n", + " \n", + " # ic(word, board, position, orientation)\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", + " pos = list(position)\n", + " for char in word:\n", + " # ic(pos, char)\n", + " board_char = board[height - pos[1] - 1][pos[0]]\n", + " if board_char != char:\n", + " # ic(f\"DEBUG: Unmatch {char} and {board_char} at {pos}\")\n", + " return 0\n", + " # Advance to next character\n", + " move = movements[orientation]\n", + " # ic(\"Advance of\", move)\n", + " pos[0] += move[0]\n", + " if pos[0] < 0 or pos[0] >= width:\n", + " return 0\n", + " pos[1] += move[1]\n", + " if pos[1] < 0 or pos[1] >= height:\n", + " return 0\n", + "\n", + " ic(f\"DEBUG: {word} match starting at {position}, orientation={orientation}\")\n", + " return 1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "2672708d-3eec-4410-80bd-2e5cb40f6ab3", + "metadata": {}, + "outputs": [], + "source": [ + "def find_word_at(word, board, position):\n", + " \"\"\"\n", + " Find a word on board starting at coordinate position=(x,y)\n", + " \n", + " position: (x, y) - (0, 0) is lower left\n", + " \n", + " Return number of occurrencies found\n", + " \"\"\"\n", + " \n", + " # ic(word, board, position)\n", + "\n", + " total = 0\n", + " total += find_word_across(word, board, position, \"N\")\n", + " total += find_word_across(word, board, position, \"S\")\n", + " total += find_word_across(word, board, position, \"E\")\n", + " total += find_word_across(word, board, position, \"W\")\n", + " total += find_word_across(word, board, position, \"NE\")\n", + " total += find_word_across(word, board, position, \"NW\")\n", + " total += find_word_across(word, board, position, \"SE\")\n", + " total += find_word_across(word, board, position, \"SW\")\n", + " \n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8d23c8b5-785f-4bfb-8702-f16a064d607b", + "metadata": {}, + "outputs": [], + "source": [ + "def find_word_in_board(word, board):\n", + " \"\"\"\n", + " Find a word on board\n", + " Return number of occurrencies found\n", + " \"\"\"\n", + "\n", + " # ic()\n", + " # ic(word, board)\n", + "\n", + " total = 0\n", + " height = len(board)\n", + " width = len(board[0])\n", + "\n", + " ic(width, height)\n", + " for x in range(width):\n", + " for y in range(height):\n", + " total += find_word_at(word, board, (x, y))\n", + "\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a8c2b044-3430-42b6-bb94-95d56950bd2c", + "metadata": {}, + "outputs": [], + "source": [ + "test1 = \"\"\"..X...\n", + ".SAMX.\n", + ".A..A.\n", + "XMAS.S\n", + ".X....\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "96c152a4-182b-463e-8dcc-42b20ab9916a", + "metadata": {}, + "outputs": [], + "source": [ + "test2 = \"\"\"MMMSXXMASM\n", + "MSAMXMSMSA\n", + "AMXSXMAAMM\n", + "MSAMASMSMX\n", + "XMASAMXAMM\n", + "XXAMMXXAMA\n", + "SMSMSASXSS\n", + "SAXAMASAAA\n", + "MAMMMXMMMM\n", + "MXMXAXMASX\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9cf5cd67-c278-4037-8002-bc6b9308193d", + "metadata": {}, + "outputs": [], + "source": [ + "checkerboard = list()\n", + "\n", + "FILENAME = \"day04-input-gmacario.txt\"\n", + "\n", + "with open(FILENAME, 'r') as file:\n", + " input_text = file.read()\n", + "\n", + "# input_text = test1 # DEBUG\n", + "input_text = test2 # DEBUG\n", + "# ic(input_text)\n", + "\n", + "lineno = 0\n", + "for line in input_text.splitlines():\n", + " lineno += 1\n", + " # ic(lineno, line)\n", + " checkerboard.append(list(line))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "fb02256a-b564-4242-9635-6f6e276a9577", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# Sanity Checks\n", + "\n", + "# ic(checkerboard)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "7fe4596c-be12-4002-824a-3836cb351d5c", + "metadata": {}, + "outputs": [], + "source": [ + "# test1\n", + "#\n", + "# find_word_across(\"XMAS\", checkerboard, (0,1), \"E\")\n", + "#\n", + "# find_word_at(\"XMAS\", checkerboard, (1,0))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "db67d781-8902-44a2-97c3-d9402a8686eb", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 6), orientation=S'\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# test2\n", + "#\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", + "#\n", + "# find_word_at(\"XMAS\", checkerboard, (9,9))\n", + "# find_word_at(\"XMAS\", checkerboard, (5,9))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f3764b52-eec3-44b4-baee-90fff6e6a837", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| width: 10, height: 10\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (0, 4), orientation=NE'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (0, 5), orientation=E'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (1, 0), orientation=NE'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (3, 0), orientation=NE'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (4, 8), orientation=W'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (4, 9), orientation=SE'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 0), orientation=E'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 0), orientation=NE'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 0), orientation=NW'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 9), orientation=E'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (6, 4), orientation=NW'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (6, 5), orientation=N'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (6, 5), orientation=W'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 0), orientation=N'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 0), orientation=NW'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 6), orientation=S'\n", + "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 6), orientation=SW'\n" + ] + }, + { + "data": { + "text/plain": [ + "17" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_word_in_board(\"XMAS\", checkerboard)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b646299e-fb4e-4b39-bd1e-cf0a15421ebe", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [], + "source": [] + } + ], + "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 +} From f68e67e272676afe6084988c4bd4a8c8692e3974 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Wed, 4 Dec 2024 18:17:09 +0000 Subject: [PATCH 02/13] fix: gmacario day04 Part 1 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day04-jupyter/day04.ipynb | 69 ++++++++------------ 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/solutions/gmacario/day04-jupyter/day04.ipynb b/solutions/gmacario/day04-jupyter/day04.ipynb index 02d065e..f6b20b1 100644 --- a/solutions/gmacario/day04-jupyter/day04.ipynb +++ b/solutions/gmacario/day04-jupyter/day04.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 29, "id": "992b746b-12f1-4576-ae26-f3c5d379c844", "metadata": {}, "outputs": [], @@ -62,7 +62,7 @@ " assert position[1] >= 0 and position[1] < height, f\"{ic(word, board, position)}\"\n", "\n", " pos = list(position)\n", - " for char in word:\n", + " for char in word[:-1]:\n", " # ic(pos, char)\n", " board_char = board[height - pos[1] - 1][pos[0]]\n", " if board_char != char:\n", @@ -78,13 +78,19 @@ " if pos[1] < 0 or pos[1] >= height:\n", " return 0\n", "\n", - " ic(f\"DEBUG: {word} match starting at {position}, orientation={orientation}\")\n", + " # Check last\n", + " board_char = board[height - pos[1] - 1][pos[0]]\n", + " if board_char != word[-1:]:\n", + " # ic(f\"DEBUG: Unmatch {char} and {board_char} at {pos}\")\n", + " return 0\n", + " \n", + " # ic(f\"DEBUG: {word} match starting at {position}, orientation={orientation}\")\n", " return 1" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 30, "id": "2672708d-3eec-4410-80bd-2e5cb40f6ab3", "metadata": {}, "outputs": [], @@ -115,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 31, "id": "8d23c8b5-785f-4bfb-8702-f16a064d607b", "metadata": {}, "outputs": [], @@ -143,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 32, "id": "a8c2b044-3430-42b6-bb94-95d56950bd2c", "metadata": {}, "outputs": [], @@ -158,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 33, "id": "96c152a4-182b-463e-8dcc-42b20ab9916a", "metadata": {}, "outputs": [], @@ -178,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 39, "id": "9cf5cd67-c278-4037-8002-bc6b9308193d", "metadata": {}, "outputs": [], @@ -191,7 +197,7 @@ " input_text = file.read()\n", "\n", "# input_text = test1 # DEBUG\n", - "input_text = test2 # DEBUG\n", + "# input_text = test2 # DEBUG\n", "# ic(input_text)\n", "\n", "lineno = 0\n", @@ -203,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 40, "id": "fb02256a-b564-4242-9635-6f6e276a9577", "metadata": { "scrolled": true @@ -217,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 41, "id": "7fe4596c-be12-4002-824a-3836cb351d5c", "metadata": {}, "outputs": [], @@ -231,24 +237,17 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 42, "id": "db67d781-8902-44a2-97c3-d9402a8686eb", "metadata": {}, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 6), orientation=S'\n" - ] - }, { "data": { "text/plain": [ - "1" + "0" ] }, - "execution_count": 11, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -256,9 +255,10 @@ "source": [ "# test2\n", "#\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", + "# find_word_across(\"XMAS\", checkerboard, (9,6), \"S\")\n", "#\n", "# find_word_at(\"XMAS\", checkerboard, (9,9))\n", "# find_word_at(\"XMAS\", checkerboard, (5,9))" @@ -266,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 43, "id": "f3764b52-eec3-44b4-baee-90fff6e6a837", "metadata": {}, "outputs": [ @@ -274,33 +274,16 @@ "name": "stderr", "output_type": "stream", "text": [ - "ic| width: 10, height: 10\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (0, 4), orientation=NE'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (0, 5), orientation=E'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (1, 0), orientation=NE'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (3, 0), orientation=NE'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (4, 8), orientation=W'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (4, 9), orientation=SE'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 0), orientation=E'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 0), orientation=NE'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 0), orientation=NW'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (5, 9), orientation=E'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (6, 4), orientation=NW'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (6, 5), orientation=N'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (6, 5), orientation=W'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 0), orientation=N'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 0), orientation=NW'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 6), orientation=S'\n", - "ic| f\"DEBUG: {word} match starting at {position}, orientation={orientation}\": 'DEBUG: XMAS match starting at (9, 6), orientation=SW'\n" + "ic| width: 140, height: 140\n" ] }, { "data": { "text/plain": [ - "17" + "2642" ] }, - "execution_count": 12, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } From d233e9e1a60a22d7d955bc43187915584e4c9547 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Wed, 4 Dec 2024 19:47:11 +0000 Subject: [PATCH 03/13] feat: gmacario day04 Part 2 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day04-jupyter/day04.ipynb | 237 ++++++++++++++++--- 1 file changed, 202 insertions(+), 35 deletions(-) diff --git a/solutions/gmacario/day04-jupyter/day04.ipynb b/solutions/gmacario/day04-jupyter/day04.ipynb index f6b20b1..239a679 100644 --- a/solutions/gmacario/day04-jupyter/day04.ipynb +++ b/solutions/gmacario/day04-jupyter/day04.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 3, "id": "992b746b-12f1-4576-ae26-f3c5d379c844", "metadata": {}, "outputs": [], @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 4, "id": "2672708d-3eec-4410-80bd-2e5cb40f6ab3", "metadata": {}, "outputs": [], @@ -121,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 5, "id": "8d23c8b5-785f-4bfb-8702-f16a064d607b", "metadata": {}, "outputs": [], @@ -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", @@ -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", @@ -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": [], @@ -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", @@ -209,7 +358,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 14, "id": "fb02256a-b564-4242-9635-6f6e276a9577", "metadata": { "scrolled": true @@ -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", @@ -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", @@ -266,7 +412,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 17, "id": "f3764b52-eec3-44b4-baee-90fff6e6a837", "metadata": {}, "outputs": [ @@ -283,7 +429,7 @@ "2642" ] }, - "execution_count": 43, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -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": { From b3fc3e2861769b8167613a44398ca8b0bad0e43a Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Thu, 5 Dec 2024 20:13:35 +0100 Subject: [PATCH 04/13] feat(solutions/gmacario): add day05 (Part 1 OK, Part 2 WIP) Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day05-jupyter/day05.ipynb | 359 +++++ .../day05-jupyter/input_day05_gmacario.txt | 1370 +++++++++++++++++ 2 files changed, 1729 insertions(+) create mode 100644 solutions/gmacario/day05-jupyter/day05.ipynb create mode 100644 solutions/gmacario/day05-jupyter/input_day05_gmacario.txt diff --git a/solutions/gmacario/day05-jupyter/day05.ipynb b/solutions/gmacario/day05-jupyter/day05.ipynb new file mode 100644 index 0000000..f34e7f1 --- /dev/null +++ b/solutions/gmacario/day05-jupyter/day05.ipynb @@ -0,0 +1,359 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673", + "metadata": {}, + "source": [ + "# AoC 2024 - Day 5\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", + "metadata": {}, + "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": {}, + "outputs": [], + "source": [ + "test_input = \"\"\"47|53\n", + "97|13\n", + "97|61\n", + "97|47\n", + "75|29\n", + "61|13\n", + "75|53\n", + "29|13\n", + "97|29\n", + "53|29\n", + "61|53\n", + "97|53\n", + "61|29\n", + "47|13\n", + "75|47\n", + "97|75\n", + "47|61\n", + "75|61\n", + "47|29\n", + "75|13\n", + "53|13\n", + "\n", + "75,47,61,53,29\n", + "97,61,53,29,13\n", + "75,29,13\n", + "75,97,47,61,53\n", + "61,13,29\n", + "97,13,75,29,47\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"input_day05_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": "48622bfa-12c9-43ff-9758-28047f26047e", + "metadata": {}, + "outputs": [], + "source": [ + "parse_rules = True\n", + "page_ordering_rules = list()\n", + "page_numbers = list()\n", + "\n", + "for line in input_text.splitlines():\n", + " # ic(line)\n", + " if line == \"\":\n", + " parse_rules = False\n", + " elif parse_rules:\n", + " page_ordering_rules.append(tuple(int(ele) for ele in line.split(\"|\")))\n", + " else:\n", + " page_numbers.append(tuple(int(ele) for ele in line.split(\",\")))\n", + "\n", + "# ic(page_ordering_rules)\n", + "# ic(page_numbers)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "93c4e5b7-4d9d-4b82-a530-fd045e2fc6ab", + "metadata": {}, + "outputs": [], + "source": [ + "def is_sequence_ordered(seq):\n", + " # ic(\"BEGIN is_sequence_ordered\", seq)\n", + " result = True\n", + " for rule in page_ordering_rules:\n", + " if rule[0] not in seq or rule[1] not in seq:\n", + " # ic(\"DEBUG: Skipping(1) rule\", rule)\n", + " continue\n", + " pos1 = seq.index(rule[0])\n", + " pos2 = seq.index(rule[1])\n", + " # ic(pos1, pos2)\n", + " if pos1 > pos2:\n", + " # ic(\"DEBUG: Skipping(2) rule\", rule)\n", + " result = False\n", + " # ic(\"DEBUG: Relevant\", rule, result)\n", + " # ic(\"is_sequence_ordered\", seq, result)\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ca2ece58-99f7-4657-a1e8-b164b5982a5e", + "metadata": {}, + "outputs": [], + "source": [ + "# Sanity Checks with test1\n", + "#\n", + "# assert is_sequence_ordered(page_numbers[0]) == True\n", + "# assert is_sequence_ordered(page_numbers[1]) == True\n", + "# assert is_sequence_ordered(page_numbers[2]) == True\n", + "# assert is_sequence_ordered(page_numbers[3]) == False" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "2f1e2b3c-db37-4ea3-9e24-7cb8cf15dcde", + "metadata": {}, + "outputs": [], + "source": [ + "def middle_element(seq):\n", + " result = seq[len(seq) // 2]\n", + " # ic(\"middle_element\", seq, result)\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d84ac7eb-0518-41bb-a6a7-e1c62086a71d", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'Part 1 result', total: 5329\n" + ] + }, + { + "data": { + "text/plain": [ + "('Part 1 result', 5329)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = 0\n", + "for seq in page_numbers:\n", + " # ic(seq)\n", + "\n", + " if is_sequence_ordered(seq):\n", + " total += middle_element(seq) \n", + "\n", + "ic(\"Part 1 result\", total)" + ] + }, + { + "cell_type": "markdown", + "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", + "metadata": {}, + "source": [ + "## Part 2" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "3e522b8b-8eae-40bf-844f-12ff20957c00", + "metadata": {}, + "outputs": [], + "source": [ + "def reorder_sequence(seq):\n", + " # ic(\"BEGIN reorder_sequence\", seq)\n", + "\n", + " # Consider only the page_ordering_rules which contain all element\n", + "\n", + " # ic(len(page_ordering_rules))\n", + " relevant_ordering_rules = [r for r in page_ordering_rules if r[0] in seq and r[1] in seq]\n", + " sorted(relevant_ordering_rules)\n", + " # ic(len(relevant_ordering_rules), relevant_ordering_rules)\n", + " \n", + " result = list(seq)\n", + "\n", + " # for rule in sorted(page_ordering_rules):\n", + " # if rule[0] not in result or rule[1] not in result:\n", + " # # ic(\"DEBUG: Skipping(1) rule\", rule)\n", + " # continue\n", + " # pos1 = result.index(rule[0])\n", + " # pos2 = result.index(rule[1])\n", + " # # ic(pos1, pos2)\n", + " # if pos1 > pos2:\n", + " # # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n", + " # result[pos1], result[pos2] = result[pos2], result[pos1]\n", + " # ic(\"DEBUG: After swapping\", pos1, \"and\", pos2, \"->\", result)\n", + " # # ic(\"DEBUG: Relevant\", rule, result)\n", + "\n", + " for rule in relevant_ordering_rules:\n", + " pos1 = result.index(rule[0])\n", + " pos2 = result.index(rule[1])\n", + " # ic(pos1, pos2)\n", + " if pos1 > pos2:\n", + " # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n", + " result[pos1], result[pos2] = result[pos2], result[pos1]\n", + " # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n", + " # ic(\"DEBUG: Relevant\", rule, result)\n", + " \n", + " #\n", + " \n", + " # ic(\"reorder sequence\", seq, \"->\", result)\n", + " # assert is_sequence_ordered(result), ic(seq, result)\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d7393bcb-135a-470e-a37b-4149449ef1d8", + "metadata": {}, + "outputs": [], + "source": [ + "# Sanity Checks with test1\n", + "#\n", + "# assert is_sequence_ordered(page_numbers[3]) == False\n", + "\n", + "# ic(reorder_sequence(page_numbers[3]))\n", + "# ic(reorder_sequence(page_numbers[4]))\n", + "# ic(reorder_sequence(page_numbers[5]))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6ed8e5c4-625b-4c4b-be23-5ccbdeaee80e", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| reorder_sequence(page_numbers[0]): [67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]\n" + ] + }, + { + "data": { + "text/plain": [ + "[67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Sanity Checks on real data\n", + "#\n", + "# assert is_sequence_ordered(page_numbers[0]) == False\n", + "ic(reorder_sequence(page_numbers[0]))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9e518d75-370f-495c-9a86-8887562ef15b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'Part 2 result', total: 5896\n" + ] + }, + { + "data": { + "text/plain": [ + "('Part 2 result', 5896)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = 0\n", + "for seq in page_numbers:\n", + " # ic(seq)\n", + "\n", + " if not is_sequence_ordered(seq):\n", + " ordered_seq = reorder_sequence(seq)\n", + " # ic(\"DEBUG: incorrectly-ordered\", seq, \"->\", ordered_seq)\n", + " total += middle_element(ordered_seq)\n", + "\n", + "ic(\"Part 2 result\", total)" + ] + } + ], + "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/day05-jupyter/input_day05_gmacario.txt b/solutions/gmacario/day05-jupyter/input_day05_gmacario.txt new file mode 100644 index 0000000..081af78 --- /dev/null +++ b/solutions/gmacario/day05-jupyter/input_day05_gmacario.txt @@ -0,0 +1,1370 @@ +98|43 +91|38 +91|97 +11|56 +11|72 +11|55 +36|34 +36|73 +36|66 +36|49 +22|69 +22|37 +22|98 +22|36 +22|45 +69|97 +69|85 +69|75 +69|42 +69|45 +69|39 +38|19 +38|33 +38|63 +38|94 +38|69 +38|41 +38|93 +82|22 +82|91 +82|24 +82|76 +82|87 +82|94 +82|45 +82|39 +96|73 +96|37 +96|35 +96|85 +96|45 +96|39 +96|69 +96|62 +96|57 +23|68 +23|86 +23|91 +23|98 +23|47 +23|45 +23|97 +23|89 +23|42 +23|37 +75|39 +75|97 +75|62 +75|42 +75|99 +75|33 +75|29 +75|68 +75|86 +75|53 +75|38 +66|68 +66|85 +66|77 +66|38 +66|94 +66|72 +66|49 +66|86 +66|99 +66|22 +66|29 +66|82 +56|19 +56|69 +56|76 +56|37 +56|24 +56|77 +56|93 +56|94 +56|23 +56|72 +56|55 +56|47 +56|22 +85|67 +85|59 +85|19 +85|99 +85|94 +85|93 +85|43 +85|77 +85|22 +85|63 +85|49 +85|72 +85|34 +85|76 +43|59 +43|33 +43|93 +43|76 +43|96 +43|63 +43|53 +43|69 +43|82 +43|31 +43|22 +43|49 +43|23 +43|77 +43|72 +63|35 +63|89 +63|14 +63|45 +63|47 +63|57 +63|37 +63|19 +63|24 +63|62 +63|98 +63|97 +63|11 +63|69 +63|36 +63|75 +19|75 +19|57 +19|87 +19|45 +19|73 +19|23 +19|47 +19|24 +19|89 +19|97 +19|66 +19|62 +19|69 +19|91 +19|96 +19|39 +19|35 +49|82 +49|33 +49|19 +49|22 +49|67 +49|87 +49|45 +49|96 +49|47 +49|72 +49|53 +49|63 +49|76 +49|98 +49|41 +49|55 +49|23 +49|93 +53|19 +53|82 +53|67 +53|22 +53|69 +53|31 +53|72 +53|87 +53|55 +53|96 +53|77 +53|47 +53|23 +53|45 +53|93 +53|98 +53|76 +53|56 +53|59 +89|77 +89|33 +89|34 +89|97 +89|43 +89|41 +89|29 +89|42 +89|53 +89|86 +89|66 +89|62 +89|59 +89|93 +89|85 +89|14 +89|56 +89|38 +89|68 +89|31 +55|72 +55|75 +55|96 +55|37 +55|93 +55|82 +55|47 +55|24 +55|91 +55|77 +55|23 +55|35 +55|94 +55|22 +55|41 +55|19 +55|45 +55|98 +55|69 +55|59 +55|63 +86|68 +86|49 +86|67 +86|34 +86|59 +86|77 +86|55 +86|72 +86|38 +86|82 +86|63 +86|99 +86|43 +86|22 +86|94 +86|33 +86|85 +86|53 +86|76 +86|29 +86|41 +86|93 +94|24 +94|14 +94|97 +94|42 +94|96 +94|62 +94|23 +94|47 +94|75 +94|36 +94|87 +94|63 +94|35 +94|76 +94|39 +94|91 +94|37 +94|98 +94|73 +94|69 +94|89 +94|19 +94|45 +59|36 +59|94 +59|35 +59|67 +59|69 +59|76 +59|24 +59|72 +59|23 +59|93 +59|41 +59|22 +59|75 +59|63 +59|96 +59|91 +59|45 +59|87 +59|47 +59|98 +59|77 +59|19 +59|37 +59|82 +57|34 +57|59 +57|56 +57|31 +57|85 +57|99 +57|66 +57|97 +57|49 +57|14 +57|55 +57|33 +57|41 +57|62 +57|73 +57|43 +57|42 +57|86 +57|89 +57|53 +57|68 +57|38 +57|11 +57|29 +76|98 +76|23 +76|96 +76|57 +76|89 +76|19 +76|39 +76|42 +76|97 +76|87 +76|24 +76|91 +76|62 +76|73 +76|14 +76|69 +76|75 +76|37 +76|35 +76|47 +76|45 +76|63 +76|36 +76|11 +87|89 +87|47 +87|66 +87|57 +87|91 +87|37 +87|69 +87|73 +87|98 +87|86 +87|35 +87|68 +87|14 +87|39 +87|42 +87|75 +87|11 +87|85 +87|45 +87|34 +87|36 +87|97 +87|24 +87|62 +35|62 +35|86 +35|33 +35|42 +35|36 +35|89 +35|73 +35|39 +35|56 +35|14 +35|99 +35|43 +35|68 +35|31 +35|85 +35|29 +35|34 +35|53 +35|38 +35|66 +35|57 +35|11 +35|97 +35|49 +41|24 +41|72 +41|75 +41|37 +41|35 +41|96 +41|63 +41|87 +41|22 +41|19 +41|23 +41|94 +41|82 +41|67 +41|69 +41|77 +41|91 +41|45 +41|98 +41|47 +41|39 +41|36 +41|93 +41|76 +14|49 +14|56 +14|55 +14|41 +14|31 +14|22 +14|67 +14|38 +14|33 +14|59 +14|29 +14|53 +14|86 +14|85 +14|43 +14|66 +14|82 +14|11 +14|99 +14|62 +14|68 +14|93 +14|77 +14|34 +39|49 +39|38 +39|31 +39|53 +39|33 +39|34 +39|97 +39|62 +39|57 +39|85 +39|42 +39|99 +39|68 +39|66 +39|56 +39|43 +39|14 +39|89 +39|29 +39|59 +39|55 +39|11 +39|73 +39|86 +72|76 +72|98 +72|42 +72|35 +72|87 +72|73 +72|96 +72|45 +72|97 +72|75 +72|39 +72|47 +72|91 +72|24 +72|37 +72|19 +72|69 +72|57 +72|89 +72|36 +72|94 +72|63 +72|14 +72|23 +29|96 +29|23 +29|56 +29|31 +29|98 +29|63 +29|59 +29|19 +29|53 +29|55 +29|77 +29|76 +29|82 +29|45 +29|69 +29|22 +29|93 +29|94 +29|67 +29|87 +29|41 +29|33 +29|72 +29|49 +67|73 +67|63 +67|96 +67|23 +67|57 +67|36 +67|35 +67|91 +67|45 +67|82 +67|24 +67|47 +67|19 +67|39 +67|87 +67|72 +67|37 +67|69 +67|76 +67|75 +67|89 +67|98 +67|22 +67|94 +42|67 +42|55 +42|11 +42|62 +42|97 +42|34 +42|77 +42|14 +42|43 +42|53 +42|68 +42|29 +42|31 +42|38 +42|56 +42|66 +42|59 +42|41 +42|33 +42|86 +42|99 +42|93 +42|49 +42|85 +77|72 +77|69 +77|94 +77|24 +77|93 +77|57 +77|67 +77|82 +77|47 +77|35 +77|39 +77|91 +77|63 +77|23 +77|45 +77|36 +77|76 +77|98 +77|96 +77|22 +77|75 +77|37 +77|87 +77|19 +62|49 +62|33 +62|59 +62|38 +62|99 +62|93 +62|82 +62|72 +62|66 +62|34 +62|77 +62|11 +62|68 +62|55 +62|85 +62|86 +62|67 +62|31 +62|29 +62|41 +62|53 +62|56 +62|43 +62|22 +24|66 +24|85 +24|99 +24|43 +24|36 +24|57 +24|97 +24|38 +24|75 +24|53 +24|14 +24|11 +24|89 +24|42 +24|34 +24|35 +24|39 +24|68 +24|73 +24|29 +24|62 +24|49 +24|86 +24|91 +37|35 +37|43 +37|36 +37|24 +37|62 +37|73 +37|97 +37|38 +37|57 +37|91 +37|11 +37|86 +37|66 +37|29 +37|39 +37|49 +37|89 +37|99 +37|68 +37|14 +37|34 +37|85 +37|42 +37|75 +68|34 +68|67 +68|72 +68|77 +68|53 +68|59 +68|38 +68|96 +68|49 +68|82 +68|63 +68|94 +68|55 +68|43 +68|31 +68|93 +68|22 +68|33 +68|56 +68|41 +68|19 +68|76 +68|99 +68|29 +73|68 +73|99 +73|43 +73|77 +73|53 +73|59 +73|49 +73|31 +73|85 +73|97 +73|55 +73|14 +73|38 +73|89 +73|86 +73|29 +73|33 +73|62 +73|42 +73|66 +73|34 +73|41 +73|11 +73|56 +47|66 +47|11 +47|39 +47|37 +47|42 +47|89 +47|14 +47|62 +47|91 +47|85 +47|35 +47|24 +47|99 +47|86 +47|36 +47|68 +47|29 +47|73 +47|75 +47|34 +47|38 +47|43 +47|57 +47|97 +97|67 +97|99 +97|68 +97|14 +97|59 +97|31 +97|55 +97|56 +97|77 +97|38 +97|86 +97|85 +97|11 +97|82 +97|62 +97|33 +97|53 +97|49 +97|29 +97|66 +97|43 +97|41 +97|34 +97|93 +31|45 +31|72 +31|24 +31|96 +31|69 +31|98 +31|77 +31|67 +31|41 +31|94 +31|93 +31|82 +31|63 +31|23 +31|22 +31|59 +31|37 +31|47 +31|55 +31|91 +31|76 +31|19 +31|87 +31|75 +33|55 +33|76 +33|47 +33|87 +33|31 +33|98 +33|19 +33|24 +33|96 +33|63 +33|94 +33|72 +33|45 +33|37 +33|69 +33|59 +33|41 +33|23 +33|91 +33|93 +33|22 +33|77 +33|67 +33|82 +99|19 +99|93 +99|87 +99|43 +99|33 +99|29 +99|63 +99|55 +99|67 +99|77 +99|96 +99|31 +99|76 +99|22 +99|41 +99|72 +99|23 +99|59 +99|38 +99|94 +99|49 +99|53 +99|56 +99|82 +93|69 +93|36 +93|23 +93|91 +93|72 +93|73 +93|37 +93|45 +93|75 +93|96 +93|35 +93|87 +93|82 +93|94 +93|57 +93|39 +93|63 +93|22 +93|67 +93|19 +93|47 +93|24 +93|98 +93|76 +45|47 +45|39 +45|89 +45|57 +45|97 +45|86 +45|85 +45|14 +45|73 +45|98 +45|35 +45|62 +45|75 +45|66 +45|99 +45|68 +45|24 +45|91 +45|34 +45|36 +45|42 +45|11 +45|37 +45|38 +34|67 +34|82 +34|76 +34|38 +34|31 +34|96 +34|43 +34|53 +34|19 +34|63 +34|22 +34|72 +34|23 +34|56 +34|49 +34|29 +34|77 +34|41 +34|93 +34|94 +34|55 +34|59 +34|33 +34|99 +98|99 +98|91 +98|24 +98|97 +98|39 +98|62 +98|35 +98|85 +98|34 +98|66 +98|89 +98|11 +98|38 +98|37 +98|57 +98|36 +98|75 +98|47 +98|86 +98|14 +98|42 +98|73 +98|68 +91|39 +91|56 +91|14 +91|99 +91|36 +91|35 +91|66 +91|11 +91|43 +91|42 +91|73 +91|75 +91|49 +91|57 +91|86 +91|62 +91|34 +91|89 +91|29 +91|68 +91|85 +91|53 +11|77 +11|34 +11|31 +11|41 +11|94 +11|67 +11|99 +11|93 +11|38 +11|49 +11|82 +11|33 +11|43 +11|85 +11|59 +11|66 +11|22 +11|86 +11|53 +11|29 +11|68 +36|31 +36|33 +36|99 +36|62 +36|11 +36|38 +36|97 +36|85 +36|89 +36|39 +36|57 +36|14 +36|68 +36|43 +36|56 +36|86 +36|42 +36|55 +36|53 +36|29 +22|35 +22|96 +22|91 +22|42 +22|94 +22|19 +22|72 +22|75 +22|63 +22|97 +22|39 +22|76 +22|89 +22|87 +22|24 +22|73 +22|47 +22|57 +22|23 +69|47 +69|34 +69|89 +69|62 +69|14 +69|37 +69|36 +69|99 +69|66 +69|24 +69|91 +69|68 +69|11 +69|35 +69|73 +69|86 +69|98 +69|57 +38|72 +38|23 +38|22 +38|56 +38|53 +38|87 +38|55 +38|82 +38|49 +38|59 +38|43 +38|67 +38|29 +38|76 +38|77 +38|31 +38|96 +82|57 +82|96 +82|42 +82|75 +82|73 +82|37 +82|19 +82|47 +82|23 +82|89 +82|63 +82|72 +82|69 +82|36 +82|98 +82|35 +96|97 +96|14 +96|47 +96|89 +96|86 +96|75 +96|11 +96|24 +96|42 +96|36 +96|66 +96|23 +96|87 +96|98 +96|91 +23|36 +23|62 +23|75 +23|14 +23|66 +23|57 +23|87 +23|85 +23|69 +23|73 +23|24 +23|11 +23|39 +23|35 +75|73 +75|11 +75|56 +75|66 +75|85 +75|89 +75|57 +75|35 +75|43 +75|34 +75|36 +75|49 +75|14 +66|33 +66|31 +66|55 +66|43 +66|53 +66|56 +66|41 +66|93 +66|34 +66|76 +66|67 +66|59 +56|98 +56|87 +56|31 +56|41 +56|33 +56|96 +56|67 +56|59 +56|63 +56|82 +56|45 +85|41 +85|82 +85|55 +85|29 +85|53 +85|38 +85|68 +85|33 +85|31 +85|56 +43|67 +43|55 +43|94 +43|87 +43|56 +43|45 +43|19 +43|41 +43|29 +63|42 +63|39 +63|73 +63|87 +63|91 +63|66 +63|23 +63|96 +19|11 +19|42 +19|36 +19|37 +19|14 +19|86 +19|98 +49|31 +49|77 +49|69 +49|94 +49|59 +49|56 +53|94 +53|33 +53|41 +53|63 +53|37 +89|49 +89|99 +89|11 +89|55 +55|67 +55|87 +55|76 +86|56 +86|31 +94|57 + +98,37,47,35,22,73,76,36,67,39,69,82,45 +72,94,76,96,23,87,69,37,24,91,75,36,57,73,89,42,97 +35,36,57,66,86,38,43,49,53,56,33 +94,63,19,87,98,75,39 +37,98,86,23,42,91,96 +99,38,43,29,49,53,56,33,31,55,59,41,77,67,82,22,72,94,76,63,19,96,23 +34,29,77,33,89,55,97,62,11 +86,38,29,49,33 +98,37,24,35,39,57,89,97,14,62,86,85,38 +93,59,98,94,55,37,82,47,19,33,22,24,96,72,67,77,23 +23,35,63,37,69,98,47,36,96,82,19,91,57,39,89,45,24,22,94 +49,53,56,33,31,55,93,67,82,72,96 +39,97,66,68,43,53,55 +33,55,59,77,93,67,82,72,76,63,19,96,23,69,45,98,47,37,24 +72,35,93,87,47,96,82,41,37,45,23,76,98,24,67,63,59,75,19,94,69,22,77 +87,72,91,67,24,76,47,57,75,22,73,35,19 +36,75,98,73,24,47,87,76,89,45,96,94,35,39,63,69,57,42,22,72,23 +98,47,37,91,35,57,89,97,11,66,86,68,38 +49,86,75,29,68,39,35,43,91,73,38,14,57,97,34,66,11,62,42,53,85,36,89 +76,37,47,94,93,19,45,91,22,36,77,69,98,87,82,75,41 +86,85,68,34,99,38,43,29,49,53,56,33,31,55,59,41,77,93,82,22,72,94,76 +37,94,98,45,19,87,35,93,72,39,77,22,63,47,23,24,91,69,75,96,36 +69,45,91,75,35,36,57,89,14,62,66,86,85,68,34 +41,93,82,94,31,22,96,72,59,38,76,77,67,56,33,19,63,99,29 +24,75,35,39,57,86,38,29,49 +31,55,59,77,82,72,76,63,23,45,98,47,37,24,91 +34,43,49,56,77,67,76 +69,96,91,45,87,22,72,55,67,23,94,59,76,77,24,47,75,93,82,41,98,19,37 +73,42,14,62,11,86,34,99,43,29,49,53,56,33,55,59,41 +85,86,38,56,29,82,76,43,31,72,93,99,33,53,94 +55,85,66,43,49,42,53,77,86,56,89 +97,72,63,19,57,23,89,24,47 +87,69,98,47,75,35,36,39,89,97,14,62,11,85,68 +45,98,93,37,94,57,82,69,35,36,22,63,24,19,39 +49,82,53,63,59,96,72,22,87,29,69,45,23,56,67,76,93,41,94 +33,31,55,77,93,82,22,72,76,19,96,23,87,69,45,37,24 +49,66,68,75,89,36,97,38,57,34,73,86,91,99,29,62,53,11,39 +69,14,86,57,68,34,75,42,62 +14,62,66,85,43,53,56,59,41,77,82 +63,96,23,45,24,75,35,36,89,42,97,14,11 +82,22,76,19,96,98,37,24,75,39,89 +43,53,56,33,59,67,82,22,72,76,19,87,69 +77,76,94,59,41,29,87,63,67,96,72,82,23,33,55,56,69,49,19,43,31 +87,69,91,59,82,45,63,96,67,76,94,23,55,98,37,77,47,75,93 +14,62,11,68,38,43,29,56,55,59,67 +93,67,82,22,72,76,63,19,96,23,87,69,45,47,37,91,75,35,36,39,57 +36,39,89,97,14,62,11,66,86,85,68,38,43,29,49,53,56,33,31 +94,41,37,93,55,76,82,72,77,67,45,96,98,56,87,31,33 +94,53,72,96,82,43,55 +38,43,49,56,33,31,55,41,93,67,22,72,94,76,63,19,96,23,87 +45,98,33,77,47,69,59,87,19,67,23,31,94 +35,24,98,45,91,57,87,37,11,36,89,63,39,75,42,96,23,47,19,73,69 +98,47,37,24,91,35,36,39,57,73,89,42,97,14,62,11,66,86,85,68,34,99,38 +99,22,53,56,82,59,72,33,31,85,63,68,76,34,77,93,43,94,29 +62,49,68,93,56,59,77,85,41,66,86,22,11,82,38 +14,53,97,41,42,66,33,11,85 +45,19,98,73,69,91,82,22,23,96,47,36,75,63,94,72,57,67,24 +63,69,94,45,47,87,72,97,39 +36,66,91,37,29,38,39 +68,56,66,41,62,99,97,67,31,86,49,33,77,29,85,43,34,11,38,55,53 +43,49,31,55,77,93,82,23,69 +39,11,66,68,38 +68,99,38,43,29,49,53,33,59,41,77,93,67,82,22,72,94,76,63 +24,91,75,35,36,57,73,89,42,97,14,62,11,66,86,85,68,34,99,38,43,29,49 +14,29,36,42,37,43,73,34,85,62,99,38,39,11,68,24,91,35,57 +68,39,55,53,31,11,33,86,56,14,62,49,42,97,43 +98,47,36,89,42,14,62,86,68,99,38 +38,56,77,33,53,86,41,22,76 +82,87,72,22,63,75,69,76,24,35,23,73,39,57,47,36,91,89,96,94,45,98,19 +37,75,35,36,57,42,85,68,99,38,29 +91,35,57,73,89,14,11,86,99,49,53 +14,97,45,87,35,73,23,85,66 +75,36,69,91,39,42,68,11,34 +24,91,35,39,89,42,86,85,68,99,38,43,29 +73,89,42,97,14,62,11,66,85,34,99,29,49,53,31,55,59 +34,99,43,53,56,63,96 +35,39,57,62,86,85,99,29,49,53,33 +42,11,68,34,99,38,43,29,53,56,33,31,55,59,41,77,93 +59,22,33,41,77,49,68,38,63,99,29,67,72,94,31,19,56 +77,93,82,94,76,63,19,45,37 +47,14,39,87,75,45,97,36,73,86,42 +38,82,34,59,68,56,67,22,31,55,76,77,41,29,53,99,85,94,43,93,63 +22,69,47,24,75,36,57,89,42 +98,45,37,63,72,94,73,69,47,91,97,42,75 +97,14,62,11,66,86,85,68,99,38,43,29,49,53,56,55,59,41,77,93,67 +67,82,22,72,94,76,63,19,96,23,87,69,45,98,91,75,35,57,73 +53,33,55,59,41,77,67,22,94,69,45,98,47 +35,39,57,73,14,66,34,38,53,56,33 +89,14,66,86,68,34,29,59,77 +19,96,23,69,45,47,91,75,35,73,89,42,97,62,66 +69,24,75,93,22,45,96,39,77 +57,73,42,97,14,62,11,66,86,85,68,34,99,38,43,29,49,53,56,33,31,55,59 +47,91,63,69,35,11,97,73,96 +19,47,37,36,57,89,42,11,66 +56,98,76,82,47,53,59,96,67,41,69,72,55,87,23 +39,91,73,23,35,36,42,98,22,47,94,45,63,89,37,76,75,24,69 +36,39,57,89,42,97,14,62,11,66,86,85,68,34,99,38,43,29,49,53,56,33,31 +72,77,45,94,41,49,82,22,19,29,63,31,96,87,93,53,33,55,59,76,23,69,56 +56,33,31,55,59,41,77,67,82,22,72,94,76,63,96,23,87,69,45,98,37 +87,69,47,75,35,39,57,86,68 +29,49,53,56,33,31,55,59,41,77,93,67,82,22,72,94,76,63,19,96,87,69,45 +66,82,77,85,31,86,34,29,99,94,59,22,33,93,68 +73,35,39,91,68 +93,39,98,72,23,22,82,57,69,45,87 +11,85,34,99,38,29,53,56,31,55,59,41,77,93,82 +47,37,91,36,89,42,97,62,86,85,68,34,43 +72,49,76,38,31,55,93 +75,45,39,36,62,11,98,24,37,73,66,35,97,23,85,42,87,47,86,69,57 +33,59,93,63,19,96,98 +77,99,53,31,38,29,11,62,59,66,85,93,82,67,68,49,56,86,41,43,34 +38,96,31,94,22,19,34,93,63,76,59,53,67 +35,11,97,33,34,29,66 +24,35,36,62,11,85,49 +93,67,22,72,94,76,63,19,96,23,87,69,45,98,47,37,24,91,75,35,36,39,57 +97,86,11,34,36,39,14,35,53,43,49 +19,96,87,69,98,47,24,57,73,89,42,97,62,11,66 +73,38,35,49,86,91,14,34,43,85,53,89,62,75,36,42,29 +69,98,24,37,14,36,68,97,89,39,73,47,87,86,85,75,11,35,62 +23,87,19,98,24,76,57,63,45,47,73,37,14,75,39,97,36,42,91,96,89 +55,93,96,87,47,37,75 +73,11,49,29,53,41,86,62,31 +34,38,43,49,53,56,33,31,59,41,77,93,67,82,22,76,63,19,96 +85,34,53,31,59,93,67,94,63 +42,57,53,36,31,34,11,89,68,73,49 +67,53,72,31,43,38,86,93,41,11,22,56,77,68,99,85,49,59,82,29,33 +77,93,82,76,87,69,98,47,37,24,35,36,39 +29,68,85,11,53,89,38,66,73,62,42,43,55,86,31,56,33,41,59,14,97,49,99 +53,94,87,55,69,23,59,43,63,29,67,41,56,22,96,31,33,77,72,76,93 +37,91,35,36,39,57,73,89,14,62,66,86,85,68,34,38,29 +29,49,53,56,33,31,55,77,93,67,82,22,63,19,96,23,87,69,45 +77,76,98,47,67,53,41,96,63,31,55 +56,86,85,53,41,93,33,55,77,29,99,66,38,43,68,34,94 +37,24,75,36,39,42,97,14,62,11,66,85,34,38,43 +35,73,14,11,43,53,33 +31,62,99,85,66,34,97,29,59,33,57,43,86,42,55,38,11 +69,98,47,24,35,36,39,89,42,97,14,62,66,86,85,68,34 +99,14,31,93,86,62,38,11,42 +63,37,24,39,97,62,11 +59,41,22,94,96,24,35 +98,37,91,75,35,36,39,57,73,89,42,97,14,62,11,66,86,85,68,34,99 +11,33,68,14,86,43,89,49,66,62,73,56,42,31,55,39,53 +36,57,75,66,37,99,35,45,47,86,39,68,89 +69,45,98,47,91,75,35,36,57,73,89,42,97,14,62,11,66,86,85,68,34 +38,43,77,82,22,94,19,96,87 +76,63,23,87,47,37,24,91,75,57,73,42,62 +35,42,97,66,68 +36,57,89,42,62,11,66,86,85,68,34,99,38,43,49,33,31 +49,41,38,67,34,77,55,72,56,43,11,85,99,33,93,31,53,59,29,82,68,22,66 +33,99,43,31,38,89,34,86,97,36,11,14,39 +97,66,85,57,42,24,73,89,39 +11,39,14,66,62,97,91,99,34,75,37,86,38,57,36,85,73,89,24,35,68 +86,42,73,85,43,49,89,31,38,34,62,56,39,99,68,55,97,29,33 +66,73,91,14,89,24,62,35,97,37,29,36,39,38,42,34,86 +53,41,33,55,31,94,59,77,22,98,49 +87,33,43,93,29,53,22,59,94,41,82,76,72,69,49,56,23,31,63,55,77,19,96 +72,76,19,69,45,91,75,35,73 +87,31,33,63,29,94,67,93,53 +98,41,47,67,36,76,91,72,69,87,24,77,96,63,93,35,75 +23,87,69,45,98,47,37,24,91,75,35,36,39,57,73,89,42,97,14,62,11,86,85 +94,76,63,19,96,23,87,45,98,37,24,91,75,35,36,39,73,89,42 +85,57,45,24,75,89,99,14,86,36,62,37,47,34,35,91,98,11,66 +19,75,63,89,73,98,97,24,94,45,23,35,37,36,69,57,14,91,42,87,96,39,47 +56,33,31,55,59,41,77,93,82,76,96,45,98,47,37 +89,42,97,14,62,11,66,86,85,68,34,99,38,43,29,49,53,56,33,31,59,41,77 +94,45,91,96,97,87,76,47,89,39,37,24,35,42,72,19,57,73,36,75,98,69,63 +33,87,93,72,94,77,55,82,23,53,76,49,22,98,63 +34,49,56,33,31,55,59,41,77,93,22,72,94,76,63,19,96 +23,87,37,93,94,96,77,75,19,35,59 +55,59,41,77,93,67,82,22,72,94,76,63,19,96,23,87,69,45,98,47,37,91,75 +19,96,23,69,45,91,35,36,39,57,73,89,97,14,62,11,66 +22,72,19,23,37,24,36,39,73,89,42 +75,39,57,73,89,14,62,11,66,68,34,43,29,53,56 +91,31,47,24,98,67,37,82,23,45,69,87,94,77,55,19,63,93,96,41,59,76,72 +55,93,34,56,99,22,85,49,59,29,66,41,86 +35,36,39,73,42,97,62,11,66,86,68,34,99,38,43,29,49,53,33 +24,36,73,39,98,82,19,94,63,69,76,22,96,75,23,72,37,89,45 +29,62,39,57,99,97,66,85,53,91,11,35,86,34,68,42,75 +53,33,55,67,82,76,63,19,96,23,87,45,47 +38,53,11,59,34,73,33,62,66,99,42,97,29,43,68,89,56,31,86,49,57,14,55 +24,47,76,45,37,72,23,63,89,82,69,19,57,73,87 +24,47,87,89,98,19,66,97,75,57,36,73,11,62,39,96,91 +49,53,56,31,55,41,77,93,82,94,76,19,96,23,87,69,98 +19,72,87,96,63,94,45,39,57,37,82,91,98,67,36,23,93,47,35 +38,66,14,29,62,59,53,97,56,57,43,73,33 +43,29,49,53,56,33,31,55,59,41,77,93,67,82,72,94,76,63,19,96,23,87,69 +45,62,23,14,98,76,24 +49,77,59,53,38,55,89 +23,55,96,24,59,76,69,31,22,67,33,98,19 +37,77,33,45,59,24,96 +45,19,94,23,98,55,93,96,67,37,76,72,33,31,22,47,24,59,82,69,41 +87,47,93,56,59,82,19,76,23,45,31,72,63 +67,49,31,53,66,43,99,14,55,11,77,38,86,41,62 +68,11,75,89,42,49,62,86,24 From 84148b1472e19813833a4cb9f543108f3428fdbb Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Fri, 6 Dec 2024 07:43:59 +0100 Subject: [PATCH 05/13] 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 @@ +................................##..........................................................................#.......#............. +...#.......................................................................................................#...................... +.#.....................#.............................................#................#..................#...#..#.#..#...#........ +.#........................#....#.#.#..........................................#...............................................#... +..................#.......................................................#.......#.............#................................. +........................................#...................................#.........#......#...............#......#......#...... +......#...#.......#..............#.....#..#....................#.................................................#......#......... +......................#...................#....#....................................#.#........................#.........#...#.... +....................#..........................................................................................#.................. +..#.........................#.............#...............#....................................................................... +.#...............................#...............................#...#................#.........#......................#.......... +...#.....................................#.........#.............#...................#.......#........................#......#.... +#...........................#............................#.............................#........................................#. +.........................#................#...........#..........................#.#.............................................. +...........#.#.......#...#.............#.................................#............................................#...#....... +....#.......................#.......#..........#....................................#...............................#.#..#........ +....#............#.......#.............................#..................#......#......#..............#.............#.....#.#.... +...................................#..............#............#...#..............................................#............... +..............................#....#.#........#......................#...................#........#..#............................ +....................................#.#....#.............................................#....#......#...................#........ +..................##..................#........#.....#.........................................#.......#............#............. +.............#......................................................##...................................................#......#. +#......#................#.......................##...............#.....#.......................#..#.........#.............#...#... +.............................................................................................................#.....#.............. +.......#..........#...............#.....##......#.......#.................#............#.......................................... +.......#.............#.........................................................#................................................#. +...........#.......................#.....#...............#..............#........#.....#.........#.........................#...... +.........#........#...............#................................................................#......#..........#............ +..............#..................#...#...............................#.#.................................#.............#........#. +.....................#.......#..............................#........................#...........#....#.................#.......#. +..........#..................................#......#..#................##.........................................#..........#... +.....................#.....#.........#.....#...............#..........................................#.........................#. +..........#...#..............................#...................#.........#........................................#............. +.......#.........................................................................#..........#............#..#..........#.......... +.....#..#..#...............#....................#..............#...........#.....#.....................#.......................... +................................................#.............................................................................#... +................#.......................................................................................#......................... +.............#...#.......................#..................................................#....#................................ +...............................#.......#.#.............................................#....................#....#................ +......#....................................................#.......#......#.................................................#..... +...........................................................#..........................#.#.....#................#.................# +....#....#............#.........................#..#.........................#..................#......#...#.........##........... +...#..................#................................#......#.............#.#............................#...................... +..........................#.............................................................................##........................ +..............................................................................#.................#..........##..................... +....#.....................#.................#......................#...........#.........................................#........ +..................................#..#....#.................................................................#............#.....#.# +.#.........................#.........................#....................#......................................#................ +................##...........................#.................................................................................... +.........#........................................#...........................#........#.........#.......#......#.......#...#...#. +.#..................................................................................#.........#..................................# +....#...............#..........................#.....#....................................................#....................... +.......................#.............................#......#...........^........#................................................ +#.#..#.....#...................##..........#...................................................................................#.. +..#....#.....................................................#...#.............#....#............#..#............................# +........................#..........#.....#............#...........#...............#............#....#.................#........#.. +.................................#.............#...#..................#................................................#.......... +...........#............................................................................#................#....................#... +..#.........#.............................#............................#....................#....................#................ +.................................#..#................#................#........................................................... +.......................#.......................................#........................#................#......#...........#..... +#..............#.......#.........#...#.....#......#.....................#...#....#.......................................#........ +.........#.#.............................#....#.......#..........................................................#................ +...#............................................................................................#................................. +....................#...................#....................#..............#......#..#.............#............................. +...........#.....................................................##............................................................... +.....#......#...............................................................................#........#...................#........ +..............................................#.............#....#................................................................ +......................#.....................#........................................................#...#.........#..#.....#..... +..#...............................................................#..#....................#.#..................................... +............................................#...............#.....#.....#...#..#.................................................. +.#...........................................................................#.................................................... +.............................................................................................................#.................... +#....................................................#..............##.......................................#.................... +#..#....................#.......#...................................................................#...#......................... +..#........................................#............#...............................................................#......... +...............#...........................#...................#....#......#..................................#....#...........#.# +..........##........#.........................#.......#..#.........#...............#........#........#..........#................. +....##.......................................#...........#...................#......#............................................. +..............................#.........#............................#.............................................#.............. +......#........#...........................#..................................#.....#............................................. +...............#.........#.....#.............#.............#...............#.#...............................................#.... +........................................#...............................................................................##.#...#.. +................#.......#.....#........................................................................#........................#. +....................................................................................#.........................................#... +........................................................................................#...#..................................... +........................#.#.........................................................................................#......#....## +......#.........................#.............#................................................................................... +................#...........................#..............#...#..........................#.......#......##.................#..#.. +....................................................#.#.................................#....#.................................... +#..................................#...............................................#......#.#.............#....................... +.....#..........#.....................................................................................#.#.......#............#.... +..#.........................................................#...................#..................................#..###.#.....#. +...#.#..........#.........#..#......................#..##...#.......#..#.#.#...............#.....#....#........................... +...........#.....#...........#........................................................#..................#........................ +.................#........#.......#...................#...#.......#.....#........#..#............................................. +...............................................#.......................#..................#....................................... +....#...................#.#......#............#...................#..........#..##................................................ +....#........................#...........................................................#..................#..#.............#.... +............#...............................................#..................................................................... +.........#.......#...................#...........#...............#....................#................#...#..#.......#........... +...............#...........................................................................#.....................#................ +.............#....#.........................................................#............#....#.#....#.....#..#................#.. +..#...............#.........................................................#........##.....#...#.........................#....... +..........................#.....................................#.................................#............................... +..#.............#...#...............................#.................................................................##.......... +......#........#........................#..........................#..............................................#............... +......#.........................#..#.............................................................................................# +..................................................#..................#.....#...........#....#...#..........#................#..... +....#.....................................#..........#.....#......#..........#.................................................... +...............................#...#...###.#.............................................................#...................#.... +...#...................................#.....#...#....................#........................#..#.......................#......# +....#...#........#...................#..........#......................................#............#............................. +#.................................#.....................#.................#................#...#.#................................ +.................................#.......#...........#..#...#......#.......#.........................#............................ +.......................................##..........................#..................................................#........... +......#............#.##.........#...............#..........#....#.......................................#..#.................#.... +.....#............................#.....#..................................................#.......#......................#....... +.............#.......................................................#...#...#....#...................#........................... +......#.................#.#................................#..#...............#.............................#....#...........#.... +...............#...##......#.............................#.......................................#................................ +#..................................................#.........#....#....#.....#...................#.#....#.....#................... +......#...#...##.................................................................#............................#...............#... +.#..................................#.#...............#................................#...#...................................... +.........#.......#...#.......................#..................................................#.#................#.............. +..................#............................#.....#.....#........#.....................#........................#.......#..#... +..#.............................#.............#.#................................................#.#.##....................#...... +.......#.#......................#.#.....................................................##.............#.......................... +..............#...#........................#................................#.........#.......................#..........#........ +.......................#...............................#..#.........#............................................................. From 564a6a047e75f082f061fdeb7e4ecd03d03fb5d4 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sat, 7 Dec 2024 09:17:28 +0100 Subject: [PATCH 06/13] wip(solutions/gmacario: begin work on day07 Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day07/day07.ipynb | 360 ++++++++ .../gmacario/day07/input_day07_gmacario.txt | 850 ++++++++++++++++++ 2 files changed, 1210 insertions(+) create mode 100644 solutions/gmacario/day07/day07.ipynb create mode 100644 solutions/gmacario/day07/input_day07_gmacario.txt diff --git a/solutions/gmacario/day07/day07.ipynb b/solutions/gmacario/day07/day07.ipynb new file mode 100644 index 0000000..27253ce --- /dev/null +++ b/solutions/gmacario/day07/day07.ipynb @@ -0,0 +1,360 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673", + "metadata": {}, + "source": [ + "# AoC 2024 - Day 7\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", + "metadata": {}, + "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": {}, + "outputs": [], + "source": [ + "test = \"\"\"190: 10 19\n", + "3267: 81 40 27\n", + "83: 17 5\n", + "156: 15 6\n", + "7290: 6 8 6 15\n", + "161011: 16 10 13\n", + "192: 17 8 14\n", + "21037: 9 7 18 13\n", + "292: 11 6 16 20\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| input_lines: ['190: 10 19',\n", + " '3267: 81 40 27',\n", + " '83: 17 5',\n", + " '156: 15 6',\n", + " '7290: 6 8 6 15',\n", + " '161011: 16 10 13',\n", + " '192: 17 8 14',\n", + " '21037: 9 7 18 13',\n", + " '292: 11 6 16 20']\n" + ] + }, + { + "data": { + "text/plain": [ + "['190: 10 19',\n", + " '3267: 81 40 27',\n", + " '83: 17 5',\n", + " '156: 15 6',\n", + " '7290: 6 8 6 15',\n", + " '161011: 16 10 13',\n", + " '192: 17 8 14',\n", + " '21037: 9 7 18 13',\n", + " '292: 11 6 16 20']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Read the puzzle input into a list of strings, one per line\n", + "#\n", + "with open(\"input_day07_gmacario.txt\", 'r') as file:\n", + " input_lines = [line.rstrip() for line in file]\n", + " input_lines = test.splitlines() # Uncomment for debug\n", + "\n", + "ic(input_lines)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "205f3d2b-403d-47cb-8270-145a341b1ecc", + "metadata": {}, + "outputs": [], + "source": [ + "def line_to_test_entry(line: str) -> tuple():\n", + " \"\"\"\n", + " Convert one line of the puzzle input into a tuple()\n", + " - key: the result of the calibration test\n", + " - operands: a list of the operands in the equation\n", + " \"\"\"\n", + " key = int(line.split(\":\")[0])\n", + " operands = list(map(int, list(line.split(\" \")[1:])))\n", + " # ic(key, operands)\n", + " result = (key, operands)\n", + " # ic(line, result)\n", + " return result\n", + "\n", + "# Test\n", + "# ic(line_to_test_entry(input_lines[0]))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d35b2b1a-a493-4c55-8696-a4af4299708a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| test_equations: [(190, [10, 19]),\n", + " (3267, [81, 40, 27]),\n", + " (83, [17, 5]),\n", + " (156, [15, 6]),\n", + " (7290, [6, 8, 6, 15]),\n", + " (161011, [16, 10, 13]),\n", + " (192, [17, 8, 14]),\n", + " (21037, [9, 7, 18, 13]),\n", + " (292, [11, 6, 16, 20])]\n" + ] + }, + { + "data": { + "text/plain": [ + "[(190, [10, 19]),\n", + " (3267, [81, 40, 27]),\n", + " (83, [17, 5]),\n", + " (156, [15, 6]),\n", + " (7290, [6, 8, 6, 15]),\n", + " (161011, [16, 10, 13]),\n", + " (192, [17, 8, 14]),\n", + " (21037, [9, 7, 18, 13]),\n", + " (292, [11, 6, 16, 20])]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test_equations = [line_to_test_entry(l) for l in input_lines]\n", + "\n", + "ic(test_equations)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5cbc3551-7488-4416-9193-e86b85246736", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| total: 121, result: 3267, o: 40, op: '+'\n", + "ic| total: 4840, result: 3267, o: 40, op: '*'\n", + "ic| total: 4867, result: 3267, o: 27, op: '+'\n", + "ic| total: 131409, result: 3267, o: 27, op: '*'\n", + "ic| result: 3267, operands: [81, 40, 27], operators: []\n", + "ic| find_operators(test_equations[1][0], test_equations[1][1]): ()\n" + ] + }, + { + "data": { + "text/plain": [ + "()" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def find_operators(result:int, operands: tuple) -> tuple:\n", + " \"\"\"\n", + " Find the operands which applied to operands produce the result\n", + " Return a list with the operators, or None if no solution is found\n", + " \"\"\"\n", + " operators = list()\n", + "\n", + " total = operands[0]\n", + " # totals = [operands[0]]\n", + "\n", + " for o in operands[1:]:\n", + " for op in ['+', '*']:\n", + " if op == '+':\n", + " total += o\n", + " else:\n", + " total *= o\n", + " ic(total, result, o, op)\n", + " if total == result:\n", + " operators.append(op)\n", + " break\n", + " \n", + " ic(result, operands, operators)\n", + " return tuple(operators)\n", + "\n", + "# ic(find_operators(test_equations[0][0], test_equations[0][1])) # 190\n", + "ic(find_operators(test_equations[1][0], test_equations[1][1])) # 3267" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "03896837-6eb5-4003-a940-bd637c273656", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51de10dc-4093-4fce-bd21-50ef40cdf1dd", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea0b61ee-501f-4893-ad9a-e93fdb317e33", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00be4c47-79f6-4c21-9859-9fc329f871cb", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24c136ce-fe8d-4274-8ed9-4423fa9f52d3", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a57a063b-f310-459e-9c2e-bb921a3d3cf7", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13d150e0-de45-4fc8-b419-195130cee355", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7d3a2de-90fd-428f-9902-3ee5a5eb109b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b47c8d88-92df-4c5d-8063-b94550af37c2", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b11850d1-2907-4176-8098-b26bf45a83a9", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "8a366015-d692-4bf7-aa35-1f250d022cb2", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'solve_part1' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDay 07 Part 1: RESULT:\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43msolve_part1\u001b[49m(lab_map))\n", + "\u001b[0;31mNameError\u001b[0m: name 'solve_part1' is not defined" + ] + } + ], + "source": [ + "print(\"Day 07 Part 1: RESULT:\", solve_part1(lab_map))" + ] + }, + { + "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/day07/input_day07_gmacario.txt b/solutions/gmacario/day07/input_day07_gmacario.txt new file mode 100644 index 0000000..8567b14 --- /dev/null +++ b/solutions/gmacario/day07/input_day07_gmacario.txt @@ -0,0 +1,850 @@ +5055314034: 10 1 489 35 44 14 16 4 +108773: 18 1 121 2 6 38 +22175: 1 73 45 2 35 5 +4076637638: 338 1 6 1 191 7 3 4 34 4 +479231839: 9 7 73 1 6 4 7 8 581 7 2 8 +161312232: 761 852 11 4 8 28 6 +6760600000: 220 40 7 5 878 25 +601121975: 608 91 53 5 5 53 649 +761: 79 2 6 558 42 +84130: 65 90 8 6 9 +52590888326: 7 3 9 3 8 543 1 673 8 4 +21603: 9 80 3 1 +6498240: 9 6 7 32 210 +2420318475: 8 413 85 167 5 81 +6368572316: 885 2 2 599 5 1 6 6 6 5 9 +23428423: 870 8 1 308 581 5 34 9 +17387502: 1 8 50 1 76 67 395 +16073047995028: 9 8 2 6 2 282 5 507 954 +1527631: 2 8 95 6 8 8 152 6 1 9 5 8 +90086454: 32 593 2 1 4 1 6 4 6 45 7 +516078606614: 8 60 6 7 860 59 7 14 +145962: 2 86 847 269 3 6 +81188: 9 8 7 3 386 37 82 2 8 +159171: 50 1 764 817 12 4 +9440342478: 866 326 50 499 67 +177118: 77 846 845 3 18 +389593323: 41 95 2 3 62 46 6 780 3 +2318793: 3 74 9 8 792 +6077575: 421 4 6 84 17 7 +1513923281: 760 3 166 82 4 1 +1368574: 950 17 520 460 2 534 +2602638433: 6 506 58 1 346 262 4 +324285404: 7 4 1 6 6 443 3 4 6 41 1 4 +123419188: 831 275 4 1 8 9 7 9 5 6 +7639222: 3 2 1 50 4 9 823 98 1 2 8 +2165757141719: 93 575 4 8 498 81 5 5 6 +32949456: 7 6 1 1 338 31 9 6 958 7 +373015448: 7 1 8 86 146 2 8 6 545 5 +58401: 50 1 74 9 7 3 672 7 59 +321369974958: 3 5 380 7 3 3 2 5 801 3 9 +1654884263287: 398 42 99 1 72 91 287 +1497933556: 4 89 6 2 41 8 5 558 +14538726514: 726 8 658 699 6 2 514 +653959328: 63 519 95 2 3 27 +12554226440: 8 4 46 8 8 912 828 58 7 +24068829: 2 1 7 9 8 6 3 14 33 6 6 1 +13716449: 1 37 16 449 +70300416144: 1 9 61 300 406 2 8 1 44 +12041328: 60 3 4 839 78 +179748877650: 179 2 5 4 881 6 76 49 +22666025380: 4 230 3 54 99 96 456 4 +74741534: 747 4 14 33 40 61 +194212880: 97 2 201 7 4 8 1 98 7 8 2 +534529044: 7 7 673 8 4 680 7 9 28 9 +688: 4 5 5 23 6 +256747454: 8 3 6 9 9 3 76 266 9 5 2 +61497991: 52 29 759 18 993 +8046070: 4 35 2 833 36 70 +19899933: 6 23 801 6 5 94 4 5 3 2 3 +175019472155: 9 4 84 444 214 25 5 30 +122: 2 12 8 5 5 +7716862272: 8 6 1 9 1 9 782 49 8 4 3 7 +292572: 7 3 314 36 21 1 +9265739: 181 9 828 7 5 8 5 1 38 9 +38166224456: 8 55 930 6 995 144 54 +10633: 461 77 9 972 7 +11448249: 11 359 89 2 51 +6409: 3 2 5 6 252 8 4 3 352 3 +144060624: 7 3 34 1 601 1 27 907 6 +29213: 97 1 3 8 6 +61614560278: 23 4 758 900 98 35 8 +701009838: 786 93 959 2 13 +170675: 51 1 4 446 34 27 6 5 +3590981856: 2 6 77 7 3 6 6 12 99 77 7 +159313258080: 20 933 158 9 79 76 +13950095: 620 774 482 3 524 5 +19276100: 3 828 1 95 773 519 53 +91701711135: 58 300 704 2 527 +19037065502: 29 69 9 1 90 5 46 955 9 +4796722: 841 57 30 1 9 +6491761244154: 9 689 19 5 83 5 68 67 +2288734074: 3 20 351 81 11 35 7 2 +51: 1 7 3 +6703: 5 19 2 7 6 3 8 6 3 678 3 7 +55285: 7 151 96 43 5 673 +1345: 20 5 4 13 323 2 +648683: 90 9 85 8 +647351437: 1 79 2 8 7 568 12 2 3 5 +496420640: 9 34 69 9 710 2 8 +17707: 78 98 93 7 7 +1322647: 1 6 4 7 4 2 8 8 9 8 50 283 +13373109: 2 771 1 865 1 9 5 5 2 5 4 +1771452188: 17 714 52 1 86 +3876558518158: 3 6 5 6 9 2 3 4 271 5 53 8 +121216672: 3 2 8 3 7 6 57 8 4 4 8 674 +99709866569: 778 9 7 32 42 664 4 7 +8132: 61 12 84 5 743 +181628139355: 3 48 7 9 3 5 475 41 5 7 4 +8137536: 2 3 5 7 8 3 7 31 7 2 6 9 +125320123: 16 7 7 13 96 372 8 123 +3928122: 7 3 5 97 4 4 6 389 +16010345: 8 25 8 95 1 5 590 230 9 +353033: 6 434 5 80 633 +662454: 882 6 7 3 402 3 28 26 +4623860913: 6 6 8 6 2 49 8 3 2 8 4 982 +6223012578846: 70 889 125 788 46 +1233252: 8 6 5 9 3 804 2 9 213 8 9 +6377598: 7 5 7 11 692 3 2 9 3 1 2 6 +851215538888: 9 5 4 7 4 1 9 42 8 2 890 +652063806: 26 4 21 8 1 6 2 49 8 3 5 8 +569651493608: 626 5 17 9 531 3 607 2 +109564049: 5 4 4 2 622 5 501 5 50 1 +108244890: 920 181 7 3 6 65 +3064775: 6 3 1 7 1 6 7 9 65 9 581 5 +2168098354: 4 479 73 6 40 4 29 53 6 +2213354545: 9 1 212 335 38 87 656 +113305647: 83 1 93 9 5 36 4 8 3 +3648621: 45 2 9 6 4 3 4 1 76 1 7 6 +39788761: 3 631 52 58 75 8 +37988340: 996 882 389 3 52 +105593616: 3 9 2 2 9 196 1 815 57 3 +34732786878: 391 3 6 907 6 88 1 +432317883: 273 791 11 182 +485351797: 58 4 76 83 9 96 +75514220688: 839 90 4 220 688 +8989734402: 89 81 8 734 400 2 +1560183: 7 48 54 4 1 84 1 +1639799045: 116 589 8 9 2 5 3 8 8 +52538: 29 78 3 736 62 86 +943: 726 6 206 5 3 +426657: 5 53 7 230 7 +34098: 40 841 1 3 6 441 7 +987900367: 6 4 6 8 98 8 2 6 36 4 1 3 +3240: 7 2 16 6 9 81 +167040619026129: 8 5 537 764 5 9 479 2 9 +21896: 19 4 6 4 86 184 +473: 364 1 95 7 6 +231: 5 8 191 +4095: 7 571 9 8 81 +40921600: 20 37 92 816 4 50 +39353184077: 99 835 3 9 1 585 8 79 +224194610985: 9 552 4 190 3 999 2 9 2 +81804625: 8 79 5 93 27 84 27 +146: 9 6 8 15 9 +793655568: 491 449 8 8 9 4 +83314065: 462 8 559 18 6 +364726: 40 87 27 26 4 +1678449: 176 3 952 73 +2423015: 5 76 99 6 8 8 501 3 8 +171837368646: 94 1 609 33 1 3 68 648 +686498: 8 8 78 95 2 +504336092: 5 945 553 30 4 2 9 9 5 4 +73304: 12 736 98 +3458989639: 4 6 3 61 7 95 6 50 463 9 +182: 1 4 90 83 5 +2487563: 78 1 992 61 23 45 +13836888716: 772 7 1 8 272 5 8 4 8 68 +6288370: 29 7 180 77 7 137 207 +10944097534206: 1 673 409 408 9 2 327 +1195444320: 92 7 998 6 310 +27360851258: 66 6 9 9 4 2 5 8 51 258 +6317930: 986 63 70 9 982 30 +68190208: 29 918 40 576 8 8 8 8 +21470713: 1 7 2 3 200 5 2 11 359 +309386: 3 72 3 1 6 41 +88555437: 72 696 19 81 93 +692826037754: 766 4 663 899 5 2 54 +88288240327202: 6 6 65 539 57 61 7 20 4 +6481223: 646 9 9 3 145 4 72 +37018831358: 671 7 6 91 31 358 +185508656: 2 14 9 371 4 14 5 93 1 4 +60076539: 4 150 760 39 3 491 5 +349331: 2 3 27 317 329 +6527: 7 1 7 4 7 5 5 2 6 50 1 54 +582802573: 321 516 6 284 2 245 +12151573: 5 92 44 3 9 663 551 9 6 +3265154989: 777 408 9 8 7 2 36 2 3 4 +516041069541: 8 2 4 17 72 7 7 1 546 4 +1695: 7 5 327 5 +17171: 163 5 52 321 +177861: 4 94 120 6 3 +17432592: 9 870 573 716 12 +420355623360: 1 213 7 23 98 19 33 61 +572193: 9 26 3 902 655 6 +417217: 46 2 27 2 58 83 4 55 +2061628: 474 8 2 3 656 978 4 +546001430: 6 8 391 637 8 80 22 65 +281028055: 3 35 9 3 9 9 8 8 48 33 55 +33580: 33 5 68 6 6 +723642076: 467 7 973 2 8 412 5 18 +47287138632892: 72 749 4 4 405 65 3 90 +227860: 37 9 61 6 92 1 4 +6571872443: 543 121 9 148 2 443 +741867: 5 21 9 81 7 +177518571: 8 53 611 1 6 17 862 5 3 +3596: 44 299 7 78 79 +360138: 9 4 1 665 1 2 497 42 3 9 +42509: 23 495 82 6 27 +28260834920: 785 36 834 79 7 5 118 +263067760: 515 714 40 65 85 6 70 +256447296093: 860 200 7 35 864 4 9 2 +246840600711: 53 3 735 350 92 3 712 +13192021941: 409 7 68 56 9 388 5 +15576: 7 40 7 3 3 378 8 390 4 +2189253: 46 8 73 4 53 +1816444120: 605 4 6 3 6 3 4 2 4 1 2 10 +46744143989399: 4 6 74 414 3 98 7 2 399 +1255715008872: 9 872 2 3 5 8 69 7 8 87 2 +118404: 885 12 941 279 4 +404222: 1 9 5 389 2 21 +154879: 1 580 72 79 39 3 +3359259415: 646 26 29 7 2 15 +4538379: 3 8 529 233 8 4 513 +915: 4 1 640 5 265 +581101: 978 3 2 99 8 161 +83836120: 20 94 50 94 41 2 72 8 +2033775: 38 539 61 99 9 9 +692410: 692 3 60 48 5 +194576: 304 4 8 8 2 +620550: 2 5 5 8 64 9 3 562 9 7 +7624704956: 3 93 64 264 8 96 958 +964319157: 147 3 78 35 74 84 +627487938: 8 2 7 641 7 938 +162785716: 37 6 6 3 7 6 3 9 1 4 +9445586101: 6 8 8 3 35 45 7 6 6 4 102 +539240: 9 21 9 7 65 +389898820815: 8 5 538 4 6 3 4 3 601 8 7 +10363: 42 856 6 132 1 +1039848290: 6 3 26 781 102 1 11 +140532: 193 151 13 8 8 574 6 +5068: 15 1 7 44 25 423 +15985935: 81 8 3 1 9 665 +1041709883: 4 795 8 7 1 7 2 2 9 6 646 +1432651301: 102 194 724 99 4 +4878: 7 114 523 99 5 6 390 +4181369637: 4 79 43 880 203 +31575685: 4 6 304 7 3 9 3 2 965 8 5 +3622: 3 61 3 1 9 +982: 99 93 791 +27390952984: 22 67 5 5 7 944 980 6 4 +184342: 71 88 25 329 14 +3392818: 467 24 691 7 +10248050597: 2 7 1 5 1 45 2 66 724 9 5 +53172521: 870 10 4 61 781 19 +11304: 98 3 4 80 9 +104258902: 7 7 4 463 7 484 5 82 2 2 +5613: 6 19 1 22 117 +1148356567: 7 8 274 662 2 745 6 6 6 +4084: 5 67 16 7 3 +124533: 9 6 5 59 14 2 3 5 8 3 3 +2057435762: 74 7 94 74 7 571 +6509263: 62 563 280 871 55 37 +264982: 325 1 8 42 7 6 2 10 +50343: 2 2 44 52 5 +28924499: 59 49 84 60 98 +8914562171: 6 1 9 9 6 160 5 16 7 3 +3969876: 62 8 8 867 946 63 +577332: 774 4 492 70 812 +7380856: 38 3 30 6 856 +159862230: 2 2 8 2 71 46 3 343 7 5 9 +103487100: 2 49 51 41 2 977 483 3 +2547882725245: 483 5 1 4 145 8 9 524 3 +1387400184215: 13 874 100 184 212 +10146850566: 241 7 6 2 77 9 6 10 82 6 +471205560: 7 89 164 40 49 +304928495: 6 813 7 372 95 +5476: 67 4 35 39 4 1 4 +437171: 261 58 370 634 345 +1544843589: 1 544 4 4 3 7 5 9 4 5 645 +1148401434: 3 31 77 3 33 133 9 95 +67822: 73 5 19 8 344 4 91 479 +3534850086174: 72 243 233 3 8 5 739 7 +479471892: 9 97 355 61 9 +1858976: 592 4 608 4 386 +200176098: 85 894 7 6 439 +17579: 9 8 3 58 1 3 +36768: 8 3 60 86 4 16 8 6 +9500988: 579 1 780 28 21 +1156896: 48 241 1 8 8 +66078: 83 79 6 2 440 6 +37342237806: 957 922 441 14 8 423 +2135507316: 3 130 68 703 5 2 7 314 +24622848: 3 206 2 2 384 +517132446: 5 2 518 7 8 9 7 687 6 4 1 +26297215017: 1 5 41 4 658 7 5 386 1 9 +11323819355: 2 45 73 460 41 33 +877788: 94 7 30 8 666 +34923962: 9 588 1 60 5 303 972 1 +2060156: 5 93 6 913 3 9 75 731 +1010573: 4 897 516 604 5 7 2 +1153373: 6 9 416 49 619 370 +188237471616: 338 3 5 926 3 1 9 6 2 16 +351420096: 77 386 3 7 22 8 563 +28154770105: 554 8 50 7 25 2 33 1 55 +797941714: 931 9 6 7 7 7 4 952 68 +17307: 20 213 7 8 7 7 841 +278861076: 31 562 2 85 1 3 4 468 +939675870639: 989 5 366 945 638 +3355236: 1 1 531 732 1 91 20 3 5 +303636711: 94 3 41 427 73 +4553356517: 417 79 36 30 85 5 3 +119833217456: 149 79 152 18 2 8 +8647858: 2 91 55 716 785 8 +70791495: 6 9 5 1 3 9 1 5 9 385 89 5 +58703112937: 3 7 592 1 4 73 2 949 +1333781: 5 22 7 3 321 5 5 +55088: 449 376 49 63 26 +14704: 1 235 3 5 9 8 2 4 747 4 4 +141818221948: 468 6 97 6 9 336 3 170 +106725235: 275 66 6 98 323 4 +935144558: 90 36 72 243 4 45 57 +4783996: 4 760 23 993 1 +7179142: 74 97 91 9 6 216 +17860470: 337 9 14 312 10 9 530 +82330587240: 56 4 5 56 7 2 810 4 4 5 6 +7624624: 9 486 900 38 236 38 +3117225: 7 7 52 32 8 9 89 723 5 4 +4533781031: 70 6 86 9 4 742 933 17 +1152: 62 5 5 190 287 +202477780855: 5 278 6 251 2 97 265 7 +33930789: 22 8 31 9 6 8 3 4 1 +46868: 46 366 1 7 4 491 +2234965: 9 9 253 10 9 8 8 9 6 76 +56725: 725 78 7 2 513 +413871: 5 17 25 8 68 +4411739: 612 901 54 8 7 4 +280019599: 62 497 5 4 1 3 9 80 599 +139832: 4 9 965 2 343 7 7 79 2 +6626869: 599 3 86 64 28 2 +22108023: 854 2 9 258 5 +149940: 80 69 462 480 1 +2333364: 52 223 804 4 44 +564496918: 5 748 658 4 9 6 918 +7310688: 4 7 1 82 40 4 9 51 21 92 +232322688: 6 74 64 9 24 84 32 +472649: 2 28 8 20 45 606 +4985: 980 2 13 2 5 +173748825: 9 48 75 97 419 +4032: 53 90 75 6 18 +2819352: 73 3 16 711 77 6 19 37 +956118: 557 3 5 570 786 8 4 +16924517: 2 7 4 67 6 21 984 9 701 +92: 9 4 1 57 1 +812046062: 7 2 5 4 32 38 96 7 9 581 +87867529187: 67 2 7 4 906 6 8 3 8 3 87 +28749775: 2 4 2 61 5 82 32 11 49 5 +6310651: 983 3 8 8 2 51 +5014704: 4 2 747 839 753 2 7 78 +15436752: 47 207 327 27 33 3 +83562106762: 83 8 9 498 383 2 6 764 +60877371: 37 5 688 42 41 17 +310856: 677 5 25 6 91 +116678676: 1 29 177 4 511 +4599: 434 6 88 87 78 +1737334: 159 43 4 7 53 657 2 22 +512120: 441 3 719 15 9 836 7 4 +18168384: 1 7 7 243 971 +5436: 424 40 9 734 56 +2853540: 30 948 95 39 1 +207065: 414 13 5 +1387710382: 14 11 9 2 8 6 2 77 9 79 +95854: 93 51 3 5 6 8 216 4 4 +11476: 98 2 4 110 36 +88098: 89 790 1 91 6 3 +4650: 77 6 1 22 7 +320716: 6 82 41 234 61 82 670 +912: 86 7 5 2 41 48 627 +164961: 3 674 966 30 92 538 1 +138616: 4 3 34 4 835 8 7 2 8 +1965: 961 2 45 +100926540962: 5 2 9 991 990 6 1 960 +3194282223: 195 5 426 400 3 9 57 +2377: 885 5 902 520 65 +875172675: 2 485 3 9 4 8 6 84 3 2 3 5 +3527088: 2 2 38 17 89 32 39 1 6 +36414372: 9 749 25 4 1 6 917 8 7 2 +1956972: 4 1 7 1 4 4 7 1 728 48 6 2 +186714996: 891 7 3 3 7 99 8 +8779120: 10 381 1 7 23 20 +58735380: 97 89 220 10 6 +1546053: 81 320 590 52 6 5 87 6 +891089745: 9 521 946 38 5 4 +18500736: 2 2 5 925 72 8 9 +46556774: 9 33 499 77 +506616547829: 7 79 2 5 152 9 1 3 6 2 6 +9423: 32 2 9 27 1 +249538843: 4 9 13 4 23 5 709 3 41 +9677312192: 1 23 224 2 6 5 3 2 180 9 +367662: 3 9 1 5 2 1 276 37 8 7 7 8 +479671395840: 3 4 91 8 9 2 5 812 46 4 7 +2408811: 96 305 11 9 340 4 5 6 +16956800: 33 8 950 2 32 +461607: 45 985 4 87 9 835 2 +776: 1 97 8 +13140615: 3 73 2 6 5 47 8 5 5 1 1 9 +28824: 1 5 46 3 58 1 3 9 87 1 +95810721: 1 94 791 1 18 665 58 +117868923: 81 1 97 928 3 5 +1068: 86 3 7 41 4 53 874 +6282: 1 27 21 66 65 9 +7620674: 5 71 20 487 94 93 2 +7935824: 7 819 739 22 1 5 13 78 +199809421: 694 64 75 9 499 335 5 +30580: 305 4 3 36 +13500687200: 68 3 266 180 572 7 5 2 +127633711135: 1 99 7 8 7 4 8 555 2 8 5 3 +194559682: 6 648 5 12 1 2 22 5 679 +116522137: 82 19 59 44 1 35 34 4 +19715: 98 2 9 7 16 +101871: 38 991 99 +258511840: 5 42 1 54 709 149 32 +2391735493: 32 765 3 7 35 458 34 +187570353992: 4 9 9 9 7 7 9 3 4 344 7 49 +360374400018: 200 208 625 8 36 5 5 8 +2230817787: 4 58 2 2 1 181 7 5 9 1 9 3 +401494: 3 4 99 1 938 6 4 6 +733544: 4 2 4 8 382 2 8 2 7 8 3 5 +77590906330: 8 3 4 76 8 2 42 7 4 68 8 +2004866958: 250 6 8 66 933 1 25 +533045520: 5 6 6 6 6 3 7 521 8 5 7 6 +5413: 8 4 451 +3651758346: 942 546 71 10 1 45 +1125824227545: 3 3 4 1 8 533 87 5 7 8 6 +190513132: 6 24 9 49 6 5 9 3 5 5 4 99 +934891: 9 7 26 4 2 8 2 8 82 1 90 +1137: 8 43 20 86 688 +17531: 89 84 10 221 +118321967: 8 9 704 65 6 7 4 3 631 2 +638843084623: 1 71 3 7 5 4 5 578 179 1 +98230: 29 2 2 1 6 2 90 1 7 64 8 +13444932444: 8 42 123 216 95 1 4 2 +71659034: 626 1 2 9 25 9 95 8 114 +32711009: 26 141 51 15 1 88 +1684270: 1 280 7 794 92 1 7 59 +29916: 1 86 82 339 6 4 +4811820531: 4 82 1 3 4 2 2 90 611 8 4 +9867762: 10 1 8 3 1 2 9 4 188 3 3 2 +510: 457 24 30 +3353882283: 3 93 490 2 2 757 3 1 3 8 +61: 5 17 9 1 32 +1727: 708 898 26 86 8 +223611912: 7 7 3 1 451 86 231 9 3 8 +116475838: 8 840 24 549 +2156733: 7 8 7 2 4 533 8 1 37 730 +49787: 4 8 645 883 258 +771230164: 9 2 97 4 1 9 808 2 4 6 6 +5275: 105 5 5 +69778355889: 802 87 4 2 5 9 5 9 99 91 +911554728: 4 5 2 3 9 4 236 93 7 8 5 6 +704196331803: 704 1 963 31 802 +472914541: 28 67 78 4 9 3 7 9 91 7 9 +7257683: 93 868 6 817 89 +36471370885: 300 770 3 47 6 2 86 +33958632312: 19 992 99 99 248 73 +2488243: 4 8 74 4 190 +26913521521: 7 8 9 6 9 4 49 6 35 1 67 4 +52685: 405 98 23 8 8 +4406699117: 5 974 4 4 9 2 5 9 91 +795088373103: 158 978 39 67 4 6 21 5 +305130396: 9 5 1 59 3 347 9 67 12 +531: 47 3 7 1 1 9 +1192268044384: 3 871 88 35 4 435 4 31 +6392322: 7 63 351 7 8 251 44 27 +69739288: 8 71 7 4 8 84 4 +523483: 87 2 3 5 2 91 2 692 +802030: 6 2 81 86 22 30 +626316838: 41 201 38 2 419 416 +18023805: 969 31 6 60 42 +2762443: 15 64 777 45 1 204 4 +71441: 87 81 96 9 +93838674: 6 7 55 7 155 70 9 97 +7306479: 3 2 1 283 9 63 9 7 9 5 8 +314885: 9 34 8 8 12 70 6 +4845827: 484 5 102 697 31 +22788228: 418 9 8 1 68 +135358192800: 7 2 3 1 5 4 949 8 5 80 9 +1546: 4 51 96 2 1 26 3 909 7 +361176914: 3 848 2 49 7 2 2 8 2 914 +9544257: 7 7 9 42 3 2 7 5 546 27 +526: 65 8 2 4 +1932693441: 53 67 7 8 9 4 29 9 780 1 +821715: 33 83 3 14 +1810654: 22 81 975 556 321 +415368: 31 3 735 2 54 +2650: 39 79 21 295 96 5 +3277842795: 8 69 22 4 6 2 5 5 3 613 +787763809: 692 4 8 363 53 6 35 49 +140345412505: 296 4 947 24 43 58 5 +397671263: 9 1 3 1 57 6 8 5 4 3 126 3 +2875941: 4 603 5 44 47 8 +4621566793294: 8 932 452 5 22 61 853 +8968000: 467 8 59 32 3 +6027428: 5 675 352 423 6 +1724: 8 2 12 694 46 +392769: 9 6 907 5 1 1 39 5 3 3 9 +3925: 759 4 1 61 827 +1486822: 82 594 5 9 17 3 2 +2400: 6 7 143 90 8 +4286544: 347 9 807 1 546 +3307871: 3 4 4 4 6 6 72 2 285 23 7 +398278260: 3 7 81 6 9 7 526 +59250: 6 93 623 28 79 +122906054: 30 884 64 3 92 7 53 +32646: 3 1 627 9 +2980467454: 8 8 9 9 3 3 4 46 7 440 5 9 +55943811099: 530 535 758 770 2 9 9 +2168287875: 6 86 257 2 42 7 5 +481195320: 789 790 772 +62339349: 4 1 8 930 8 9 3 16 9 3 2 4 +7157155545: 3 15 3 973 5 4 3 5 55 48 +210528982: 4 9 9 32 521 57 866 52 +1545266035: 61 62 6 32 5 680 9 24 +51739: 9 5 2 7 82 79 +6102859: 60 181 756 90 8 3 76 +10159693192: 4 27 33 114 721 +214575225: 8 51 6 4 9 837 675 9 5 9 +7028944: 47 7 4 3 9 7 1 51 2 376 +23322867: 9 49 29 28 4 574 7 +902118625817: 883 8 759 94 625 819 +713256123099: 742 155 47 961 9 1 97 +1755816105888: 698 3 776 796 351 3 +396663497: 1 983 2 634 97 +1348244080: 65 768 205 78 1 +462274: 248 8 233 +61266: 8 7 87 6 69 +5882741765: 1 7 3 5 3 1 427 2 4 8 8 8 +766181250: 485 29 811 771 15 50 +1716743769: 58 8 701 754 7 308 69 +21777: 2 9 3 5 65 +1553336: 56 295 94 1 393 59 +40332646896: 873 35 22 781 6 6 +4110: 35 5 80 156 754 +37564007116818: 93 910 8 5 7 1 116 818 +26127012: 9 24 1 7 4 98 4 2 7 3 33 9 +12548006988763: 979 1 1 4 2 86 149 763 +636520546: 998 1 4 7 7 425 4 77 85 +14145768: 829 4 7 4 651 4 889 +8725165: 9 173 5 903 6 8 3 65 +474882020: 448 53 101 5 4 +124342128: 983 5 871 614 28 35 8 +358665062: 5 3 80 8 1 1 8 8 337 3 6 +669561657: 3 28 2 607 2 3 6 1 656 +117632: 8 3 208 555 4 +1220055: 217 56 7 40 39 119 +205348861: 9 56 3 716 61 +8410710: 5 64 4 8 98 7 363 +7928792: 376 46 8 80 3 4 3 3 764 +5799083120393: 3 5 6 47 4 7 170 7 7 83 5 +107463712: 9 6 1 6 60 9 5 9 9 2 8 67 +10207: 91 1 33 428 549 +43330935: 8 9 53 7 68 947 82 3 +19476019: 1 9 3 1 72 94 3 7 6 4 34 +284580: 15 8 1 20 9 +2144840841: 57 38 505 9 99 +22349258705: 19 1 26 5 9 1 732 8 5 8 +487325080187: 39 935 650 160 38 5 +24929138: 1 395 91 18 932 +65799405: 5 3 1 263 435 +2216: 82 4 6 24 8 +980777690: 954 220 584 835 50 +561801738: 1 4 50 8 15 4 325 673 8 +1490988: 93 2 8 2 987 +7942059112: 7 9 1 1 3 8 5 5 4 582 9 12 +4180933171: 830 6 5 93 260 54 3 27 +330636: 715 387 13 3 +1069672872524: 1 298 9 795 7 5 2 252 4 +5186234: 5 1 794 18 50 37 +5016205: 722 43 6 426 1 4 7 1 56 +258833147: 4 8 7 6 548 513 23 +28645434967: 203 1 61 14 25 9 967 +9181353: 69 98 751 1 351 +4124314800: 66 7 8 687 772 190 9 6 +291900470: 6 7 3 6 2 525 2 2 61 9 +4665835983: 170 296 58 35 983 +30415786635: 713 864 927 394 108 +346694957: 385 21 561 42 9 531 +76864: 70 6 864 +127218604: 18 7 8 3 96 14 8 5 2 2 6 2 +98043728: 4 3 79 5 1 8 19 8 6 9 2 51 +1125553: 29 83 55 52 3 +142859: 9 51 150 3 205 +8996988138: 493 389 943 8 65 5 4 7 +32707052: 6 51 5 5 8 4 6 218 3 3 5 2 +4186878922: 41 7 74 94 73 5 902 20 +50607732: 3 3 132 3 57 9 113 43 3 +1318255: 12 186 8 59 905 +320484960: 6 1 707 6 1 914 487 +2068225: 3 8 9 44 227 +79076: 85 3 310 26 +13952270: 29 979 64 137 2 95 +106877774: 2 490 37 93 6 113 567 +3846: 3 1 8 37 1 7 21 7 73 149 +13619850960: 25 28 444 860 673 +2074682368: 3 226 8 4 226 2 5 8 5 65 +7226571570: 722 182 91 384 1 567 +900282: 89 9 217 988 77 +1127715: 6 97 5 787 4 76 816 3 +16058952397: 35 608 392 741 394 +26843: 886 8 4 3 3 +35145018: 1 66 39 290 50 19 +1278: 93 534 2 15 9 +24038: 893 521 17 +196530656: 4 1 721 784 3 956 8 7 +47772498: 4 7 5 3 748 6 8 7 15 4 9 9 +8610: 70 4 11 2 795 69 +423060737457: 4 8 7 2 3 54 3 2 1 886 9 3 +63375360: 75 109 77 16 96 5 +128980522089: 8 475 3 2 8 418 2 829 2 +7945: 5 5 6 843 8 +8684275286: 1 784 83 421 6 52 88 +3714: 33 4 11 +110467308: 5 835 86 819 18 25 84 +297205: 7 67 102 342 4 +61361: 2 3 2 7 2 361 +22550147294: 3 186 4 7 46 5 36 823 4 +1052927614392: 2 1 5 564 4 3 3 718 393 +3828: 3 58 417 8 7 +9729036288: 656 4 16 314 82 9 +5831042160: 915 619 3 834 4 5 514 +10047756640: 8 301 4 2 7 6 8 11 8 388 +34334730: 51 440 1 90 17 +2355371884: 7 7 8 1 5 3 2 3 7 184 4 4 +7720: 5 69 7 4 433 1 9 8 +508919: 92 204 79 1 3 2 9 5 +2749463626: 642 921 5 465 +148277603765: 1 5 43 40 7 4 39 8 429 5 +6912: 81 51 84 2 55 89 12 +2519958: 7 304 32 8 253 3 9 66 +68495: 42 634 7 38 95 +37935797473371: 76 176 3 498 73 368 +4088514: 20 460 1 85 14 +42340486560: 727 7 104 4 2 1 6 5 57 +93889045874: 7 7 3 3 722 4 7 9 8 5 873 +111628894: 29 375 84 38 702 +1836669: 5 4 34 602 68 +127205: 9 10 8 4 3 893 8 3 5 8 1 5 +10169671: 1 72 7 6 997 8 1 46 4 3 2 +237605921214087: 62 52 7 874 38 140 87 +37392: 1 5 276 175 82 +33961623: 1 6 1 6 99 3 61 543 6 71 +37304: 8 4 5 620 65 2 38 +328923190: 876 126 113 7 4 415 +9004522: 852 15 2 21 352 8 2 +6277: 6 274 4 +799: 18 5 3 7 5 3 4 8 4 7 7 305 +815: 5 567 75 167 1 +1907156: 7 55 29 4 2 4 3 733 5 6 +54232722: 8 11 1 1 12 3 3 6 51 438 +90356: 6 53 204 65 98 +80393954535: 6 980 8 699 3 9 815 +585333000: 81 1 6 8 557 1 4 7 6 3 7 3 +1740698240: 1 89 16 4 1 8 19 8 944 5 +27080469199: 147 5 997 177 196 +397655: 4 80 674 4 54 +32581186: 58 546 913 14 4 3 +1411288: 329 6 788 5 1 50 5 7 5 3 +686822508: 6 837 3 29 455 10 +14329614: 37 387 10 540 74 +900648672: 172 596 9 8 44 89 37 +12010046: 6 8 72 100 47 +51520200: 831 9 19 27 613 1 +44168970: 72 20 6 8 1 2 77 67 +53032: 529 796 8 1 4 +88279: 5 38 7 293 86 +37669257997338: 570 7 463 33 292 1 66 +37263336: 423 4 47 22 4 +1953126: 196 1 2 2 4 411 3 7 8 3 +18262: 25 181 88 80 55 +745577968: 9 31 27 70 2 4 5 8 7 +325718593204: 3 1 6 3 7 1 8 3 9 35 132 2 +70183: 7 5 92 597 83 +292206: 8 528 2 973 5 31 +35684388: 9 6 531 4 7 9 1 5 7 8 798 +4518001568: 8 4 4 3 3 9 900 4 1 563 6 +353907: 69 907 3 870 5 6 +14174170: 141 735 60 582 28 +4579: 89 80 9 5 3 +905743293: 17 4 3 57 9 922 48 +4721355: 3 4 6 13 6 8 7 2 8 15 853 +28300777: 6 2 7 4 4 3 9 94 4 38 97 8 +3059337317: 305 44 49 373 17 +123832907: 800 5 7 762 8 412 5 2 6 +189634: 9 9 9 624 7 +53083923: 906 59 13 852 58 1 +117861384016: 5 46 14 5 2 6 8 8 5 5 8 17 +1670110: 25 95 542 6 25 54 53 +18266750880: 8 28 98 20 86 69 478 +846: 754 3 7 6 74 +91230: 565 325 22 30 +21602109: 878 1 82 6 3 833 +383: 6 1 2 178 2 11 +4640341196: 5 58 4 4 3 3 99 5 4 3 96 +295: 2 7 35 7 5 15 +25640: 60 7 42 146 +2324: 7 2 7 4 852 +2106855: 1 918 527 243 5 6 15 +1096879615: 9 1 38 52 20 61 792 +206838115: 2 7 2 7 42 7 94 1 7 9 2 5 +892: 838 53 1 +37042143372: 1 38 45 81 549 26 386 +13608260: 9 7 9 3 63 260 +10296568836: 260 11 15 801 6 6 +1152012: 2 7 410 450 12 +15670: 9 174 9 +378879: 428 87 522 6 530 762 +63217413002: 7 7 10 61 7 1 1 5 3 350 3 +214357346: 9 9 2 3 3 9 527 5 9 5 2 6 +6384: 1 5 860 5 42 7 +3660322: 2 687 53 8 8 1 770 9 20 +597992800: 59 79 878 49 1 8 8 +2086695: 3 1 8 2 164 1 6 1 3 195 3 +56275146474: 7 1 82 572 903 88 71 +5878173: 9 97 2 615 9 +256125: 69 157 24 3 57 13 112 +63613676031: 345 710 62 53 49 17 +6041746627: 6 65 6 3 180 1 6 77 25 5 +445082: 50 9 4 7 75 92 141 9 5 +3336568969: 11 93 3 1 605 537 7 9 +14476045838: 720 220 154 45 8 3 8 +25791150: 73 65 2 37 350 +1770: 4 18 8 2 7 +634521698: 96 816 81 89 9 +766610: 7 35 29 2 609 +165288: 99 8 89 88 6 97 +122461717: 2 657 224 269 8 37 5 +49274709: 8 61 4 71 709 +156370725697: 1 771 2 9 3 3 2 75 5 6 9 6 +135450075: 306 97 8 1 42 10 73 +178096158: 7 471 686 391 54 +37098919: 1 2 6 539 2 1 7 45 1 8 2 2 +564755082: 829 68 103 508 2 +15091: 84 7 590 978 7 +5925156: 4 80 5 6 7 5 7 443 95 8 +227132981304: 639 235 47 7 9 1 9 2 3 4 +83734: 4 2 3 7 36 +4764348248: 231 851 16 64 642 44 +201359438581: 33 9 429 677 5 580 1 +5382: 32 497 1 9 3 +89850: 46 810 70 866 5 50 +7095185620: 2 18 2 83 9 388 9 1 34 +898628: 89 340 93 429 8 +4521335: 2 988 456 2 691 4 +825553: 527 522 3 216 56 +1437: 6 7 492 737 196 +6349783608: 2 98 268 522 216 63 +26307: 47 42 204 4 36 79 +2664689: 4 37 46 3 295 96 88 +517476672: 699 24 5 28 74 +906139989: 15 59 62 559 98 8 3 +101621401: 3 4 99 29 42 6 841 7 +174668041: 291 11 339 3 3 2 1 +499516611066: 5 3 6 3 6 238 6 7 55 1 6 3 +656734: 5 789 741 21 7 9 34 +2964812: 3 4 7 2 36 1 11 813 +457793: 526 17 130 8 791 39 5 +324783326229: 32 474 9 34 32 62 29 +714670: 42 17 552 60 9 46 +174857716: 6 9 146 8 734 1 9 18 2 9 +22400846: 350 64 843 +9498234307: 4 12 966 709 23 +16680905: 9 9 156 7 3 17 6 6 4 8 4 +405: 9 9 5 +4565438: 913 1 5 4 37 +214448709: 670 13 423 7 318 +2056320: 6 854 62 7 247 378 +28709528: 3 580 8 550 57 75 9 8 +12101046387: 5 1 86 7 4 3 7 3 638 5 2 +9550: 52 7 992 7 58 +8347014: 7 32 5 92 9 9 331 443 +27345853945: 9 202 2 3 8 3 3 8 4 649 3 +154494235766: 772 469 2 1 777 6 5 3 2 +55637839247: 6 8 2 44 955 8 7 5 9 93 4 +3395211904: 285 9 77 53 415 224 +410136: 51 26 7 8 +22526624126: 2 555 82 778 854 6 +2985869706: 46 9 70 8 5 7 8 7 6 90 9 +3423438677: 714 705 958 346 5 +784310979: 784 310 89 8 9 +2859878751: 3 96 3 36 48 8 37 9 305 +900: 38 7 1 4 5 +30425338: 30 4 25 33 8 +567294: 4 702 3 12 8 +33541379: 110 571 6 89 64 775 +494363134: 555 4 620 12 89 886 +71527: 3 6 5 3 8 6 1 7 12 5 8 7 +46980046: 53 721 96 540 43 +37206236963: 7 7 88 94 6 9 83 1 61 5 3 +50976954: 5 2 2 80 45 37 11 87 14 +5203315048: 1 63 2 74 61 27 3 5 50 +1939803185: 969 901 59 2 6 +1028599911631: 118 20 2 97 1 749 87 1 +91641646160: 9 424 43 62 472 206 +4808181291: 2 978 9 889 26 12 93 +357338008: 706 3 504 8 3 2 9 6 789 +81364559: 800 13 56 8 556 +1517735009: 1 6 414 291 923 787 +10942: 2 2 130 72 +2927: 74 5 3 8 1 31 9 646 +21363940: 72 22 73 3 49 829 260 +1075962223: 21 512 7 615 53 670 +91216741: 912 1 30 37 41 +3020021513: 749 56 686 8 458 9 2 +3412491525: 28 57 29 4 8 912 3 23 +586986542: 34 36 81 832 2 7 8 7 68 +3985200: 3 3 3 616 9 118 4 9 45 +25153680: 82 71 3 1 60 8 3 39 24 +5121: 557 3 8 1 9 +4361404380: 9 72 779 4 9 6 6 2 5 12 +4633574: 1 8 52 2 26 4 9 4 4 7 6 41 +2500: 10 3 62 200 7 433 +205765071: 237 599 5 866 4 +475225461: 938 7 336 6 687 90 81 +67637: 67 633 6 +62118081357: 1 2 9 1 90 290 4 5 2 4 90 +3666: 3 58 7 8 8 +149245: 4 382 4 19 718 5 87 28 +561006667: 82 5 68 66 6 6 +83570460265375: 92 6 318 188 547 5 85 +71579598: 29 75 67 329 55 +108468786: 27 117 4 747 42 +108885688319: 1 3 7 7 770 5 4 2 88 320 +274800836: 5 60 2 6 49 4 47 435 7 9 +161022283: 38 7 604 358 232 51 +19692: 41 990 933 9 43 +1814645: 9 270 89 7 85 695 +5522958: 9 3 9 6 1 63 769 +14581981: 11 9 6 9 6 8 2 7 4 60 63 +150150: 5 4 90 3 69 5 18 6 72 +34959356: 368 3 678 22 14 6 1 1 3 +2325598: 48 57 850 +13630671514: 1 45 3 4 9 526 31 171 3 +83325934080: 8 7 8 9 7 182 7 6 32 9 1 +37047437: 4 583 9 777 2 1 9 10 4 1 +2664496: 98 65 9 7 9 3 709 +2768330498: 1 893 31 30 498 +865227: 50 2 57 167 33 +5902050651: 843 1 487 1 7 7 1 48 54 +7499664484: 832 694 602 9 4 84 +7952436: 770 1 2 233 6 1 1 436 +8294452: 2 9 3 8 66 5 1 5 41 8 7 2 +6456627216: 4 3 9 26 4 86 3 8 544 1 8 +2802: 255 2 169 9 4 48 2 +699018: 2 7 2 6 6 353 7 5 4 39 +2189890971124: 92 980 9 785 704 2 3 1 +33192972: 646 99 519 826 20 +6335442: 2 6 564 13 3 4 1 2 96 6 +8535690: 1 866 1 2 9 6 2 9 5 9 3 90 +5936091: 89 900 3 49 6 +1429232: 79 7 8 626 9 500 112 7 +12011121824: 15 8 11 12 1 824 From 11ad3234d1bdb1c791ea6e43344e463d1f082945 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sat, 7 Dec 2024 09:57:05 +0100 Subject: [PATCH 07/13] feat(solutions/gmacario): day07 Part 1 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day07/day07.ipynb | 233 +++++++++++---------------- 1 file changed, 91 insertions(+), 142 deletions(-) diff --git a/solutions/gmacario/day07/day07.ipynb b/solutions/gmacario/day07/day07.ipynb index 27253ce..6804095 100644 --- a/solutions/gmacario/day07/day07.ipynb +++ b/solutions/gmacario/day07/day07.ipynb @@ -52,49 +52,15 @@ "execution_count": 3, "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "ic| input_lines: ['190: 10 19',\n", - " '3267: 81 40 27',\n", - " '83: 17 5',\n", - " '156: 15 6',\n", - " '7290: 6 8 6 15',\n", - " '161011: 16 10 13',\n", - " '192: 17 8 14',\n", - " '21037: 9 7 18 13',\n", - " '292: 11 6 16 20']\n" - ] - }, - { - "data": { - "text/plain": [ - "['190: 10 19',\n", - " '3267: 81 40 27',\n", - " '83: 17 5',\n", - " '156: 15 6',\n", - " '7290: 6 8 6 15',\n", - " '161011: 16 10 13',\n", - " '192: 17 8 14',\n", - " '21037: 9 7 18 13',\n", - " '292: 11 6 16 20']" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Read the puzzle input into a list of strings, one per line\n", "#\n", "with open(\"input_day07_gmacario.txt\", 'r') as file:\n", " input_lines = [line.rstrip() for line in file]\n", - " input_lines = test.splitlines() # Uncomment for debug\n", + " # input_lines = test.splitlines() # Uncomment for debug\n", "\n", - "ic(input_lines)" + "# ic(input_lines)" ] }, { @@ -126,45 +92,11 @@ "execution_count": 5, "id": "d35b2b1a-a493-4c55-8696-a4af4299708a", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "ic| test_equations: [(190, [10, 19]),\n", - " (3267, [81, 40, 27]),\n", - " (83, [17, 5]),\n", - " (156, [15, 6]),\n", - " (7290, [6, 8, 6, 15]),\n", - " (161011, [16, 10, 13]),\n", - " (192, [17, 8, 14]),\n", - " (21037, [9, 7, 18, 13]),\n", - " (292, [11, 6, 16, 20])]\n" - ] - }, - { - "data": { - "text/plain": [ - "[(190, [10, 19]),\n", - " (3267, [81, 40, 27]),\n", - " (83, [17, 5]),\n", - " (156, [15, 6]),\n", - " (7290, [6, 8, 6, 15]),\n", - " (161011, [16, 10, 13]),\n", - " (192, [17, 8, 14]),\n", - " (21037, [9, 7, 18, 13]),\n", - " (292, [11, 6, 16, 20])]" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "test_equations = [line_to_test_entry(l) for l in input_lines]\n", "\n", - "ic(test_equations)" + "# ic(test_equations)" ] }, { @@ -172,66 +104,105 @@ "execution_count": 6, "id": "5cbc3551-7488-4416-9193-e86b85246736", "metadata": {}, + "outputs": [], + "source": [ + "# def find_operators(result:int, operands: tuple) -> tuple:\n", + "# \"\"\"\n", + "# Find the operands which applied to operands produce the result\n", + "# Return a list with the operators, or None if no solution is found\n", + "# \"\"\"\n", + "# operators = list()\n", + "\n", + "# total = operands[0]\n", + "# # totals = [operands[0]]\n", + "\n", + "# for o in operands[1:]:\n", + "# for op in ['+', '*']:\n", + "# if op == '+':\n", + "# total += o\n", + "# else:\n", + "# total *= o\n", + "# # ic(total, result, o, op)\n", + "# if total == result:\n", + "# operators.append(op)\n", + "# break\n", + " \n", + "# # ic(result, operands, operators)\n", + "# return tuple(operators)\n", + "\n", + "# # ic(find_operators(test_equations[0][0], test_equations[0][1])) # 190\n", + "# ic(find_operators(test_equations[1][0], test_equations[1][1])) # 3267" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "03896837-6eb5-4003-a940-bd637c273656", + "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "ic| total: 121, result: 3267, o: 40, op: '+'\n", - "ic| total: 4840, result: 3267, o: 40, op: '*'\n", - "ic| total: 4867, result: 3267, o: 27, op: '+'\n", - "ic| total: 131409, result: 3267, o: 27, op: '*'\n", - "ic| result: 3267, operands: [81, 40, 27], operators: []\n", - "ic| find_operators(test_equations[1][0], test_equations[1][1]): ()\n" + "ic| solve_equation(test_equations[1][0], test_equations[1][1]): None\n" ] - }, - { - "data": { - "text/plain": [ - "()" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "def find_operators(result:int, operands: tuple) -> tuple:\n", - " \"\"\"\n", - " Find the operands which applied to operands produce the result\n", - " Return a list with the operators, or None if no solution is found\n", - " \"\"\"\n", - " operators = list()\n", - "\n", - " total = operands[0]\n", - " # totals = [operands[0]]\n", - "\n", - " for o in operands[1:]:\n", - " for op in ['+', '*']:\n", - " if op == '+':\n", - " total += o\n", + "def solve_equation(result: int, operands: tuple) -> tuple:\n", + " # ic(\"solve_equation\", result, operands)\n", + " num_operators = len(operands) - 1\n", + " possible_operators = list()\n", + " for k in range(2 ** num_operators):\n", + " seq = bin(k)[2:].zfill(num_operators)\n", + " seq2 = list(seq.replace(\"0\", \"+\").replace(\"1\", \"*\"))\n", + " # ic(result, operands, seq, seq2)\n", + " # ic(\"Trying\", seq2, \"with\", operands, \"to get\", result)\n", + " total = operands[0]\n", + " for operand, operator in zip(operands[1:], seq2):\n", + " # ic(total, operand, operator)\n", + " if operator == '+':\n", + " total += operand\n", " else:\n", - " total *= o\n", - " ic(total, result, o, op)\n", - " if total == result:\n", - " operators.append(op)\n", - " break\n", - " \n", - " ic(result, operands, operators)\n", - " return tuple(operators)\n", + " total *= operand\n", + " if total > result:\n", + " # ic(\"Got a\", total, \"bigger than\", result, \"--> continue\")\n", + " continue\n", + " # ic(\"After loop:\", seq2, total, result)\n", + " if total == result:\n", + " # ic(\"FOUND\", seq2, total, result)\n", + " return tuple(seq2)\n", + " \n", + " # ic(\"WARNING: No results found\", result, total)\n", + " return None\n", "\n", - "# ic(find_operators(test_equations[0][0], test_equations[0][1])) # 190\n", - "ic(find_operators(test_equations[1][0], test_equations[1][1])) # 3267" + "ic(solve_equation(test_equations[1][0], test_equations[1][1])) # 3267" ] }, { "cell_type": "code", - "execution_count": null, - "id": "03896837-6eb5-4003-a940-bd637c273656", + "execution_count": 8, + "id": "64b57e8b-70a5-4dc3-b738-25239bcf28f4", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 07 Part 1: RESULT: 932137732557\n" + ] + } + ], + "source": [ + "def solve_part1(test_equations):\n", + " result = 0\n", + " for eq in test_equations:\n", + " if solve_equation(eq[0], eq[1]) != None:\n", + " result += eq[0]\n", + " return result\n", + "\n", + "print(\"Day 07 Part 1: RESULT:\", solve_part1(test_equations))" + ] }, { "cell_type": "code", @@ -241,14 +212,6 @@ "outputs": [], "source": [] }, - { - "cell_type": "code", - "execution_count": null, - "id": "ea0b61ee-501f-4893-ad9a-e93fdb317e33", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null, @@ -307,25 +270,11 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "8a366015-d692-4bf7-aa35-1f250d022cb2", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'solve_part1' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDay 07 Part 1: RESULT:\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43msolve_part1\u001b[49m(lab_map))\n", - "\u001b[0;31mNameError\u001b[0m: name 'solve_part1' is not defined" - ] - } - ], - "source": [ - "print(\"Day 07 Part 1: RESULT:\", solve_part1(lab_map))" - ] + "outputs": [], + "source": [] }, { "cell_type": "markdown", From b35a3f7b681475abe7e36178de8d2d1021284536 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sat, 7 Dec 2024 13:30:17 +0100 Subject: [PATCH 08/13] feat(solutions/gmacario): day07 Part 2 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day07/day07.ipynb | 311 ++++++++++++++------------- 1 file changed, 161 insertions(+), 150 deletions(-) diff --git a/solutions/gmacario/day07/day07.ipynb b/solutions/gmacario/day07/day07.ipynb index 6804095..1d812c6 100644 --- a/solutions/gmacario/day07/day07.ipynb +++ b/solutions/gmacario/day07/day07.ipynb @@ -17,7 +17,18 @@ "metadata": {}, "outputs": [], "source": [ - "from icecream import ic" + "from icecream import ic\n", + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6c8149a1-7aed-4bfb-8ec9-748c65077d45", + "metadata": {}, + "outputs": [], + "source": [ + "# use_test = True # Comment out for using actual puzzle input" ] }, { @@ -30,10 +41,18 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "b8ac6332-ebd1-424b-a781-df1771e81be8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'use_test was undefined - forcing to', use_test: False\n" + ] + } + ], "source": [ "test = \"\"\"190: 10 19\n", "3267: 81 40 27\n", @@ -44,35 +63,56 @@ "192: 17 8 14\n", "21037: 9 7 18 13\n", "292: 11 6 16 20\n", - "\"\"\"" + "\"\"\"\n", + "\n", + "try:\n", + " use_test\n", + "except NameError:\n", + " use_test = False\n", + " ic(\"use_test was undefined - forcing to\", use_test)\n", + " \n", + "if use_test:\n", + " assert test != None" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| time.ctime(): 'Sat Dec 7 12:29:07 2024'\n" + ] + } + ], "source": [ "# Read the puzzle input into a list of strings, one per line\n", "#\n", - "with open(\"input_day07_gmacario.txt\", 'r') as file:\n", - " input_lines = [line.rstrip() for line in file]\n", - " # input_lines = test.splitlines() # Uncomment for debug\n", + "if use_test:\n", + " input_lines = test.splitlines() # Uncomment for debug\n", + "else:\n", + " with open(\"input_day07_gmacario.txt\", 'r') as file:\n", + " input_lines = [line.rstrip() for line in file]\n", "\n", - "# ic(input_lines)" + "ic(time.ctime())\n", + "if use_test:\n", + " ic(input_lines)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "205f3d2b-403d-47cb-8270-145a341b1ecc", "metadata": {}, "outputs": [], "source": [ - "def line_to_test_entry(line: str) -> tuple():\n", + "def line_to_test_entry(line: str) -> tuple:\n", " \"\"\"\n", - " Convert one line of the puzzle input into a tuple()\n", + " Convert one line of the puzzle input into a tuple\n", " - key: the result of the calibration test\n", " - operands: a list of the operands in the equation\n", " \"\"\"\n", @@ -83,113 +123,110 @@ " # ic(line, result)\n", " return result\n", "\n", - "# Test\n", - "# ic(line_to_test_entry(input_lines[0]))" + "if use_test:\n", + " ic(line_to_test_entry(input_lines[0]))" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "d35b2b1a-a493-4c55-8696-a4af4299708a", "metadata": {}, "outputs": [], "source": [ "test_equations = [line_to_test_entry(l) for l in input_lines]\n", "\n", - "# ic(test_equations)" + "if use_test:\n", + " ic(test_equations)" ] }, { "cell_type": "code", - "execution_count": 6, - "id": "5cbc3551-7488-4416-9193-e86b85246736", + "execution_count": 7, + "id": "b01cf872-6e6b-4cbd-b484-3b4ebfede2ab", "metadata": {}, "outputs": [], "source": [ - "# def find_operators(result:int, operands: tuple) -> tuple:\n", - "# \"\"\"\n", - "# Find the operands which applied to operands produce the result\n", - "# Return a list with the operators, or None if no solution is found\n", - "# \"\"\"\n", - "# operators = list()\n", + "def eval_equation(expected_result: int, operands: tuple, operators: tuple) -> bool:\n", + " \"\"\"\n", + " Return True if the equation evaluates to expected_result\n", + " \"\"\"\n", "\n", - "# total = operands[0]\n", - "# # totals = [operands[0]]\n", + " assert len(operators) > 0\n", + " assert len(operands) == len(operators) + 1\n", + " \n", + " total = operands[0]\n", "\n", - "# for o in operands[1:]:\n", - "# for op in ['+', '*']:\n", - "# if op == '+':\n", - "# total += o\n", - "# else:\n", - "# total *= o\n", - "# # ic(total, result, o, op)\n", - "# if total == result:\n", - "# operators.append(op)\n", - "# break\n", - " \n", - "# # ic(result, operands, operators)\n", - "# return tuple(operators)\n", + " for operand, operator in zip(operands[1:], operators):\n", + " # ic(total, operand, operator)\n", + " if operator == '+':\n", + " total += operand\n", + " elif operator == '*':\n", + " total *= operand\n", + " elif operator == '||':\n", + " total = int(str(total) + str(operand))\n", + " else:\n", + " assert False, ic(\"Wrong operator:\", operator)\n", + " if total > expected_result:\n", + " return False\n", "\n", - "# # ic(find_operators(test_equations[0][0], test_equations[0][1])) # 190\n", - "# ic(find_operators(test_equations[1][0], test_equations[1][1])) # 3267" + " return total == expected_result" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "03896837-6eb5-4003-a940-bd637c273656", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "ic| solve_equation(test_equations[1][0], test_equations[1][1]): None\n" - ] - } - ], + "outputs": [], "source": [ - "def solve_equation(result: int, operands: tuple) -> tuple:\n", + "import itertools\n", + "\n", + "def gen_operator(possible_operators: tuple):\n", + " for op in possible_operators:\n", + " yield op\n", + "\n", + "def gen_operators(possible_operators: tuple, length: int):\n", + " for sequence in itertools.product(possible_operators, repeat=length):\n", + " yield sequence\n", + "\n", + "def solve_equation(result: int, operands: tuple, possible_operators: tuple) -> tuple:\n", " # ic(\"solve_equation\", result, operands)\n", " num_operators = len(operands) - 1\n", - " possible_operators = list()\n", - " for k in range(2 ** num_operators):\n", - " seq = bin(k)[2:].zfill(num_operators)\n", - " seq2 = list(seq.replace(\"0\", \"+\").replace(\"1\", \"*\"))\n", - " # ic(result, operands, seq, seq2)\n", - " # ic(\"Trying\", seq2, \"with\", operands, \"to get\", result)\n", - " total = operands[0]\n", - " for operand, operator in zip(operands[1:], seq2):\n", - " # ic(total, operand, operator)\n", - " if operator == '+':\n", - " total += operand\n", - " else:\n", - " total *= operand\n", - " if total > result:\n", - " # ic(\"Got a\", total, \"bigger than\", result, \"--> continue\")\n", - " continue\n", - " # ic(\"After loop:\", seq2, total, result)\n", - " if total == result:\n", - " # ic(\"FOUND\", seq2, total, result)\n", + " \n", + " ops = gen_operators(possible_operators, num_operators)\n", + " for seq2 in ops:\n", + " # ic(\"Testing\", seq2)\n", + " if eval_equation(result, operands, seq2):\n", + " # ic(\"solve_equation: OK for\", result, operands, seq2)\n", " return tuple(seq2)\n", " \n", " # ic(\"WARNING: No results found\", result, total)\n", - " return None\n", - "\n", - "ic(solve_equation(test_equations[1][0], test_equations[1][1])) # 3267" + " return None" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "64b57e8b-70a5-4dc3-b738-25239bcf28f4", "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 0.2131354808807373\n", + " \"result=\": 'result='\n", + " part1_result: 932137732557\n" + ] + }, { "name": "stdout", "output_type": "stream", "text": [ - "Day 07 Part 1: RESULT: 932137732557\n" + "Day 07 Part 1 RESULT:\n", + "932137732557\n" ] } ], @@ -197,91 +234,65 @@ "def solve_part1(test_equations):\n", " result = 0\n", " for eq in test_equations:\n", - " if solve_equation(eq[0], eq[1]) != None:\n", + " if solve_equation(eq[0], eq[1], ('+', '*')) != None:\n", " result += eq[0]\n", " return result\n", "\n", - "print(\"Day 07 Part 1: RESULT:\", solve_part1(test_equations))" + "tm_begin = time.time()\n", + "part1_result = solve_part1(test_equations)\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part1_result)\n", + "print(\"Day 07 Part 1 RESULT:\")\n", + "print(part1_result)" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "51de10dc-4093-4fce-bd21-50ef40cdf1dd", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "00be4c47-79f6-4c21-9859-9fc329f871cb", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "24c136ce-fe8d-4274-8ed9-4423fa9f52d3", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a57a063b-f310-459e-9c2e-bb921a3d3cf7", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "13d150e0-de45-4fc8-b419-195130cee355", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a7d3a2de-90fd-428f-9902-3ee5a5eb109b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b47c8d88-92df-4c5d-8063-b94550af37c2", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b11850d1-2907-4176-8098-b26bf45a83a9", + "cell_type": "markdown", + "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "## Part 2" + ] }, { "cell_type": "code", - "execution_count": null, - "id": "8a366015-d692-4bf7-aa35-1f250d022cb2", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", + "execution_count": 10, + "id": "51de10dc-4093-4fce-bd21-50ef40cdf1dd", "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 15.438014268875122\n", + " \"result=\": 'result='\n", + " part2_result: 661823605105500\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 07 Part 2 RESULT:\n", + "661823605105500\n" + ] + } + ], "source": [ - "## Part 2" + "def solve_part2(test_equations):\n", + " result = 0\n", + " for eq in test_equations:\n", + " if solve_equation(eq[0], eq[1], ('+', '*', '||')) != None:\n", + " result += eq[0]\n", + " return result\n", + "\n", + "tm_begin = time.time()\n", + "part2_result = solve_part2(test_equations)\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n", + "print(\"Day 07 Part 2 RESULT:\")\n", + "print(part2_result)" ] } ], From 44c27152c789a05a9a49e885a693e2eb831eb099 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sat, 7 Dec 2024 16:44:26 +0100 Subject: [PATCH 09/13] feat(solutions/gmacario): day05 Part 2 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day05-jupyter/day05.ipynb | 228 +++++++++++-------- 1 file changed, 128 insertions(+), 100 deletions(-) diff --git a/solutions/gmacario/day05-jupyter/day05.ipynb b/solutions/gmacario/day05-jupyter/day05.ipynb index f34e7f1..75afeff 100644 --- a/solutions/gmacario/day05-jupyter/day05.ipynb +++ b/solutions/gmacario/day05-jupyter/day05.ipynb @@ -17,7 +17,18 @@ "metadata": {}, "outputs": [], "source": [ - "from icecream import ic" + "from icecream import ic\n", + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ba8767da-1138-466c-8756-496c662def09", + "metadata": {}, + "outputs": [], + "source": [ + "# use_test = True # Comment out for using actual puzzle input" ] }, { @@ -30,12 +41,20 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "b8ac6332-ebd1-424b-a781-df1771e81be8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'use_test was undefined - forcing to', use_test: False\n" + ] + } + ], "source": [ - "test_input = \"\"\"47|53\n", + "test = \"\"\"47|53\n", "97|13\n", "97|61\n", "97|47\n", @@ -63,27 +82,37 @@ "75,97,47,61,53\n", "61,13,29\n", "97,13,75,29,47\n", - "\"\"\"" + "\"\"\"\n", + "\n", + "try:\n", + " use_test\n", + "except NameError:\n", + " use_test = False\n", + " ic(\"use_test was undefined - forcing to\", use_test)\n", + " \n", + "if use_test:\n", + " assert test != None" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", "metadata": {}, "outputs": [], "source": [ - "with open(\"input_day05_gmacario.txt\", 'r') as file:\n", - " input_text = file.read()\n", - "\n", - "# input_text = test_input # DEBUG\n", + "if use_test:\n", + " input_text = test\n", + "else:\n", + " with open(\"input_day05_gmacario.txt\", 'r') as file:\n", + " input_text = file.read()\n", "\n", "# ic(input_text)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "48622bfa-12c9-43ff-9758-28047f26047e", "metadata": {}, "outputs": [], @@ -107,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "93c4e5b7-4d9d-4b82-a530-fd045e2fc6ab", "metadata": {}, "outputs": [], @@ -132,22 +161,21 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "ca2ece58-99f7-4657-a1e8-b164b5982a5e", "metadata": {}, "outputs": [], "source": [ - "# Sanity Checks with test1\n", - "#\n", - "# assert is_sequence_ordered(page_numbers[0]) == True\n", - "# assert is_sequence_ordered(page_numbers[1]) == True\n", - "# assert is_sequence_ordered(page_numbers[2]) == True\n", - "# assert is_sequence_ordered(page_numbers[3]) == False" + "if use_test:\n", + " assert is_sequence_ordered(page_numbers[0]) == True\n", + " assert is_sequence_ordered(page_numbers[1]) == True\n", + " assert is_sequence_ordered(page_numbers[2]) == True\n", + " assert is_sequence_ordered(page_numbers[3]) == False" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "2f1e2b3c-db37-4ea3-9e24-7cb8cf15dcde", "metadata": {}, "outputs": [], @@ -160,7 +188,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "d84ac7eb-0518-41bb-a6a7-e1c62086a71d", "metadata": {}, "outputs": [ @@ -168,29 +196,37 @@ "name": "stderr", "output_type": "stream", "text": [ - "ic| 'Part 1 result', total: 5329\n" + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 0.05955862998962402\n", + " \"result=\": 'result='\n", + " part1_result: 5329\n" ] }, { - "data": { - "text/plain": [ - "('Part 1 result', 5329)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 05 Part 1 RESULT:\n", + "5329\n" + ] } ], "source": [ - "total = 0\n", - "for seq in page_numbers:\n", - " # ic(seq)\n", - "\n", - " if is_sequence_ordered(seq):\n", - " total += middle_element(seq) \n", + "def solve_part1():\n", + " total = 0\n", + " for seq in page_numbers:\n", + " # ic(seq)\n", + " \n", + " if is_sequence_ordered(seq):\n", + " total += middle_element(seq)\n", + " return total\n", "\n", - "ic(\"Part 1 result\", total)" + "tm_begin = time.time()\n", + "part1_result = solve_part1()\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part1_result)\n", + "print(\"Day 05 Part 1 RESULT:\")\n", + "print(part1_result)" ] }, { @@ -203,47 +239,32 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "3e522b8b-8eae-40bf-844f-12ff20957c00", "metadata": {}, "outputs": [], "source": [ "def reorder_sequence(seq):\n", " # ic(\"BEGIN reorder_sequence\", seq)\n", - "\n", " # Consider only the page_ordering_rules which contain all element\n", - "\n", - " # ic(len(page_ordering_rules))\n", " relevant_ordering_rules = [r for r in page_ordering_rules if r[0] in seq and r[1] in seq]\n", - " sorted(relevant_ordering_rules)\n", - " # ic(len(relevant_ordering_rules), relevant_ordering_rules)\n", - " \n", - " result = list(seq)\n", + " # ic(len(page_ordering_rules), len(relevant_ordering_rules), relevant_ordering_rules)\n", "\n", - " # for rule in sorted(page_ordering_rules):\n", - " # if rule[0] not in result or rule[1] not in result:\n", - " # # ic(\"DEBUG: Skipping(1) rule\", rule)\n", - " # continue\n", - " # pos1 = result.index(rule[0])\n", - " # pos2 = result.index(rule[1])\n", - " # # ic(pos1, pos2)\n", - " # if pos1 > pos2:\n", - " # # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n", - " # result[pos1], result[pos2] = result[pos2], result[pos1]\n", - " # ic(\"DEBUG: After swapping\", pos1, \"and\", pos2, \"->\", result)\n", - " # # ic(\"DEBUG: Relevant\", rule, result)\n", - "\n", - " for rule in relevant_ordering_rules:\n", - " pos1 = result.index(rule[0])\n", - " pos2 = result.index(rule[1])\n", - " # ic(pos1, pos2)\n", - " if pos1 > pos2:\n", - " # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n", - " result[pos1], result[pos2] = result[pos2], result[pos1]\n", - " # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n", - " # ic(\"DEBUG: Relevant\", rule, result)\n", - " \n", - " #\n", + " result = list(seq)\n", + " must_reorder = True\n", + " while must_reorder:\n", + " must_reorder = False\n", + " for rule in relevant_ordering_rules:\n", + " pos1 = result.index(rule[0])\n", + " pos2 = result.index(rule[1])\n", + " # ic(pos1, pos2)\n", + " if pos1 > pos2:\n", + " # print(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n", + " a, b = result[pos2], result[pos1]\n", + " result[pos1], result[pos2] = a, b\n", + " # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n", + " must_reorder = True\n", + " break\n", " \n", " # ic(\"reorder sequence\", seq, \"->\", result)\n", " # assert is_sequence_ordered(result), ic(seq, result)\n", @@ -252,23 +273,21 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "id": "d7393bcb-135a-470e-a37b-4149449ef1d8", "metadata": {}, "outputs": [], "source": [ - "# Sanity Checks with test1\n", - "#\n", - "# assert is_sequence_ordered(page_numbers[3]) == False\n", - "\n", - "# ic(reorder_sequence(page_numbers[3]))\n", - "# ic(reorder_sequence(page_numbers[4]))\n", - "# ic(reorder_sequence(page_numbers[5]))" + "if use_test:\n", + " assert is_sequence_ordered(page_numbers[3]) == False\n", + " assert reorder_sequence(page_numbers[3]) == [97, 75, 47, 61, 53]\n", + " assert reorder_sequence(page_numbers[4]) == [61, 29, 13]\n", + " assert reorder_sequence(page_numbers[5]) == [97, 75, 47, 29, 13]" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "id": "6ed8e5c4-625b-4c4b-be23-5ccbdeaee80e", "metadata": {}, "outputs": [ @@ -276,16 +295,16 @@ "name": "stderr", "output_type": "stream", "text": [ - "ic| reorder_sequence(page_numbers[0]): [67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]\n" + "ic| reorder_sequence(page_numbers[0]): [67, 82, 22, 76, 69, 45, 98, 47, 37, 35, 36, 39, 73]\n" ] }, { "data": { "text/plain": [ - "[67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]" + "[67, 82, 22, 76, 69, 45, 98, 47, 37, 35, 36, 39, 73]" ] }, - "execution_count": 11, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -299,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "id": "9e518d75-370f-495c-9a86-8887562ef15b", "metadata": {}, "outputs": [ @@ -307,31 +326,40 @@ "name": "stderr", "output_type": "stream", "text": [ - "ic| 'Part 2 result', total: 5896\n" + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 0.12361502647399902\n", + " \"result=\": 'result='\n", + " part2_result: 5833\n" ] }, { - "data": { - "text/plain": [ - "('Part 2 result', 5896)" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 05 Part 2 RESULT:\n", + "5833\n" + ] } ], "source": [ - "total = 0\n", - "for seq in page_numbers:\n", - " # ic(seq)\n", - "\n", - " if not is_sequence_ordered(seq):\n", - " ordered_seq = reorder_sequence(seq)\n", - " # ic(\"DEBUG: incorrectly-ordered\", seq, \"->\", ordered_seq)\n", - " total += middle_element(ordered_seq)\n", + "def solve_part2():\n", + " total = 0\n", + " for seq in page_numbers:\n", + " # ic(seq)\n", + " \n", + " if not is_sequence_ordered(seq):\n", + " ordered_seq = reorder_sequence(seq)\n", + " # ic(\"DEBUG: incorrectly-ordered\", seq, \"->\", ordered_seq)\n", + " total += middle_element(ordered_seq)\n", + " return total\n", "\n", - "ic(\"Part 2 result\", total)" + "tm_begin = time.time()\n", + "part2_result = solve_part2()\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n", + "print(\"Day 05 Part 2 RESULT:\")\n", + "print(part2_result)\n", + "# assert part2_result == 123 # test_data" ] } ], From 9472c8aa278834da67871126da4931141536fed9 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sun, 8 Dec 2024 07:38:12 +0100 Subject: [PATCH 10/13] wip(solutions/gmacario): debugging day08 part1 Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day08/day08.ipynb | 395 ++++++++++++++++++ .../gmacario/day08/input_day08_gmacario.txt | 50 +++ 2 files changed, 445 insertions(+) create mode 100644 solutions/gmacario/day08/day08.ipynb create mode 100644 solutions/gmacario/day08/input_day08_gmacario.txt diff --git a/solutions/gmacario/day08/day08.ipynb b/solutions/gmacario/day08/day08.ipynb new file mode 100644 index 0000000..a48b72c --- /dev/null +++ b/solutions/gmacario/day08/day08.ipynb @@ -0,0 +1,395 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673", + "metadata": {}, + "source": [ + "# AoC 2024 - Day 8\n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", + "metadata": {}, + "outputs": [], + "source": [ + "from icecream import ic\n", + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6c8149a1-7aed-4bfb-8ec9-748c65077d45", + "metadata": {}, + "outputs": [], + "source": [ + "# use_test = True # Comment out for using actual puzzle input" + ] + }, + { + "cell_type": "markdown", + "id": "f3458754-aa32-44e5-b229-4f519108459e", + "metadata": {}, + "source": [ + "## Part 1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b8ac6332-ebd1-424b-a781-df1771e81be8", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'use_test was undefined - forcing to', use_test: False\n" + ] + } + ], + "source": [ + "test_data = \"\"\"............\n", + "........0...\n", + ".....0......\n", + ".......0....\n", + "....0.......\n", + "......A.....\n", + "............\n", + "............\n", + "........A...\n", + ".........A..\n", + "............\n", + "............\n", + "\"\"\"\n", + "\n", + "try:\n", + " use_test\n", + "except NameError:\n", + " use_test = False\n", + " ic(\"use_test was undefined - forcing to\", use_test)\n", + " \n", + "if use_test:\n", + " assert test_data != None" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| time.ctime(): 'Sun Dec 8 06:35:35 2024'\n" + ] + } + ], + "source": [ + "# Read the puzzle input into a list of strings, one per line\n", + "#\n", + "if use_test:\n", + " input_lines = test_data.splitlines() # Uncomment for debug\n", + "else:\n", + " with open(\"input_day08_gmacario.txt\", 'r') as file:\n", + " input_lines = [line.rstrip() for line in file]\n", + "\n", + "ic(time.ctime())\n", + "if use_test:\n", + " ic(input_lines)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "6cdc2816-078d-46ca-a615-6f8fb74d4a2f", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| \"map_width\": 'map_width'\n", + " len(input_map[0]): 50\n", + " \"map_height\": 'map_height'\n", + " len(input_map): 50\n" + ] + }, + { + "data": { + "text/plain": [ + "('map_width', 50, 'map_height', 50)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "input_map = [list(l) for l in input_lines]\n", + "assert(all(len(l) == len(input_map[0]) for l in input_map ))\n", + "# ic(input_map)\n", + "ic(\"map_width\", len(input_map[0]), \"map_height\", len(input_map))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "221cb8d5-cd33-46da-a04d-7e01c4088f89", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('V',\n", + " 'b',\n", + " 'g',\n", + " 'c',\n", + " 'T',\n", + " 'Z',\n", + " 'P',\n", + " 'x',\n", + " 'H',\n", + " 'R',\n", + " 'f',\n", + " 'A',\n", + " 'l',\n", + " 'k',\n", + " '5',\n", + " 'S',\n", + " '0',\n", + " 'N',\n", + " 'L',\n", + " 's',\n", + " 't',\n", + " '6',\n", + " 'n',\n", + " 'r',\n", + " 'K',\n", + " 'E',\n", + " 'm',\n", + " '3',\n", + " 'W',\n", + " '2',\n", + " 'Q',\n", + " 'M',\n", + " 'h',\n", + " '1',\n", + " 'z',\n", + " '4',\n", + " 'e',\n", + " 'Y',\n", + " 'y',\n", + " 'a',\n", + " 'v',\n", + " 'q',\n", + " '7',\n", + " '9',\n", + " 'X',\n", + " '8',\n", + " 'B',\n", + " 'C',\n", + " 'G',\n", + " 'p',\n", + " 'F',\n", + " 'D',\n", + " 'd',\n", + " 'w')" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def get_antenna_frequencies(input_map) -> tuple:\n", + " \"\"\"\n", + " Return a tuple with all the antenna frequencies in input_map\n", + " \"\"\"\n", + " result = []\n", + " for r in range(len(input_map)):\n", + " for c in range(len(input_map[0])):\n", + " ch = input_map[r][c]\n", + " if ch.isalnum() and not ch in result:\n", + " result.append(ch)\n", + " # ic(\"DEBUG: After adding\", ch, result)\n", + " return tuple(result)\n", + "\n", + "get_antenna_frequencies(input_map)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "1947dafc-74f0-4d88-9e1c-5c39393dbdb3", + "metadata": {}, + "outputs": [], + "source": [ + "def add_antinodes(input_map) -> int:\n", + " \"\"\"\n", + " Return the number of antinodes added to input_map\n", + " \"\"\"\n", + " # ic(\"BEGIN add_antinodes\")\n", + " result = 0\n", + " antenna_freqs = get_antenna_frequencies(input_map)\n", + " map_width = len(input_map)\n", + " map_height = len(input_map[0])\n", + " for r in range(map_width):\n", + " for c in range(map_height):\n", + " ch = input_map[r][c]\n", + " if ch in antenna_freqs:\n", + " # ic(\"Checking antenna\", ch, \"at\", (r,c))\n", + " for r2 in range(map_width):\n", + " for c2 in range(map_height):\n", + " ch2 = input_map[r2][c2]\n", + " if ch == ch2:\n", + " # Antenna found at (r2,c2)\n", + " # Find the position of reflection\n", + " r3 = (r - r2) + r\n", + " c3 = (c - c2) + c\n", + " # ic(\"Checking\", (r3,c3))\n", + " if r3 < 0 or r3 >= map_height:\n", + " pass\n", + " elif c3 < 0 or c3 >= map_width:\n", + " pass\n", + " elif (r3,c3) == (r,c):\n", + " pass\n", + " else:\n", + " ch3 = input_map[r3][c3]\n", + " # ic(\"Candidate location\", (r3,c3), \"contains\", ch3)\n", + " if ch3 == \".\":\n", + " # ic(\"Antinode placed in\", (r3,c3))\n", + " input_map[r3][c3] = \"#\"\n", + " result += 1\n", + " elif ch3 == \"#\":\n", + " # ic(\"Antinode already placed at\", (r3,c3))\n", + " pass\n", + " else:\n", + " # ic(\"Antenna\", ch, \"at\", (r3,c3), \"is an antinode\")\n", + " result += 1\n", + "\n", + " # ic(\"END add_antinodes\", result)\n", + " return result\n", + "\n", + "# ic(add_antinodes(input_map))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "793a08d6-09b2-45be-86a8-96b2a41891e3", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 0.018702030181884766\n", + " \"result=\": 'result='\n", + " part1_result: 394\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 08 Part 1 RESULT:\n", + "394\n" + ] + } + ], + "source": [ + "def solve_part1(input_map):\n", + " return add_antinodes(input_map)\n", + "\n", + "tm_begin = time.time()\n", + "part1_result = solve_part1(input_map)\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part1_result)\n", + "print(\"Day 08 Part 1 RESULT:\")\n", + "print(part1_result)" + ] + }, + { + "cell_type": "markdown", + "id": "29f1e0f9-95e7-4261-91d1-6bbbef8f0279", + "metadata": {}, + "source": [ + "## TODO" + ] + }, + { + "cell_type": "markdown", + "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", + "metadata": {}, + "source": [ + "## Part 2" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "51de10dc-4093-4fce-bd21-50ef40cdf1dd", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'test_equations' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n\u001b[1;32m 8\u001b[0m tm_begin \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[0;32m----> 9\u001b[0m part2_result \u001b[38;5;241m=\u001b[39m solve_part2(\u001b[43mtest_equations\u001b[49m)\n\u001b[1;32m 10\u001b[0m tm_end \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 11\u001b[0m ic(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124melapsed=\u001b[39m\u001b[38;5;124m\"\u001b[39m, tm_end \u001b[38;5;241m-\u001b[39m tm_begin, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mresult=\u001b[39m\u001b[38;5;124m\"\u001b[39m, part2_result)\n", + "\u001b[0;31mNameError\u001b[0m: name 'test_equations' is not defined" + ] + } + ], + "source": [ + "def solve_part2(test_equations):\n", + " result = 0\n", + " for eq in test_equations:\n", + " if solve_equation(eq[0], eq[1], ('+', '*', '||')) != None:\n", + " result += eq[0]\n", + " return result\n", + "\n", + "tm_begin = time.time()\n", + "part2_result = solve_part2(test_equations)\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n", + "print(\"Day 07 Part 2 RESULT:\")\n", + "print(part2_result)" + ] + } + ], + "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/day08/input_day08_gmacario.txt b/solutions/gmacario/day08/input_day08_gmacario.txt new file mode 100644 index 0000000..17d1fd1 --- /dev/null +++ b/solutions/gmacario/day08/input_day08_gmacario.txt @@ -0,0 +1,50 @@ +...........V..................b.g................. +..................................g............... +.............................c.................... +............T........Z.......P.................... +.x........................VP...................... +..........................PH...................... +.................H.....Z.......g.R................ +......f............T.V....b......A................ +......................P........................... +.......f..................A.............R......... +........x..............T.......l..H.....A.c....... +..k..x..............Z............................. +........5....S...............0.A.................. +.............N....L............................... +.f............................T........s.....N.... +..................l..........bH.......tc.R..N..... +......Z...6......n......l...k.N...0............... +...........g....S......l.r.................t..s... +..L................b.......K..t................... +................5....n........0.............c..... +.....L......n............................E........ +.k.......L................m.....................Es +..............St.....5....Rm...................... +............6..5...................3...0.......... +...........k.................W........3........... +................n......K...E....2S..........3..... +....................................E....Q........ +..........M.....x...............K................. +..h.............................1................. +.6............z..............4...e.........WY....y +........f............a.......Y..y...s............. +...h............r.............v....m.............. +.....h.................v....m.....Y.Q.....W3...... +.........................Yq....Q.................7 +.........6..............7.................9....... +...................X..........y..q.....2.......... +............r..............q.....y...........7.8.. +..B..............M....4............9.............. +...1.......M...X.......CGzp...4..B...2..K......... +.....................z...v....Q.....8...........9. +B.......X.F....rM...v...............2...8..D...... +h1..............................7..D.....8....d... +...............F.....................9D....4....d. +..........a......p............F.........W.D......d +.........................G..C...........q......... +...B..................................C........... +.........w..........z....p.....................e.. +.a............G....w........p........F........e... +........a...w..................................... +........w...............XC.......G................ From fcc0dfa0496ce9f7eb920c42b7c666c6708735c0 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sun, 8 Dec 2024 08:05:58 +0100 Subject: [PATCH 11/13] feat(solutions/gmacario): day08 part1 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day08/day08.ipynb | 100 ++++++--------------------- 1 file changed, 21 insertions(+), 79 deletions(-) diff --git a/solutions/gmacario/day08/day08.ipynb b/solutions/gmacario/day08/day08.ipynb index a48b72c..ecb0dcb 100644 --- a/solutions/gmacario/day08/day08.ipynb +++ b/solutions/gmacario/day08/day08.ipynb @@ -88,7 +88,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "ic| time.ctime(): 'Sun Dec 8 06:35:35 2024'\n" + "ic| time.ctime(): 'Sun Dec 8 07:04:43 2024'\n" ] } ], @@ -145,71 +145,7 @@ "execution_count": 6, "id": "221cb8d5-cd33-46da-a04d-7e01c4088f89", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "('V',\n", - " 'b',\n", - " 'g',\n", - " 'c',\n", - " 'T',\n", - " 'Z',\n", - " 'P',\n", - " 'x',\n", - " 'H',\n", - " 'R',\n", - " 'f',\n", - " 'A',\n", - " 'l',\n", - " 'k',\n", - " '5',\n", - " 'S',\n", - " '0',\n", - " 'N',\n", - " 'L',\n", - " 's',\n", - " 't',\n", - " '6',\n", - " 'n',\n", - " 'r',\n", - " 'K',\n", - " 'E',\n", - " 'm',\n", - " '3',\n", - " 'W',\n", - " '2',\n", - " 'Q',\n", - " 'M',\n", - " 'h',\n", - " '1',\n", - " 'z',\n", - " '4',\n", - " 'e',\n", - " 'Y',\n", - " 'y',\n", - " 'a',\n", - " 'v',\n", - " 'q',\n", - " '7',\n", - " '9',\n", - " 'X',\n", - " '8',\n", - " 'B',\n", - " 'C',\n", - " 'G',\n", - " 'p',\n", - " 'F',\n", - " 'D',\n", - " 'd',\n", - " 'w')" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def get_antenna_frequencies(input_map) -> tuple:\n", " \"\"\"\n", @@ -224,7 +160,7 @@ " # ic(\"DEBUG: After adding\", ch, result)\n", " return tuple(result)\n", "\n", - "get_antenna_frequencies(input_map)" + "# get_antenna_frequencies(input_map)" ] }, { @@ -234,12 +170,12 @@ "metadata": {}, "outputs": [], "source": [ - "def add_antinodes(input_map) -> int:\n", + "def find_antinodes(input_map: list, antenna_freq: str) -> set:\n", " \"\"\"\n", " Return the number of antinodes added to input_map\n", " \"\"\"\n", - " # ic(\"BEGIN add_antinodes\")\n", - " result = 0\n", + " # ic(\"BEGIN find_antinodes\", antenna_freq)\n", + " result = set()\n", " antenna_freqs = get_antenna_frequencies(input_map)\n", " map_width = len(input_map)\n", " map_height = len(input_map[0])\n", @@ -268,19 +204,19 @@ " # ic(\"Candidate location\", (r3,c3), \"contains\", ch3)\n", " if ch3 == \".\":\n", " # ic(\"Antinode placed in\", (r3,c3))\n", - " input_map[r3][c3] = \"#\"\n", - " result += 1\n", + " # input_map[r3][c3] = \"#\"\n", + " result.add((r3, c3))\n", " elif ch3 == \"#\":\n", " # ic(\"Antinode already placed at\", (r3,c3))\n", " pass\n", " else:\n", " # ic(\"Antenna\", ch, \"at\", (r3,c3), \"is an antinode\")\n", - " result += 1\n", + " result.add((r3, c3))\n", "\n", - " # ic(\"END add_antinodes\", result)\n", + " # ic(\"END find_antinodes\", result)\n", " return result\n", "\n", - "# ic(add_antinodes(input_map))" + "# ic(find_antinodes(input_map, \"0\"))" ] }, { @@ -294,9 +230,9 @@ "output_type": "stream", "text": [ "ic| \"elapsed=\": 'elapsed='\n", - " tm_end - tm_begin: 0.018702030181884766\n", + " tm_end - tm_begin: 1.0230634212493896\n", " \"result=\": 'result='\n", - " part1_result: 394\n" + " part1_result: 392\n" ] }, { @@ -304,13 +240,19 @@ "output_type": "stream", "text": [ "Day 08 Part 1 RESULT:\n", - "394\n" + "392\n" ] } ], "source": [ "def solve_part1(input_map):\n", - " return add_antinodes(input_map)\n", + " result = set()\n", + " for antenna_freq in get_antenna_frequencies(input_map):\n", + " x = find_antinodes(input_map, antenna_freq)\n", + " # ic(x)\n", + " result = result.union(x)\n", + " # ic(x, result)\n", + " return len(result)\n", "\n", "tm_begin = time.time()\n", "part1_result = solve_part1(input_map)\n", From 44eefc5dd1b781b20c2e67c9a8b49e210a24d861 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sun, 8 Dec 2024 08:53:26 +0100 Subject: [PATCH 12/13] feat(solutions/gmacario): day08 part2 solved Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day08/day08.ipynb | 152 ++++++++++++++++++--------- 1 file changed, 102 insertions(+), 50 deletions(-) diff --git a/solutions/gmacario/day08/day08.ipynb b/solutions/gmacario/day08/day08.ipynb index ecb0dcb..1527d45 100644 --- a/solutions/gmacario/day08/day08.ipynb +++ b/solutions/gmacario/day08/day08.ipynb @@ -88,7 +88,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "ic| time.ctime(): 'Sun Dec 8 07:04:43 2024'\n" + "ic| time.ctime(): 'Sun Dec 8 07:52:38 2024'\n" ] } ], @@ -170,11 +170,11 @@ "metadata": {}, "outputs": [], "source": [ - "def find_antinodes(input_map: list, antenna_freq: str) -> set:\n", + "def find_antinodes(input_map: list, antenna_freq: str, multiple=False) -> set:\n", " \"\"\"\n", " Return the number of antinodes added to input_map\n", " \"\"\"\n", - " # ic(\"BEGIN find_antinodes\", antenna_freq)\n", + " # ic(\"BEGIN find_antinodes\", antenna_freq, multiple)\n", " result = set()\n", " antenna_freqs = get_antenna_frequencies(input_map)\n", " map_width = len(input_map)\n", @@ -182,38 +182,45 @@ " for r in range(map_width):\n", " for c in range(map_height):\n", " ch = input_map[r][c]\n", - " if ch in antenna_freqs:\n", + " if ch == antenna_freq:\n", " # ic(\"Checking antenna\", ch, \"at\", (r,c))\n", " for r2 in range(map_width):\n", " for c2 in range(map_height):\n", " ch2 = input_map[r2][c2]\n", " if ch == ch2:\n", - " # Antenna found at (r2,c2)\n", - " # Find the position of reflection\n", - " r3 = (r - r2) + r\n", - " c3 = (c - c2) + c\n", - " # ic(\"Checking\", (r3,c3))\n", - " if r3 < 0 or r3 >= map_height:\n", - " pass\n", - " elif c3 < 0 or c3 >= map_width:\n", - " pass\n", - " elif (r3,c3) == (r,c):\n", - " pass\n", - " else:\n", - " ch3 = input_map[r3][c3]\n", - " # ic(\"Candidate location\", (r3,c3), \"contains\", ch3)\n", - " if ch3 == \".\":\n", - " # ic(\"Antinode placed in\", (r3,c3))\n", - " # input_map[r3][c3] = \"#\"\n", - " result.add((r3, c3))\n", - " elif ch3 == \"#\":\n", - " # ic(\"Antinode already placed at\", (r3,c3))\n", + " # ic(\"Antenna\", ch2, \"found at\", (r2,c2))\n", + " keep_on_finding = True\n", + " r3, c3 = r, c\n", + " delta_r, delta_c = r - r2, c - c2\n", + " while (keep_on_finding): \n", + " # Find the position of reflection\n", + " r3 += delta_r\n", + " c3 += delta_c\n", + " # ic(\"Checking\", (r3,c3))\n", + " if r3 < 0 or r3 >= map_height:\n", + " keep_on_finding = False\n", + " elif c3 < 0 or c3 >= map_width:\n", + " keep_on_finding = False\n", + " elif (r3,c3) == (r,c):\n", + " keep_on_finding = False\n", + " else:\n", + " ch3 = input_map[r3][c3]\n", + " # ic(\"Candidate location\", (r3,c3), \"contains\", ch3)\n", + " if ch3 == \".\":\n", + " # ic(\"Antinode placed in\", (r3,c3))\n", + " result.add((r3, c3))\n", + " else:\n", + " # ic(\"Antenna\", ch, \"at\", (r3,c3), \"is an antinode\")\n", + " result.add((r3, c3))\n", + " if multiple:\n", + " result.add((r,c))\n", + " result.add((r2,c2))\n", + " ...\n", " pass\n", " else:\n", - " # ic(\"Antenna\", ch, \"at\", (r3,c3), \"is an antinode\")\n", - " result.add((r3, c3))\n", + " keep_on_finding = False\n", "\n", - " # ic(\"END find_antinodes\", result)\n", + " # ic(\"END find_antinodes\", antenna_freq, len(result))\n", " return result\n", "\n", "# ic(find_antinodes(input_map, \"0\"))" @@ -230,7 +237,7 @@ "output_type": "stream", "text": [ "ic| \"elapsed=\": 'elapsed='\n", - " tm_end - tm_begin: 1.0230634212493896\n", + " tm_end - tm_begin: 0.03243112564086914\n", " \"result=\": 'result='\n", " part1_result: 392\n" ] @@ -264,51 +271,96 @@ }, { "cell_type": "markdown", - "id": "29f1e0f9-95e7-4261-91d1-6bbbef8f0279", + "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", "metadata": {}, "source": [ - "## TODO" + "## Part 2" ] }, { - "cell_type": "markdown", - "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", + "cell_type": "code", + "execution_count": 9, + "id": "05db4eaf-fd33-4496-a669-7adb9dd45b40", "metadata": {}, + "outputs": [], "source": [ - "## Part 2" + "# ic(input_map)\n", + "# ic(get_antenna_frequencies(input_map))" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, + "id": "e15bdbba-6b57-4d70-825c-77cd5cba8c42", + "metadata": {}, + "outputs": [], + "source": [ + "# set_0 = find_antinodes(input_map, \"0\", multiple=True)\n", + "# _ = ic(len(set_0), set_0)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "37f51cde-4f9e-4c25-8b5b-7011e5a3d68a", + "metadata": {}, + "outputs": [], + "source": [ + "# set_A = find_antinodes(input_map, \"A\", multiple=True)\n", + "# _ = ic(len(set_A), set_A)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "342d516a-2032-4350-affa-e0b18b5c817e", + "metadata": {}, + "outputs": [], + "source": [ + "# set_total = set_0.union(set_A)\n", + "# _ = ic(len(set_total), set_total)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, "id": "51de10dc-4093-4fce-bd21-50ef40cdf1dd", "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'test_equations' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[9], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n\u001b[1;32m 8\u001b[0m tm_begin \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[0;32m----> 9\u001b[0m part2_result \u001b[38;5;241m=\u001b[39m solve_part2(\u001b[43mtest_equations\u001b[49m)\n\u001b[1;32m 10\u001b[0m tm_end \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 11\u001b[0m ic(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124melapsed=\u001b[39m\u001b[38;5;124m\"\u001b[39m, tm_end \u001b[38;5;241m-\u001b[39m tm_begin, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mresult=\u001b[39m\u001b[38;5;124m\"\u001b[39m, part2_result)\n", - "\u001b[0;31mNameError\u001b[0m: name 'test_equations' is not defined" + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 0.07219767570495605\n", + " \"result=\": 'result='\n", + " part2_result: 1235\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 08 Part 2 RESULT:\n", + "1235\n" ] } ], "source": [ - "def solve_part2(test_equations):\n", - " result = 0\n", - " for eq in test_equations:\n", - " if solve_equation(eq[0], eq[1], ('+', '*', '||')) != None:\n", - " result += eq[0]\n", - " return result\n", + "def solve_part2(input_map):\n", + " result = set()\n", + " for antenna_freq in get_antenna_frequencies(input_map):\n", + " x = find_antinodes(input_map, antenna_freq, multiple=True)\n", + " # ic(x)\n", + " result = result.union(x)\n", + " # ic(x, result)\n", + " return len(result)\n", "\n", "tm_begin = time.time()\n", - "part2_result = solve_part2(test_equations)\n", + "part2_result = solve_part2(input_map)\n", "tm_end = time.time()\n", "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n", - "print(\"Day 07 Part 2 RESULT:\")\n", + "print(\"Day 08 Part 2 RESULT:\")\n", "print(part2_result)" ] } From 508394b87bde2fbb79e319b5ce75ec50d7cc33a5 Mon Sep 17 00:00:00 2001 From: Gianpaolo Macario Date: Sun, 8 Dec 2024 13:10:37 +0100 Subject: [PATCH 13/13] feat(solutions/gmacario): day06 part2 implemented, but terribly slow! Signed-off-by: Gianpaolo Macario --- solutions/gmacario/day06-jupyter/day06.ipynb | 629 +++++++++++++------ 1 file changed, 446 insertions(+), 183 deletions(-) diff --git a/solutions/gmacario/day06-jupyter/day06.ipynb b/solutions/gmacario/day06-jupyter/day06.ipynb index 7c56d60..1e8c526 100644 --- a/solutions/gmacario/day06-jupyter/day06.ipynb +++ b/solutions/gmacario/day06-jupyter/day06.ipynb @@ -14,15 +14,49 @@ "cell_type": "code", "execution_count": 1, "id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ - "from icecream import ic" + "from icecream import ic\n", + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ce95fcd8-b43c-47b9-892a-fbeb03ab88cf", + "metadata": {}, + "outputs": [], + "source": [ + "# use_test = True # Comment out for using actual puzzle input" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d5ac04f4-ea76-4e7b-a1b7-30db5b5cb586", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| time.ctime(): 'Sun Dec 8 12:06:56 2024'\n" + ] + }, + { + "data": { + "text/plain": [ + "'Sun Dec 8 12:06:56 2024'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ic(time.ctime())" ] }, { @@ -35,17 +69,20 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "id": "b8ac6332-ebd1-424b-a781-df1771e81be8", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'WARNING: use_test undefined - forcing', use_test: False\n" + ] } - }, - "outputs": [], + ], "source": [ - "test_input = \"\"\"....#.....\n", + "test_data = \"\"\"....#.....\n", ".........#\n", "..........\n", "..#.......\n", @@ -55,39 +92,42 @@ "........#.\n", "#.........\n", "......#...\n", - "\"\"\"" + "\"\"\"\n", + "\n", + "try:\n", + " use_test\n", + "except NameError:\n", + " use_test = False\n", + " ic(\"WARNING: use_test undefined - forcing\", use_test)\n", + " \n", + "if use_test:\n", + " assert test_data != None" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ - "with open(\"input_day06_gmacario.txt\", 'r') as file:\n", - " input_text = file.read()\n", - "\n", - "# input_text = test_input # DEBUG\n", + "# Read the puzzle input into a list of strings, one per line\n", + "#\n", + "if use_test:\n", + " input_lines = test_data.splitlines() # Uncomment for debug\n", + "else:\n", + " with open(\"input_day06_gmacario.txt\", 'r') as file:\n", + " input_lines = [line.rstrip() for line in file]\n", "\n", - "# ic(input_text)" + "# if use_test:\n", + "# ic(input_lines)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "6938a781-bdb9-4c95-b56a-b990c68c448f", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [ { "name": "stderr", @@ -98,8 +138,8 @@ } ], "source": [ - "lab_map = list()\n", - "for l in input_text.split():\n", + "input_map = list()\n", + "for l in input_lines:\n", " # ic(l)\n", " # l = [l[ch:ch+1] for ch in range(len(l))]\n", " map_row = []\n", @@ -107,50 +147,40 @@ " 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", + " input_map.append(map_row)\n", "\n", - "map_width = len(lab_map[0])\n", - "map_height = len(lab_map)\n", - "# ic(lab_map)\n", + "map_width = len(input_map[0])\n", + "map_height = len(input_map)\n", + "# ic(input_map)\n", "ic(map_width, map_height)\n", - "assert all([len(row) == map_width for row in lab_map])" + "assert all([len(row) == map_width for row in input_map])" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "aa1580c1-3f81-4779-ad48-d6b12e9fddac", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ - "def count_element(el):\n", + "def count_element(lab_map, 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)])" + " return sum([row[c] == el for row in lab_map for c in range(len(lab_map))])" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "id": "541d53ed-3b6c-46e5-9fb1-77792ee3fd16", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "ic| [count_element(ch) for ch in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"]]: [16082, 1, 0, 0, 0, 817, 0]\n" + "ic| [count_element(input_map, ch) for ch in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"]]: [16082, 1, 0, 0, 0, 817, 0]\n" ] }, { @@ -159,25 +189,20 @@ "[16082, 1, 0, 0, 0, 817, 0]" ] }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "ic([count_element(ch) for ch in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"]])" + "ic([count_element(input_map, ch) for ch in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\"]])" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "id": "42eb54db-e0d9-4aa1-9e94-96ae895e9386", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ "def sanity_checks(lab_map):\n", @@ -186,22 +211,17 @@ " # 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 all([row[c] in [\".\", \"^\", \"V\", \"<\", \">\", \"#\", \"X\", \"|\", \"-\", \"+\", \"O\"] 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)" + "sanity_checks(input_map)" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "id": "dd102a37-15be-4067-ab1e-e2f749904754", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ "def find_guard(lab_map):\n", @@ -227,14 +247,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "34bfe1f0-fceb-401e-8e27-50be34dcddaa", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ "def move_one_step(lab_map):\n", @@ -247,10 +262,11 @@ " (new_r, new_c, new_d) = (r, c, d)\n", " if d == \"UP\":\n", " if r == 0:\n", + " lab_map[r][c] = \"X\"\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", + " # ic(\"Block found in front of\", (r, c), \"move RIGHT\")\n", " lab_map[r][c] = \">\"\n", " new_d = \"RIGHT\"\n", " else:\n", @@ -258,10 +274,11 @@ " lab_map[new_r][new_c] = \"^\"\n", " elif d == \"RIGHT\":\n", " if c == map_width-1:\n", + " lab_map[r][c] = \"X\"\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", + " # ic(\"Block found in front of\", (r, c), \"move DOWN\")\n", " lab_map[r][c] = \"V\"\n", " new_d = \"DOWN\"\n", " else:\n", @@ -269,22 +286,23 @@ " lab_map[new_r][new_c] = \">\"\n", " elif d == \"DOWN\":\n", " if r == map_height-1:\n", + " lab_map[r][c] = \"X\"\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", + " # 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", + " lab_map[r][c] = \"X\"\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", + " # ic(\"Block found in front of\", (r, c), \"move UP\")\n", " lab_map[r][c] = \"^\"\n", " new_d = \"UP\"\n", " else:\n", @@ -298,150 +316,403 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "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" - } - ], + "metadata": {}, + "outputs": [], "source": [ - "ic(find_guard(lab_map))" + "# ic(find_guard(lab_map))" ] }, { "cell_type": "code", - "execution_count": 11, - "id": "98afd4ce-4cc8-4da7-89d7-4a89901cb02e", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "execution_count": 13, + "id": "97e0ec3c-528f-4982-a2a1-88a93a534dee", + "metadata": {}, "outputs": [], "source": [ - "# Part 1: " + "# ic(lab_map)\n", + "# move_one_step(lab_map)\n", + "# ic(lab_map)" + ] + }, + { + "cell_type": "markdown", + "id": "07ef008f-6e16-4e5e-b375-95cd7ab62b69", + "metadata": {}, + "source": [ + "## Part 1" ] }, { "cell_type": "code", - "execution_count": 12, - "id": "97e0ec3c-528f-4982-a2a1-88a93a534dee", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "execution_count": 14, + "id": "f9727cab-508a-49a3-922e-dc76f46f97ff", + "metadata": {}, "outputs": [], "source": [ - "# ic(lab_map)\n", - "# move_one_step(lab_map)\n", - "# ic(lab_map)" + "import copy\n", + "\n", + "# ic(input_map)\n", + "traversed_map = copy.deepcopy(input_map)\n", + "# ic(traversed_map)" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "id": "b81767bf-570e-4cfb-873f-9cc1ac535658", "metadata": {}, + "outputs": [], + "source": [ + "def solve_part1(lab_map):\n", + " while not move_one_step(lab_map):\n", + " # ic(\"ONE STEP FURTHER:\", count_element(\"X\"))\n", + " pass\n", + " return count_element(lab_map, \"X\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "8a366015-d692-4bf7-aa35-1f250d022cb2", + "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "ic| 'PART1 RESULT', count_element(\"X\") + 1: 4977\n" + "ic| \"elapsed=\": 'elapsed='\n", + " tm_end - tm_begin: 12.0693519115448\n", + " \"result=\": 'result='\n", + " part1_result: 4977\n" ] }, { - "data": { - "text/plain": [ - "('PART1 RESULT', 4977)" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Day 06 Part 1 RESULT:\n", + "4977\n" + ] } ], "source": [ - "while not move_one_step(lab_map):\n", - " # ic(\"ONE STEP FURTHER:\", count_element(\"X\"))\n", + "tm_begin = time.time()\n", + "part1_result = solve_part1(traversed_map)\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part1_result)\n", + "print(\"Day 06 Part 1 RESULT:\")\n", + "print(part1_result)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "6334047b-05ca-488e-8f7a-92e795d3b434", + "metadata": {}, + "outputs": [], + "source": [ + "# _ = ic(input_map)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "47464599-5809-494a-8b30-bd47e23f0068", + "metadata": {}, + "outputs": [], + "source": [ + "# _ = ic(traversed_map)" + ] + }, + { + "cell_type": "markdown", + "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", + "metadata": {}, + "source": [ + "## Part 2" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "1b3623fc-371e-4826-808e-a03868bfc1e9", + "metadata": {}, + "outputs": [], + "source": [ + "# Sanity checks with test_data\n", + "if use_test:\n", + " # ic(input_map)\n", + " num_x = count_element(input_map, \"X\")\n", + " assert num_x == 0, ic(num_x)\n", + " # ic(traversed_map)\n", + " num_x = count_element(traversed_map, \"X\")\n", + " assert num_x == 41, ic(num_x)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "40e268ba-dd02-4197-ad83-fb66316f35a3", + "metadata": {}, + "outputs": [], + "source": [ + "def move_one_step_part2(lab_map):\n", + " \"\"\"\n", + " Return values:\n", + " \n", + " * 1 if moving out of map\n", + " * 2 if loop detected\n", + " * 0 in all other cases\n", + " \n", + " \"\"\"\n", + " result = 0\n", + "\n", + " (r, c, d) = find_guard(lab_map)\n", + " # ic(r, c, d)\n", + " \n", + " (new_r, new_c, new_d) = (r, c, d)\n", + " if d == \"UP\":\n", + " new_r = r - 1\n", + " if r == 0:\n", + " # ic(\"Going up moves out of map\")\n", + " return 1\n", + " ch = lab_map[new_r][new_c]\n", + " if ch == \"+\":\n", + " ic(\"Loop detected\")\n", + " return 2\n", + " elif ch in [\"#\", \"O\"]:\n", + " # ic(\"Block\", ch, \"found in front of\", (r, c), \"==> move RIGHT\")\n", + " lab_map[r][c] = \"+\"\n", + " new_r, new_c = r, c + 1\n", + " lab_map[new_r][new_c] = \">\"\n", + " new_d = \"RIGHT\"\n", + " else:\n", + " lab_map[r][c] = \"|\"\n", + " lab_map[new_r][new_c] = \"^\"\n", + " elif d == \"RIGHT\":\n", + " new_c = c + 1\n", + " if c == map_width-1:\n", + " # ic(\"Going right moves out of map\")\n", + " return 1\n", + " new_d = \"DOWN\"\n", + " ch = lab_map[new_r][new_c]\n", + " if ch == \"+\":\n", + " ic(\"Loop detected\")\n", + " return 2\n", + " elif ch in [\"#\", \"O\"]:\n", + " # ic(\"Block\", ch, \"found in front of\", (r, c), \"==> move DOWN\")\n", + " lab_map[r][c] = \"+\"\n", + " new_r, new_c = r + 1, c\n", + " lab_map[new_r][new_c] = \"V\"\n", + " else:\n", + " lab_map[r][c] = \"-\"\n", + " lab_map[new_r][new_c] = \">\"\n", + " elif d == \"DOWN\":\n", + " new_r = r + 1\n", + " if r == map_height-1:\n", + " # ic(\"Going down moves out of map\")\n", + " return 1\n", + " ch = lab_map[new_r][new_c]\n", + " if ch == \"+\":\n", + " ic(\"Loop detected\")\n", + " return 2\n", + " elif ch in [\"#\", \"O\"]:\n", + " # ic(\"Block\", ch, \"found in front of\", (r, c), \"==> move LEFT\")\n", + " lab_map[r][c] = \"+\"\n", + " new_r, new_c = r, c - 1\n", + " lab_map[new_r][new_c] = \"<\"\n", + " new_d = \"LEFT\"\n", + " else:\n", + " lab_map[r][c] = \"|\"\n", + " lab_map[new_r][new_c] = \"V\"\n", + " elif d == \"LEFT\":\n", + " new_c = c - 1\n", + " if c == 0:\n", + " # ic(\"Going left moves out of map\")\n", + " return 1\n", + " ch = lab_map[new_r][new_c]\n", + " if ch == \"+\":\n", + " ic(\"Loop detected\")\n", + " return 2\n", + " elif ch in [\"#\", \"O\"]:\n", + " # ic(\"Block\", ch, \"found in front of\", (r, c), \"==> move UP\")\n", + " lab_map[r][c] = \"+\"\n", + " new_r, new_c = r - 1, c\n", + " lab_map[new_r][new_c] = \"^\"\n", + " new_d = \"UP\"\n", + " else:\n", + " lab_map[r][c] = \"-\"\n", + " lab_map[new_r][new_c] = \"<\"\n", + " else:\n", + " assert False, ic(\"This should never happen\", r, c, lab_map)\n", + " \n", " ...\n", "\n", - "ic(\"PART1 RESULT\", count_element(\"X\") + 1)" + " sanity_checks(lab_map)\n", + " \n", + " return result" ] }, { "cell_type": "code", - "execution_count": null, - "id": "8a366015-d692-4bf7-aa35-1f250d022cb2", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "execution_count": 21, + "id": "3e8516ff-60a9-4194-aae7-13c7a06cf61b", + "metadata": {}, "outputs": [], "source": [ - "ic(lab_map)" + "def is_looping_obstruction(lab_map, r:int, c:int) -> bool:\n", + " # result = False\n", + "\n", + " # ic(\"DEBUG: Check loop with obstruction at\", (r,c))\n", + "\n", + " assert r >= 0 and r < len(lab_map), ic(r)\n", + " assert c >= 0 and c < len(lab_map[0]), ic(c)\n", + "\n", + " traversed_map = copy.deepcopy(input_map)\n", + "\n", + " ch = traversed_map[r][c]\n", + " if not ch == \".\":\n", + " # ic(\"lab_map at\", (r,c), \"has\", ch)\n", + " return False\n", + " \n", + " traversed_map[r][c] = \"O\"\n", + " # ic(traversed_map)\n", + "\n", + " res = 0\n", + " step = 0\n", + " while res == 0:\n", + " res = move_one_step_part2(traversed_map)\n", + " step += 1\n", + " if step % 1000 == 0:\n", + " ic(r, c, step)\n", + "\n", + " return res == 2" ] }, { "cell_type": "code", - "execution_count": null, - "id": "627da66e-1d88-46c0-ad51-873856aca8a9", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true - } - }, + "execution_count": 22, + "id": "2a5e5327-5384-411c-ac61-d5a79960903a", + "metadata": {}, "outputs": [], "source": [ - "count_element(\"X\")" + "# Now check obstructions against test_data\n", + "\n", + "# ic(is_looping_obstruction(input_map, 6, 1)) # No loop\n", + "#\n", + "# ic(is_looping_obstruction(input_map, 6, 3)) # Option 1\n", + "# ic(is_looping_obstruction(input_map, 7, 6)) # Option 2\n", + "# ic(is_looping_obstruction(input_map, 7, 7)) # Option 3\n", + "# ic(is_looping_obstruction(input_map, 8, 1)) # Option 4\n", + "# ic(is_looping_obstruction(input_map, 8, 3)) # Option 5\n", + "# ic(is_looping_obstruction(input_map, 9, 7)) # Option 6" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "a88a7216-7103-4b70-a0c5-43d760ee9e75", + "metadata": {}, + "outputs": [], + "source": [ + "def solve_part2(lab_map: list) -> int:\n", + " result = 0\n", + "\n", + " # TODO\n", + "\n", + " for r in range(len(lab_map)):\n", + " ic(\"solve_part2: Checking row\", r)\n", + " for c in range(len(lab_map[0])):\n", + " # ic(\"Calling is_looping_obstruction(...,\", r, c, \")\")\n", + " res = is_looping_obstruction(lab_map, r, c)\n", + " if res:\n", + " result += 1\n", + " ic(result)\n", + "\n", + " ...\n", + " \n", + " return result" ] }, { "cell_type": "code", "execution_count": null, - "id": "40e268ba-dd02-4197-ad83-fb66316f35a3", - "metadata": { - "collapsed": true, - "jupyter": { - "outputs_hidden": true + "id": "ea030ceb-065e-43c2-bfad-c91bb7967522", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ic| 'solve_part2: Checking row', r: 0\n", + "ic| r: 0, c: 0, step: 1000\n", + "ic| r: 0, c: 0, step: 2000\n", + "ic| r: 0, c: 0, step: 3000\n", + "ic| r: 0, c: 0, step: 4000\n", + "ic| r: 0, c: 0, step: 5000\n", + "ic| r: 0, c: 1, step: 1000\n", + "ic| r: 0, c: 1, step: 2000\n", + "ic| r: 0, c: 1, step: 3000\n", + "ic| r: 0, c: 1, step: 4000\n", + "ic| r: 0, c: 1, step: 5000\n", + "ic| r: 0, c: 2, step: 1000\n", + "ic| r: 0, c: 2, step: 2000\n", + "ic| r: 0, c: 2, step: 3000\n", + "ic| r: 0, c: 2, step: 4000\n", + "ic| r: 0, c: 2, step: 5000\n", + "ic| r: 0, c: 3, step: 1000\n", + "ic| r: 0, c: 3, step: 2000\n", + "ic| r: 0, c: 3, step: 3000\n", + "ic| r: 0, c: 3, step: 4000\n", + "ic| r: 0, c: 3, step: 5000\n", + "ic| r: 0, c: 4, step: 1000\n", + "ic| r: 0, c: 4, step: 2000\n", + "ic| r: 0, c: 4, step: 3000\n", + "ic| r: 0, c: 4, step: 4000\n", + "ic| r: 0, c: 4, step: 5000\n", + "ic| r: 0, c: 5, step: 1000\n", + "ic| r: 0, c: 5, step: 2000\n", + "ic| r: 0, c: 5, step: 3000\n", + "ic| r: 0, c: 5, step: 4000\n", + "ic| r: 0, c: 5, step: 5000\n", + "ic| r: 0, c: 6, step: 1000\n", + "ic| r: 0, c: 6, step: 2000\n", + "ic| r: 0, c: 6, step: 3000\n", + "ic| r: 0, c: 6, step: 4000\n", + "ic| r: 0, c: 6, step: 5000\n", + "ic| r: 0, c: 7, step: 1000\n", + "ic| r: 0, c: 7, step: 2000\n", + "ic| r: 0, c: 7, step: 3000\n", + "ic| r: 0, c: 7, step: 4000\n", + "ic| r: 0, c: 7, step: 5000\n" + ] } - }, + ], + "source": [ + "tm_begin = time.time()\n", + "part2_result = solve_part2(input_map)\n", + "tm_end = time.time()\n", + "ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n", + "print(\"Day 06 Part 2 RESULT:\")\n", + "print(part2_result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c757b576-5739-447a-9ea9-60419a57ef1a", + "metadata": {}, "outputs": [], "source": [ - "# TODO" + "part2_result" ] }, { "cell_type": "code", "execution_count": null, - "id": "caeced21-0e4c-4ac2-8e08-d6941238066e", + "id": "276634a5-7a73-443f-95de-faeb114570d9", "metadata": { "collapsed": true, "jupyter": { @@ -450,14 +721,6 @@ }, "outputs": [], "source": [] - }, - { - "cell_type": "markdown", - "id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", - "metadata": {}, - "source": [ - "## Part 2" - ] } ], "metadata": {