345 lines
8.7 KiB
Plaintext
345 lines
8.7 KiB
Plaintext
{
|
|
"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": 5,
|
|
"id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import re \n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"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": 2,
|
|
"id": "98412978-4434-4461-8255-fde982948f26",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"55834"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"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": 3,
|
|
"id": "66632939-dc5e-44ba-b740-868c20cabe8c",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"53221"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "95831680-6f23-404b-97e6-0411c59f6bdf",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Puzzel 3"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 104,
|
|
"id": "3f70dc72-b4e4-4f65-87ac-d1222a01c8aa",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = open('data/puzzle_3.txt', 'r').readlines()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 113,
|
|
"id": "91a632d1-6262-4c28-bb17-45352c60c16e",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"517021"
|
|
]
|
|
},
|
|
"execution_count": 113,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"data_list = [re.sub('\\n', '', line) for line in data]\n",
|
|
"data_list = [re.sub('[^\\d\\.]', 'X', line) for line in data_list]\n",
|
|
"total = 0\n",
|
|
"for line_number, line in enumerate(data_list):\n",
|
|
" matches = re.findall('\\d+', line)\n",
|
|
" for number in matches:\n",
|
|
" match = re.search(number, line)\n",
|
|
" surrounding_string = \"\"\n",
|
|
" start, end = match.span()\n",
|
|
" if start > 0:\n",
|
|
" start -= 1\n",
|
|
" if end <= len(line):\n",
|
|
" end += 1\n",
|
|
" if line_number != 0:\n",
|
|
" surrounding_string += data_list[line_number-1][start:end]\n",
|
|
" surrounding_string += line[start:end]\n",
|
|
" if line_number != len(data_list)-1:\n",
|
|
" surrounding_string += data_list[line_number+1][start:end]\n",
|
|
" if 'X' in surrounding_string:\n",
|
|
" total += int(match.group())\n",
|
|
" line = re.sub(match.group(), len(match.group())*'.', line, 1)\n",
|
|
" match = re.search('\\d+', line) \n",
|
|
"total"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 138,
|
|
"id": "4b239c25-3a43-459e-9669-aa9834571733",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"81296995"
|
|
]
|
|
},
|
|
"execution_count": 138,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"data_list = [re.sub('\\n', '', line) for line in data]\n",
|
|
"asterisks = []\n",
|
|
"numbers = []\n",
|
|
"for line_number, line in enumerate(data_list):\n",
|
|
" match = re.search('(\\*)|(\\d+)', line)\n",
|
|
" while match is not None:\n",
|
|
" x, y = match.span()\n",
|
|
" if match.group() == '*':\n",
|
|
" asterisks.append({'x_pos': x, 'line_number': line_number})\n",
|
|
" else:\n",
|
|
" numbers.append({'x_start':x, 'x_end': y, 'line_number': line_number, 'number': int(match.group())})\n",
|
|
" line = re.sub('(\\*)|(\\d+)', len(match.group())*'.', line, 1)\n",
|
|
" match = re.search('(\\*)|(\\d+)', line)\n",
|
|
" \n",
|
|
"for asterisk in asterisks:\n",
|
|
" asterisk_numbers = []\n",
|
|
" for number in numbers:\n",
|
|
" if number['line_number'] == asterisk['line_number']:\n",
|
|
" if number['x_end'] == asterisk['x_pos']:\n",
|
|
" asterisk_numbers.append(number['number'])\n",
|
|
" continue\n",
|
|
" if number['x_start']-1 == asterisk['x_pos']:\n",
|
|
" asterisk_numbers.append(number['number'])\n",
|
|
" continue\n",
|
|
" \n",
|
|
" if (number['line_number']+1 == asterisk['line_number']) or (number['line_number']-1 == asterisk['line_number']):\n",
|
|
" if asterisk['x_pos'] in [num for num in range(number['x_start']-1, number['x_end']+1)]:\n",
|
|
" asterisk_numbers.append(number['number'])\n",
|
|
"\n",
|
|
" asterisk['numbers'] = asterisk_numbers\n",
|
|
"total = 0\n",
|
|
"for asterisk in asterisks:\n",
|
|
" if len(asterisk['numbers']) == 2:\n",
|
|
" total+= asterisk['numbers'][0] * asterisk['numbers'][1]\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
|
|
}
|