Joe ik vind het prachtig nu
This commit is contained in:
BasGremmen 2023-12-03 01:40:18 +01:00
parent d2454b4675
commit 5fbed735c2
2 changed files with 136 additions and 133 deletions

View File

@ -44,24 +44,29 @@
},
{
"cell_type": "code",
"execution_count": 2,
"id": "12d018ab-1fa6-4403-b58f-188e27644d31",
"execution_count": 213,
"id": "98412978-4434-4461-8255-fde982948f26",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55834\n"
]
"data": {
"text/plain": [
"55834"
]
},
"execution_count": 213,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = open('data/puzzle_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"
"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"
]
},
{
@ -74,8 +79,8 @@
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c7e92c53-f373-4894-b34f-ac06c15e87e7",
"execution_count": 215,
"id": "66632939-dc5e-44ba-b740-868c20cabe8c",
"metadata": {},
"outputs": [
{
@ -84,27 +89,21 @@
"53221"
]
},
"execution_count": 3,
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = open('data/puzzle_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)"
"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"
]
},
{
@ -117,46 +116,43 @@
},
{
"cell_type": "markdown",
"id": "dedf36d2-0a24-45e2-90f7-889f1ca5da87",
"id": "a1afaf93-e716-4347-83cb-8a5d3efd8601",
"metadata": {},
"source": [
"### Deel 1 & 2 tegelijk, oeps"
"### Deel 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "46f6cc4a-2be4-4b86-be2d-6e08404dc9ef",
"execution_count": 289,
"id": "eb4c93ee-5b08-47c7-b020-16b2c545b7c1",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3035\n"
]
}
],
"source": [
"f = open('data/puzzle_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",
" "
"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",
" possible = True\n",
" for pair in re.findall('(\\d+)\\W*(red|green|blue)', line):\n",
" if (color_dict[pair[1]] < int(pair[0])):\n",
" possible = False\n",
" if possible:\n",
" total += id\n",
"total"
]
},
{
@ -164,12 +160,12 @@
"id": "810e3c1a-af84-45e1-9508-a00c40523f7b",
"metadata": {},
"source": [
"### Naja hier pas de output"
"### Deel 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 291,
"id": "21a60b17-619f-426f-8617-5ff4352cca0c",
"metadata": {},
"outputs": [
@ -177,21 +173,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"3035\n",
"66027\n"
]
}
],
"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)"
"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 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",
"total"
]
}
],

View File

@ -14,35 +14,34 @@ import re
```python
f = open('data/puzzle_1.txt', 'r')
digits = [re.findall('\d', line) for line in f]
numbers = [str(digit[0]) + str(digit[-1]) for digit in digits]
numbers = [int(x) for x in numbers]
print(sum(numbers))
total = 0
for line in f:
digit_1 = re.search('(\d)', line).group()
digit_2 = re.search('(?:\d)(?!.*\d)', line).group()
total += int(digit_1+digit_2)
total
```
55834
### Deel 2
```python
f = open('data/puzzle_1.txt', 'r')
l = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
digits = [re.findall('(?=(\d|'+'|'.join(l)+'))', line) for line in f]
numbers = [[digit[0],digit[-1]] for digit in digits]
listing = []
for number_pair in numbers:
number_string = ""
for number in number_pair:
if number in l:
number_string += str(l.index(number) + 1)
else:
number_string += str(number)
listing.append(int(number_string))
sum(listing)
digits = '|'+'|'.join(l)
f = open('data/puzzle_1.txt', 'r')
total = 0
for line in f:
string_digits = re.findall('(?=(\d'+digits+'))', line)
string_digits = [digit if (digit not in l) else str(l.index(digit)+1) for digit in string_digits]
total += int(string_digits[0] + string_digits[-1])
total
```
@ -54,54 +53,54 @@ sum(listing)
## Puzzel 2
### Deel 1 & 2 tegelijk, oeps
### Deel 1
```python
f = open('data/puzzle_2.txt', 'r')
rows = []
red = 12
green = 13
blue = 14
for x in f:
row = {}
row['id'], row['content'] = x.split(':')
row['id'] = int(re.findall('\d+', row['id'])[0])
row['counts'] = row['content'].split(';')
listing = []
maxes = {'red': 0, 'green': 0, 'blue': 0}
for count in row['counts']:
numbers = re.findall('\d+', count)
colors = re.findall('red|blue|green', count)
for test_color, test_number in zip(colors, numbers):
if maxes[test_color] < int(test_number):
maxes[test_color] = int(test_number)
listing.append(dict(zip(colors, numbers)))
row['maxes'] = maxes
row['counts'] = listing
rows.append(row)
```
### Naja hier pas de output
```python
count = 0
count_2 = 0
for row in rows:
maxes = row['maxes']
count_2 += maxes['red'] * maxes['blue'] * maxes['green']
if red >= maxes['red'] and blue >= maxes['blue'] and green >= maxes['green']:
count += row['id']
print(count)
print(count_2)
color_dict = {
'red': 12,
'green': 13,
'blue': 14
}
total = 0
for line in f:
id = int(re.findall('(\d+)', line)[0])
possible = True
for pair in re.findall('(\d+)\W*(red|green|blue)', line):
if (color_dict[pair[1]] < int(pair[0])):
possible = False
if possible:
total += id
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 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']
total
```
66027