From 0bacf9bf892499738ef08565bfbbe4e29e23e558 Mon Sep 17 00:00:00 2001 From: BasGremmen Date: Sun, 3 Dec 2023 02:02:35 +0100 Subject: [PATCH] Toch nog een update --- Book_1.ipynb | 74 ++++++++++++++++++++++++++------------------------ pretty_read.md | 47 ++++++++++++++++---------------- 2 files changed, 63 insertions(+), 58 deletions(-) diff --git a/Book_1.ipynb b/Book_1.ipynb index 54c0c03..b2146cc 100644 --- a/Book_1.ipynb +++ b/Book_1.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159", "metadata": {}, "outputs": [], @@ -114,6 +114,16 @@ "## Puzzel 2" ] }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9c3695c6-20de-4e20-b13a-45c10eb26d0c", + "metadata": {}, + "outputs": [], + "source": [ + "data = open('data/puzzle_2.txt', 'r').readlines()" + ] + }, { "cell_type": "markdown", "id": "a1afaf93-e716-4347-83cb-8a5d3efd8601", @@ -124,34 +134,35 @@ }, { "cell_type": "code", - "execution_count": 289, + "execution_count": 14, "id": "eb4c93ee-5b08-47c7-b020-16b2c545b7c1", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "3035\n" - ] + "data": { + "text/plain": [ + "3035" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "f = open('data/puzzle_2.txt', 'r')\n", "color_dict = {\n", " 'red': 12,\n", " 'green': 13,\n", " 'blue': 14\n", "}\n", "total = 0\n", - "for line in f:\n", - " id = int(re.findall('(\\d+)', line)[0])\n", + "for line in data:\n", " possible = True\n", - " for pair in re.findall('(\\d+)\\W*(red|green|blue)', line):\n", - " if (color_dict[pair[1]] < int(pair[0])):\n", + " for amount, color in re.findall('(\\d+)\\W*(red|green|blue)', line):\n", + " if (color_dict[color] < int(amount)):\n", " possible = False\n", " if possible:\n", - " total += id\n", + " total += int(re.findall('(\\d+)', line)[0])\n", "total" ] }, @@ -165,36 +176,29 @@ }, { "cell_type": "code", - "execution_count": 291, + "execution_count": 15, "id": "21a60b17-619f-426f-8617-5ff4352cca0c", "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "66027\n" - ] + "data": { + "text/plain": [ + "66027" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "f = open('data/puzzle_2.txt', 'r')\n", - "color_dict = {\n", - " 'red': 0,\n", - " 'green': 0,\n", - " 'blue': 0\n", - "}\n", "total = 0\n", - "for line in f:\n", - " color_dict = {\n", - " 'red': 0,\n", - " 'green': 0,\n", - " 'blue': 0\n", - " }\n", + "for line in data:\n", + " color_dict = {'red': [], 'green': [], 'blue': []}\n", + " \n", " for pair in re.findall('(\\d+)\\W*(red|green|blue)', line):\n", - " if color_dict[pair[1]] < int(pair[0]):\n", - " color_dict[pair[1]] = int(pair[0])\n", - " total += color_dict['red'] * color_dict['green'] * color_dict['blue']\n", + " color_dict[pair[1]].append(int(pair[0]))\n", + " total += max(color_dict['red']) * max(color_dict['green']) * max(color_dict['blue'])\n", "total" ] } @@ -215,7 +219,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/pretty_read.md b/pretty_read.md index 7b33fff..728bda8 100644 --- a/pretty_read.md +++ b/pretty_read.md @@ -53,54 +53,55 @@ total ## Puzzel 2 + +```python +data = open('data/puzzle_2.txt', 'r').readlines() +``` + ### Deel 1 ```python -f = open('data/puzzle_2.txt', 'r') color_dict = { 'red': 12, 'green': 13, 'blue': 14 } total = 0 -for line in f: - id = int(re.findall('(\d+)', line)[0]) +for line in data: possible = True - for pair in re.findall('(\d+)\W*(red|green|blue)', line): - if (color_dict[pair[1]] < int(pair[0])): + for amount, color in re.findall('(\d+)\W*(red|green|blue)', line): + if (color_dict[color] < int(amount)): possible = False if possible: - total += id + total += int(re.findall('(\d+)', line)[0]) total ``` + + + 3035 - + + ### Deel 2 ```python -f = open('data/puzzle_2.txt', 'r') -color_dict = { - 'red': 0, - 'green': 0, - 'blue': 0 -} total = 0 -for line in f: - color_dict = { - 'red': 0, - 'green': 0, - 'blue': 0 - } +for line in data: + color_dict = {'red': [], 'green': [], 'blue': []} + for pair in re.findall('(\d+)\W*(red|green|blue)', line): - if color_dict[pair[1]] < int(pair[0]): - color_dict[pair[1]] = int(pair[0]) - total += color_dict['red'] * color_dict['green'] * color_dict['blue'] + color_dict[pair[1]].append(int(pair[0])) + total += max(color_dict['red']) * max(color_dict['green']) * max(color_dict['blue']) total ``` + + + 66027 - + +