Advent_of_Code_2023_Intermate/Book_1.ipynb

552 lines
15 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": 240,
"id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159",
"metadata": {},
"outputs": [],
"source": [
"import re \n",
"import numpy as np\n",
"from functools import reduce\n",
"from math import factorial"
]
},
{
"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"
]
},
{
"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",
" "
]
},
{
"cell_type": "markdown",
"id": "bdaf4568-5996-493b-9d9b-398b5e16f522",
"metadata": {},
"source": [
"# AANSCHOUW!!!!!\n",
"Het wonder, het fantastischste ding ooit, oneliners"
]
},
{
"cell_type": "code",
"execution_count": 302,
"id": "091f006e-838e-418b-8282-610cc6fadccc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21568"
]
},
"execution_count": 302,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reduce(lambda total, multipliers: (total + 2**(multipliers-1) if (multipliers > 0) else total) ,map(lambda numbers: len(numbers)-len(set(numbers)), [re.findall('(?:(\\d+)\\s|\\n)', line) for line in open('data/puzzle_4.txt')]), 0)"
]
},
{
"cell_type": "code",
"execution_count": 400,
"id": "cdbdddfb-c0e4-4f9b-8b6c-4dd561c6236a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11827296"
]
},
"execution_count": 400,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([y for (x,y) in reduce(lambda total, cards: total + [(cards[0], 1)] + [(card, 1+sum([total_card[1] for total_card in total if total_card[0] == cards[0]])) for card in cards[1]], enumerate([j+1+i for j in range(len(re.findall(r'(?:\\b(\\d+)\\s)(?=.*\\s\\1\\b)', line)))] for i, line in enumerate(open('data/puzzle_4.txt'))), [])])"
]
}
],
"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
}