Ook ff 6 geupload

This commit is contained in:
BasGremmen 2023-12-08 10:18:19 +01:00
parent 60a0408db4
commit c08967925a
2 changed files with 325 additions and 0 deletions

View File

@ -806,6 +806,71 @@
"## Puzzel 7"
]
},
{
"cell_type": "markdown",
"id": "38709034-8443-4db3-8c4f-0952dd498fec",
"metadata": {},
"source": [
"## Deel 1"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "8369bc66-021e-44e3-a44d-a2403e4ee11f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"250120186"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = re.findall(r'(\\w+)\\s(\\d+)',open('data/puzzle_7.txt', 'r').read(), flags=re.MULTILINE)\n",
"values = \"AKQJT98765432\"\n",
"hands = [{'cards': x, 'bid': int(y)} for x,y in data]\n",
"\n",
"fivek = []\n",
"fourk = []\n",
"fh = []\n",
"threek = []\n",
"twop = []\n",
"onep = []\n",
"hc = []\n",
"for hand in hands:\n",
" hand['values'] = np.array([hand['cards'].count(letter) for letter in values])\n",
" max_card = np.argmax(hand['values'])\n",
"\n",
" if max(hand['values']) == 5:\n",
" fivek.append(hand)\n",
" elif max(hand['values']) == 4:\n",
" fourk.append(hand)\n",
" elif max(hand['values']) == 3:\n",
" if np.any(hand['values'] == 2):\n",
" fh.append(hand)\n",
" else:\n",
" threek.append(hand)\n",
" elif max(hand['values']) == 2:\n",
" if sum(hand['values'] > 1) == 2:\n",
" twop.append(hand)\n",
" else:\n",
" onep.append(hand)\n",
" else:\n",
" hc.append(hand)\n",
"combos = [fivek, fourk, fh, threek, twop, onep, hc]\n",
"for combo in combos:\n",
" combo.sort(key=lambda hand: ''.join([chr(values.index(letter)+96) for letter in hand['cards']]))\n",
"combos = [val for sublist in combos for val in sublist] \n",
"combos = combos[::-1]\n",
"sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])"
]
},
{
"cell_type": "markdown",
"id": "b36323f9-e331-4cb3-b683-0c34e0012466",
@ -871,6 +936,103 @@
"combos = combos[::-1]\n",
"sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])"
]
},
{
"cell_type": "markdown",
"id": "c3c2728a-0b18-4fef-ba87-f8b8144050bc",
"metadata": {},
"source": [
"## Puzzel 8"
]
},
{
"cell_type": "markdown",
"id": "d4513f0d-2fd2-4c84-9de0-ce92978742b6",
"metadata": {},
"source": [
"### Deel 1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "52c6a5ba-3a6a-45fe-a60c-873e10a81745",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20777\n"
]
}
],
"source": [
"data = open('data/puzzle_8.txt', 'r').read()\n",
"directions = re.search(r'(\\w+)', data).group()\n",
"nodes = {x: {'L': y, 'R': z} for x,y,z in re.findall(r'(\\w{3}).*\\((\\w{3}).*(\\w{3})', data, flags=re.MULTILINE)}\n",
"steps = 0\n",
"current_node = 'AAA'\n",
"\n",
"while current_node != 'ZZZ':\n",
" for direction in directions:\n",
" current_node = nodes[current_node][direction]\n",
" steps += 1\n",
" if current_node == 'ZZZ':\n",
" break\n",
"print(steps)"
]
},
{
"cell_type": "markdown",
"id": "5c0033da-34cb-4eff-b99c-0e45986577ce",
"metadata": {},
"source": [
"### Deel 2"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "e2e32f65-6df2-4cc7-b0ee-96df14f6b083",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13289612809129"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"current_nodes = re.findall('(\\w{2}A)\\s', data, flags=re.MULTILINE)\n",
"all_matches = []\n",
"for node in current_nodes:\n",
" matches = []\n",
" steps = 0\n",
" while len(matches) < 10:\n",
" for letter in directions:\n",
" node = nodes[node][letter]\n",
" steps += 1\n",
" if node[2] == 'Z':\n",
" matches.append(steps)\n",
" all_matches.append(matches)\n",
"lowest = [matches[0] for matches in all_matches]\n",
"\n",
"val = np.gcd.reduce(lowest)\n",
"total = 1\n",
"for x in [x/val for x in lowest]:\n",
" total *= x\n",
"int(total * val)\n",
" \n",
" \n",
" \n",
" "
]
}
],
"metadata": {

View File

@ -506,3 +506,166 @@ round(x1) - round(x0)-1
27340847
## Puzzel 7
## Deel 1
```python
data = re.findall(r'(\w+)\s(\d+)',open('data/puzzle_7.txt', 'r').read(), flags=re.MULTILINE)
values = "AKQJT98765432"
hands = [{'cards': x, 'bid': int(y)} for x,y in data]
fivek = []
fourk = []
fh = []
threek = []
twop = []
onep = []
hc = []
for hand in hands:
hand['values'] = np.array([hand['cards'].count(letter) for letter in values])
max_card = np.argmax(hand['values'])
if max(hand['values']) == 5:
fivek.append(hand)
elif max(hand['values']) == 4:
fourk.append(hand)
elif max(hand['values']) == 3:
if np.any(hand['values'] == 2):
fh.append(hand)
else:
threek.append(hand)
elif max(hand['values']) == 2:
if sum(hand['values'] > 1) == 2:
twop.append(hand)
else:
onep.append(hand)
else:
hc.append(hand)
combos = [fivek, fourk, fh, threek, twop, onep, hc]
for combo in combos:
combo.sort(key=lambda hand: ''.join([chr(values.index(letter)+96) for letter in hand['cards']]))
combos = [val for sublist in combos for val in sublist]
combos = combos[::-1]
sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])
```
250120186
### Deel 2
```python
data = re.findall(r'(\w+)\s(\d+)',open('data/puzzle_7.txt', 'r').read(), flags=re.MULTILINE)
values = "AKQT98765432J"
hands = [{'cards': x, 'bid': int(y)} for x,y in data]
fivek = []
fourk = []
fh = []
threek = []
twop = []
onep = []
hc = []
for hand in hands:
hand['values'] = np.array([hand['cards'].count(letter) for letter in values if letter != 'J'])
max_card = np.argmax(hand['values'])
hand['values'][max_card] += hand['cards'].count('J')
if max(hand['values']) == 5:
fivek.append(hand)
elif max(hand['values']) == 4:
fourk.append(hand)
elif max(hand['values']) == 3:
if np.any(hand['values'] == 2):
fh.append(hand)
else:
threek.append(hand)
elif max(hand['values']) == 2:
if sum(hand['values'] > 1) == 2:
twop.append(hand)
else:
onep.append(hand)
else:
hc.append(hand)
combos = [fivek, fourk, fh, threek, twop, onep, hc]
for combo in combos:
combo.sort(key=lambda hand: ''.join([chr(values.index(letter)+96) for letter in hand['cards']]))
combos = [val for sublist in combos for val in sublist]
combos = combos[::-1]
sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])
```
250665248
## Puzzel 8
### Deel 1
```python
data = open('data/puzzle_8.txt', 'r').read()
directions = re.search(r'(\w+)', data).group()
nodes = {x: {'L': y, 'R': z} for x,y,z in re.findall(r'(\w{3}).*\((\w{3}).*(\w{3})', data, flags=re.MULTILINE)}
steps = 0
current_node = 'AAA'
while current_node != 'ZZZ':
for direction in directions:
current_node = nodes[current_node][direction]
steps += 1
if current_node == 'ZZZ':
break
print(steps)
```
20777
### Deel 2
```python
current_nodes = re.findall('(\w{2}A)\s', data, flags=re.MULTILINE)
all_matches = []
for node in current_nodes:
matches = []
steps = 0
while len(matches) < 10:
for letter in directions:
node = nodes[node][letter]
steps += 1
if node[2] == 'Z':
matches.append(steps)
all_matches.append(matches)
lowest = [matches[0] for matches in all_matches]
val = np.gcd.reduce(lowest)
total = 1
for x in [x/val for x in lowest]:
total *= x
int(total * val)
```
13289612809129