From ee953459857ac8851d24291b090ba98dde6bedaa Mon Sep 17 00:00:00 2001 From: Dazbo Date: Thu, 28 Dec 2023 10:39:59 +0000 Subject: [PATCH] Refactoring --- src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb b/src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb index 21f3ce7..f68b588 100644 --- a/src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb +++ b/src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb @@ -1693,6 +1693,8 @@ " - I split the `winning` and `actual` numbers at the space, using `split()`. This gives me a list of string values. Then I convert the string values to `int` using `map()`, and finally convert each `list` to a `set`.\n", " - Now I can create an instance of `ScoreCard` from the `id` and the two sets.\n", "\n", + "- I've added a `matches()` method to my `ScatchCard`. This method uses the _intersects_ operator (`&`) to determine which numbers are present in both the `winning` set and the `actual` set. See more on this [here](https://aoc.just2good.co.uk/python/sets).\n", + "\n", "- Finally, just add up all the scores from each card using `sum` and a [list comprehension](https://aoc.just2good.co.uk/python/comprehensions)." ] }, @@ -1789,7 +1791,7 @@ "\n", "Now we're told that there's no such thing as a _score_. Instead scratchcards only cause you to win more scratchcards equal to the number of winning numbers you have. You win copies of the scratchcards below the winning card equal to the number of matches.\n", "\n", - "**how many total scratchcards do you end up with?**\n", + "**How many total scratchcards do you end up with?**\n", "\n", "**My solution**\n", "\n", @@ -1799,7 +1801,7 @@ "\n", "- Create a `dict` that stores the number of cards for each card ID.\n", "- Loop through all the cards we have to initialise the dict. There will be one of each.\n", - "- Then, loop through the cards again, in order. For each card:\n", + "- Then, loop through the cards again, in order. For each card:\n", " - Get the number of matches.\n", " - Use this number of matches to determine the successive cards that need to be duplicated.\n", " - Increment the count of each of those successive cards, by the count of the card we're currently on. (Because it might not be 1.)\n", @@ -1816,9 +1818,7 @@ "outputs": [], "source": [ "def solve_part2(cards: list[ScratchCard]):\n", - " card_counts = {}\n", - " for card in cards: # initialise our card counts \n", - " card_counts[card.id] = 1\n", + " card_counts = { card.id: 1 for card in cards } # initialise count to 1 for each original card\n", " \n", " for card in cards:\n", " count_this_card = card_counts[card.id]\n",