This commit is contained in:
BasGremmen 2023-12-04 09:54:47 +01:00
parent 933af159cd
commit 08bd33e765
2 changed files with 269 additions and 2 deletions

View File

@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159",
"metadata": {},
"outputs": [],
@ -318,6 +318,161 @@
" total+= asterisk['numbers'][0] * asterisk['numbers'][1]\n",
"total"
]
},
{
"cell_type": "markdown",
"id": "db726345-15cd-4692-9d98-a8d3a28adfeb",
"metadata": {},
"source": [
"## Puzzel 4"
]
},
{
"cell_type": "markdown",
"id": "42c8bc93-34b9-4722-850a-bc4c15212fd8",
"metadata": {},
"source": [
"### Deel 1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2cbabb08-e696-4466-8963-844ec7d8fc15",
"metadata": {},
"outputs": [],
"source": [
"data = open('data/puzzle_4.txt', 'r').readlines()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "813183a3-31df-4cb2-a4f4-cbb0db517cb2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"21568\n"
]
}
],
"source": [
"# initiating output variable\n",
"total = 0\n",
"for line in data:\n",
" # Cleaning identifier as it is not necessary\n",
" line_number_part = line.split(':')[1]\n",
"\n",
" # Removing the \\n \n",
" line_number_part = re.sub('\\n', '',line_number_part)\n",
"\n",
" # Creating a set with winning numbers and with owned numbers\n",
" winning_numbers, owned_numbers = line_number_part.split('|')\n",
"\n",
" # Getting the seperate numbers\n",
" winning_numbers = re.findall('(\\d+)', winning_numbers)\n",
" owned_numbers = re.findall('(\\d+)', owned_numbers)\n",
"\n",
" # Converting to a set to prepare for intersect\n",
" winning_set = set(winning_numbers)\n",
" owned_set = set(owned_numbers)\n",
"\n",
" # Only numbers that are both owned and winning will be left\n",
" winning_numbers_owned = winning_set.intersection(owned_set)\n",
"\n",
" # Get the number of matches\n",
" matched_number_count = len(winning_numbers_owned)\n",
"\n",
" # If there are no matches, we add nothing to total, so we check for zero matches\n",
" if matched_number_count > 0:\n",
" # We then add to the total 2 to the power of the total number of matches minus one, as we start at 1 instead of 2 and 2^0 is 1 \n",
" total += 2**(matched_number_count-1)\n",
"print(total)"
]
},
{
"cell_type": "markdown",
"id": "9d818873-9540-4de8-881a-620c02ddefca",
"metadata": {},
"source": [
"### Deel 2"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "64ac3677-aa84-4059-981a-857632b827c8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11827296\n"
]
}
],
"source": [
"# initiating output variable\n",
"total = 0\n",
"winning_numbers_on_card = []\n",
"card_numbers = []\n",
"copies_of_card = []\n",
"for line in data:\n",
" # Cleaning identifier\n",
" card_identifier_part, line_number_part = line.split(':')\n",
" identifier = int(re.search('\\d+', card_identifier_part).group())\n",
"\n",
" # adding to the list\n",
" card_numbers.append(identifier)\n",
"\n",
" # Also adding a 1 to the owned cards list\n",
" copies_of_card.append(1)\n",
"\n",
" # Removing the \\n \n",
" line_number_part = re.sub('\\n', '',line_number_part)\n",
"\n",
" # Creating a set with winning numbers and with owned numbers\n",
" winning_numbers, owned_numbers = line_number_part.split('|')\n",
"\n",
" # Getting the seperate numbers\n",
" winning_numbers = re.findall('(\\d+)', winning_numbers)\n",
" owned_numbers = re.findall('(\\d+)', owned_numbers)\n",
"\n",
" # Converting to a set to prepare for intersect\n",
" winning_set = set(winning_numbers)\n",
" owned_set = set(owned_numbers)\n",
"\n",
" # Only numbers that are both owned and winning will be left\n",
" winning_numbers_owned = winning_set.intersection(owned_set)\n",
"\n",
" # Get the number of matches\n",
" matched_number_count = len(winning_numbers_owned)\n",
"\n",
" # Add to card info list\n",
" winning_numbers_on_card.append(matched_number_count)\n",
"\n",
"\n",
"# Just simulating the rounds, if a card has winining numbers, loop over a range and add that loop variable to the card number\n",
"# if the card number exists, we simply add the number of copies of the current card to that card\n",
"# etc. \n",
"for i, card_id in enumerate(card_numbers):\n",
" winning_numbers = winning_numbers_on_card[i]\n",
" for number in range(1, winning_numbers+1):\n",
" if (card_id+number) in card_numbers:\n",
" id_to_add_to = card_numbers.index(card_id+number)\n",
" copies_of_card[id_to_add_to] += copies_of_card[i]\n",
"\n",
"# just sum the total cards at the end\n",
"print(sum(copies_of_card))\n",
" \n",
" \n",
" \n",
" "
]
}
],
"metadata": {
@ -336,7 +491,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.10.13"
}
},
"nbformat": 4,

View File

@ -193,3 +193,115 @@ total
81296995
## Puzzel 4
### Deel 1
```python
data = open('data/puzzle_4.txt', 'r').readlines()
```
```python
# initiating output variable
total = 0
for line in data:
# Cleaning identifier as it is not necessary
line_number_part = line.split(':')[1]
# Removing the \n
line_number_part = re.sub('\n', '',line_number_part)
# Creating a set with winning numbers and with owned numbers
winning_numbers, owned_numbers = line_number_part.split('|')
# Getting the seperate numbers
winning_numbers = re.findall('(\d+)', winning_numbers)
owned_numbers = re.findall('(\d+)', owned_numbers)
# Converting to a set to prepare for intersect
winning_set = set(winning_numbers)
owned_set = set(owned_numbers)
# Only numbers that are both owned and winning will be left
winning_numbers_owned = winning_set.intersection(owned_set)
# Get the number of matches
matched_number_count = len(winning_numbers_owned)
# If there are no matches, we add nothing to total, so we check for zero matches
if matched_number_count > 0:
# We then add to the total 2 to the power of the total number of matches minus one, as we start at 1 instead of 2 and 2^0 is 1
total += 2**(matched_number_count-1)
print(total)
```
21568
### Deel 2
```python
# initiating output variable
total = 0
winning_numbers_on_card = []
card_numbers = []
copies_of_card = []
for line in data:
# Cleaning identifier
card_identifier_part, line_number_part = line.split(':')
identifier = int(re.search('\d+', card_identifier_part).group())
# adding to the list
card_numbers.append(identifier)
# Also adding a 1 to the owned cards list
copies_of_card.append(1)
# Removing the \n
line_number_part = re.sub('\n', '',line_number_part)
# Creating a set with winning numbers and with owned numbers
winning_numbers, owned_numbers = line_number_part.split('|')
# Getting the seperate numbers
winning_numbers = re.findall('(\d+)', winning_numbers)
owned_numbers = re.findall('(\d+)', owned_numbers)
# Converting to a set to prepare for intersect
winning_set = set(winning_numbers)
owned_set = set(owned_numbers)
# Only numbers that are both owned and winning will be left
winning_numbers_owned = winning_set.intersection(owned_set)
# Get the number of matches
matched_number_count = len(winning_numbers_owned)
# Add to card info list
winning_numbers_on_card.append(matched_number_count)
# Just simulating the rounds, if a card has winining numbers, loop over a range and add that loop variable to the card number
# if the card number exists, we simply add the number of copies of the current card to that card
# etc.
for i, card_id in enumerate(card_numbers):
winning_numbers = winning_numbers_on_card[i]
for number in range(1, winning_numbers+1):
if (card_id+number) in card_numbers:
id_to_add_to = card_numbers.index(card_id+number)
copies_of_card[id_to_add_to] += copies_of_card[i]
# just sum the total cards at the end
print(sum(copies_of_card))
```
11827296