211 lines
4.9 KiB
Plaintext
211 lines
4.9 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": null,
|
|
"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": 3,
|
|
"id": "12d018ab-1fa6-4403-b58f-188e27644d31",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"55834\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"f = open('Puzzle_1_1.txt', 'r')\n",
|
|
"digits = [re.findall('\\d', line) for line in f]\n",
|
|
"numbers = [str(digit[0]) + str(digit[-1]) for digit in digits]\n",
|
|
"numbers = [int(x) for x in numbers]\n",
|
|
"print(sum(numbers))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "00cbc4f3-d337-4702-bf25-d04717a1787b",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Deel 2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 35,
|
|
"id": "c7e92c53-f373-4894-b34f-ac06c15e87e7",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"53221"
|
|
]
|
|
},
|
|
"execution_count": 35,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"f = open('Puzzle_1_1.txt', 'r')\n",
|
|
"l = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']\n",
|
|
"digits = [re.findall('(?=(\\d|'+'|'.join(l)+'))', line) for line in f]\n",
|
|
"numbers = [[digit[0],digit[-1]] for digit in digits]\n",
|
|
"listing = []\n",
|
|
"for number_pair in numbers:\n",
|
|
" number_string = \"\"\n",
|
|
" for number in number_pair:\n",
|
|
" if number in l:\n",
|
|
" number_string += str(l.index(number) + 1)\n",
|
|
" else:\n",
|
|
" number_string += str(number)\n",
|
|
" listing.append(int(number_string))\n",
|
|
"\n",
|
|
"sum(listing)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "455fa9e3-535d-4708-b0fe-1179ad095f4c",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Puzzel 2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "dedf36d2-0a24-45e2-90f7-889f1ca5da87",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Deel 1 & 2 tegelijk, oeps"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "46f6cc4a-2be4-4b86-be2d-6e08404dc9ef",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"f = open('puzzle_2_2.txt', 'r')\n",
|
|
"rows = []\n",
|
|
"red = 12\n",
|
|
"green = 13\n",
|
|
"blue = 14\n",
|
|
"for x in f:\n",
|
|
" row = {}\n",
|
|
" row['id'], row['content'] = x.split(':')\n",
|
|
" row['id'] = int(re.findall('\\d+', row['id'])[0])\n",
|
|
" row['counts'] = row['content'].split(';')\n",
|
|
" listing = []\n",
|
|
" maxes = {'red': 0, 'green': 0, 'blue': 0}\n",
|
|
" for count in row['counts']:\n",
|
|
" numbers = re.findall('\\d+', count)\n",
|
|
" colors = re.findall('red|blue|green', count)\n",
|
|
" for test_color, test_number in zip(colors, numbers):\n",
|
|
" if maxes[test_color] < int(test_number):\n",
|
|
" maxes[test_color] = int(test_number)\n",
|
|
" listing.append(dict(zip(colors, numbers)))\n",
|
|
" row['maxes'] = maxes\n",
|
|
" row['counts'] = listing\n",
|
|
" rows.append(row)\n",
|
|
"\n",
|
|
" \n",
|
|
" \n",
|
|
" \n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "810e3c1a-af84-45e1-9508-a00c40523f7b",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Naja hier pas de output"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "21a60b17-619f-426f-8617-5ff4352cca0c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"count = 0\n",
|
|
"count_2 = 0\n",
|
|
"for row in rows:\n",
|
|
" maxes = row['maxes']\n",
|
|
" count_2 += maxes['red'] * maxes['blue'] * maxes['green']\n",
|
|
" if red >= maxes['red'] and blue >= maxes['blue'] and green >= maxes['green']:\n",
|
|
" count += row['id']\n",
|
|
"print(count)\n",
|
|
"print(count_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.10.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|