Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Dec 28, 2023
1 parent c696d61 commit ea38467
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,18 @@
"source": [
"### Day 4 Part 1\n",
"\n",
"We have a set of scratch cards. Each card has an `id`, then a set of `winning` numbers, then a set of `actual` numbers that were scratched. The score of each scratch card depends on the number of winning numbers we have matched. The first match gives us 1 point, and each additional match doubles the score.\n",
"We have a pile of scratch cards. Our input data represets one card per row:\n",
"\n",
"```text\n",
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53\n",
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19\n",
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1\n",
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83\n",
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36\n",
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11\n",
"```\n",
"\n",
"Each card has an `id`, then a set of `winning` numbers, then a set of `actual` numbers that were scratched. The score of each scratch card depends on the number of winning numbers we have matched. The first match gives us 1 point, and each additional match doubles the score.\n",
"\n",
"**How points are the pile of scratch cards worth, in total?**\n",
"\n",
Expand All @@ -1670,7 +1681,8 @@
" - Stores the winning numbers, and the scratched numbers, both as sets. I want to use sets because I will want to determine the intersection of these two sets, i.e. how many winning numbers were matched. [Set algebra](https://aoc.just2good.co.uk/python/sets) makes this super easy.\n",
" - Has a `score()` method which determines the score, according to the rules.\n",
"- Then I parse the input data using [regex](https://aoc.just2good.co.uk/python/regex). Let me explain how \\\n",
" `Card(?:\\s+)(\\d+):\\s*((?:\\d+\\s*)+)(?: \\|\\s+)((?:\\d+\\s*)+)` works:\n",
" `Card\\s+(\\d+):\\s*((?:\\d+\\s*)+)(?: \\|\\s+)((?:\\d+\\s*)+)` works:\n",
" - `Card\\s+`: this matches \"Card\" followed by one or more spaces. The real input contains a variable number of spaces after `Card`. This part of the expression is not surrounded by brackets, so it is NOT returned as a captured group.\n",
" - `(\\d+)`: This is a capturing group that matches one or more digits. The `\\d` is a shorthand character class that matches any digit (0-9), and the `+` means one or more of the preceding element. This is how I capture the `id`.\n",
" - `\\s*`: This matches zero or more whitespace characters. The `*` means zero or more of the preceding element.\n",
" - `((?:\\d+\\s*)+)`: This is a capturing group containing a non-capturing group. The non-capturing group `(?:\\d+\\s*)` matches one or more digits followed by zero or more whitespace characters. The outer capturing group with the `+` at the end repeats this pattern one or more times. This is how I capture ALL of the `winning` numbers.\n",
Expand Down Expand Up @@ -1718,7 +1730,7 @@
"outputs": [],
"source": [
"def parse_cards(data) -> list[ScratchCard]:\n",
" scratch_card_pattern = r\"Card(?:\\s+)(\\d+):\\s*((?:\\d+\\s*)+)(?: \\|\\s+)((?:\\d+\\s*)+)\"\n",
" scratch_card_pattern = r\"Card\\s+(\\d+):\\s*((?:\\d+\\s*)+)(?: \\|\\s+)((?:\\d+\\s*)+)\"\n",
" scratch_card_matcher = re.compile(scratch_card_pattern)\n",
" scratch_cards = []\n",
" for line in data:\n",
Expand Down Expand Up @@ -1762,6 +1774,8 @@
" sample_cards = parse_cards(curr_input) \n",
" validate(solve_part1(sample_cards), curr_ans) # test with sample data\n",
"\n",
"logger.info(\"Tests passed!\")\n",
"\n",
"cards = parse_cards(input_data)\n",
"soln = solve_part1(cards)\n",
"logger.info(f\"Part 1 soln={soln}\")"
Expand Down

0 comments on commit ea38467

Please sign in to comment.