Skip to content

Commit

Permalink
🎨 improved the code (with suggestions from Github Copilot; added docs)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzkedzierska committed Dec 4, 2023
1 parent 27d66a6 commit 38b89e8
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions 2023/Day04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -54,6 +54,20 @@
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11\"\"\"\n",
"\n",
"def count_overlaps(input, example=False):\n",
" \"\"\"\n",
" Counts the number of overlaps between winning numbers and card numbers.\n",
"\n",
" Parameters:\n",
" - input (str): The input file path or the input data string.\n",
" - example (bool): Flag indicating whether the input is an example or a file path.\n",
"\n",
" Returns:\n",
" - list: A list of integers representing the number of overlaps for each line in the input.\n",
"\n",
" Example:\n",
" >>> count_overlaps(\"Card 1: 1 2 3 4 5 | 3 4 5 7 8 9 10\", example=True)\n",
" [3]\n",
" \"\"\"\n",
" if example: \n",
" input_data = input.split(\"\\n\")\n",
" else:\n",
Expand All @@ -63,15 +77,16 @@
" overlaps = []\n",
" for line in input_data:\n",
" _, numbers = line.split(\":\")\n",
" winning_numbers = [int(n) for n in numbers.split(\"|\")[0].split()]\n",
" card_numbers = [int(n) for n in numbers.split(\"|\")[1].split()]\n",
" numbers_split = numbers.split(\"|\")\n",
" winning_numbers = set(int(n) for n in numbers_split[0].split())\n",
" card_numbers = [int(n) for n in numbers_split[1].split()]\n",
" overlap = [n for n in winning_numbers if n in card_numbers]\n",
" overlaps.append(len(overlap))\n",
"\n",
" return overlaps\n",
"\n",
"def calculate_score(overlaps):\n",
" return sum([2**(i-1) for i in overlaps if i > 0])\n",
" return sum(2**(i-1) for i in overlaps if i > 0)\n",
" \n",
"\n",
"def part_one(input, example=False):\n",
Expand All @@ -82,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -94,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -106,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -115,7 +130,7 @@
"22488"
]
},
"execution_count": 29,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -164,15 +179,29 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def count_cards(overlaps):\n",
" cards = [1 for i in overlaps]\n",
" \"\"\"\n",
" Counts the number of cards based on the given overlaps.\n",
"\n",
" Parameters:\n",
" - overlaps (list): A list of integers representing the number of overlaps for each card.\n",
"\n",
" Returns:\n",
" - int: The total number of cards.\n",
"\n",
" Example:\n",
" >>> overlaps = [1, 2, 0, 0]\n",
" >>> count_cards(overlaps)\n",
" 9\n",
" \"\"\"\n",
" cards = [1] * len(overlaps)\n",
" for i, o in enumerate(overlaps):\n",
" if o > 0:\n",
" for j in range(i+1, i+o+1):\n",
" for j in range(i+1, min(i+o+1, len(cards))):\n",
" cards[j] += cards[i]\n",
" return sum(cards)\n",
"\n",
Expand All @@ -184,7 +213,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -193,7 +222,7 @@
"7013204"
]
},
"execution_count": 35,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand Down

0 comments on commit 38b89e8

Please sign in to comment.