{ "cells": [ { "cell_type": "markdown", "id": "d0b07b88-208c-4408-b9c4-ac9d42b5bad0", "metadata": {}, "source": [ "# Vet mooi notebook van Bas" ] }, { "cell_type": "markdown", "id": "5016996d-7e00-44a1-9553-8dd95b864662", "metadata": {}, "source": [ "Laten we beginnen met de imports" ] }, { "cell_type": "code", "execution_count": 3, "id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159", "metadata": {}, "outputs": [], "source": [ "import re " ] }, { "cell_type": "markdown", "id": "8f3db4f3-92c2-4e2c-91a2-fe59b1109673", "metadata": {}, "source": [ "## Puzzel 1" ] }, { "cell_type": "markdown", "id": "e76a322a-85e3-40b4-aa04-6f4dda2b7f40", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 213, "id": "98412978-4434-4461-8255-fde982948f26", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55834" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('data/puzzle_1.txt', 'r')\n", "total = 0\n", "for line in f:\n", " digit_1 = re.search('(\\d)', line).group()\n", " digit_2 = re.search('(?:\\d)(?!.*\\d)', line).group()\n", " total += int(digit_1+digit_2)\n", "total" ] }, { "cell_type": "markdown", "id": "00cbc4f3-d337-4702-bf25-d04717a1787b", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 215, "id": "66632939-dc5e-44ba-b740-868c20cabe8c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "53221" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']\n", "digits = '|'+'|'.join(l)\n", "f = open('data/puzzle_1.txt', 'r')\n", "total = 0\n", "for line in f:\n", " string_digits = re.findall('(?=(\\d'+digits+'))', line)\n", " string_digits = [digit if (digit not in l) else str(l.index(digit)+1) for digit in string_digits]\n", " total += int(string_digits[0] + string_digits[-1])\n", "total" ] }, { "cell_type": "markdown", "id": "455fa9e3-535d-4708-b0fe-1179ad095f4c", "metadata": {}, "source": [ "## 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", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 14, "id": "eb4c93ee-5b08-47c7-b020-16b2c545b7c1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3035" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "color_dict = {\n", " 'red': 12,\n", " 'green': 13,\n", " 'blue': 14\n", "}\n", "total = 0\n", "for line in data:\n", " possible = True\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 += int(re.findall('(\\d+)', line)[0])\n", "total" ] }, { "cell_type": "markdown", "id": "810e3c1a-af84-45e1-9508-a00c40523f7b", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 15, "id": "21a60b17-619f-426f-8617-5ff4352cca0c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66027" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total = 0\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", " color_dict[pair[1]].append(int(pair[0]))\n", " total += max(color_dict['red']) * max(color_dict['green']) * max(color_dict['blue'])\n", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }