Skip to content

Commit

Permalink
day 4 parts 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Sullivan committed Dec 5, 2023
1 parent 885ce98 commit 918cc20
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 0 deletions.
197 changes: 197 additions & 0 deletions data/day4.txt

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions docs/docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $ aoc [OPTIONS] COMMAND [ARGS]...

* `day1`: Command for running day1 stars
* `day2`: Command for running day2 stars
* `day4`: Command for running day4 stars
* `debug-version-info`: Print debug information to terminal.

## `aoc day1`
Expand Down Expand Up @@ -93,6 +94,7 @@ $ aoc day2 [OPTIONS] COMMAND [ARGS]...
**Commands**:

* `part1`: Calculate the sum of all Game IDs
* `part2`: Calculate the sum of all Game Powers

### `aoc day2 part1`

Expand All @@ -112,6 +114,79 @@ $ aoc day2 part1 [OPTIONS] [INPUT_PATH]

* `--help`: Show this message and exit.

### `aoc day2 part2`

Calculate the sum of all Game Powers

**Usage**:

```console
$ aoc day2 part2 [OPTIONS] [INPUT_PATH]
```

**Arguments**:

* `[INPUT_PATH]`: Path to file containing the input [default: data/day2.txt]

**Options**:

* `--help`: Show this message and exit.

## `aoc day4`

Command for running day4 stars

**Usage**:

```console
$ aoc day4 [OPTIONS] COMMAND [ARGS]...
```

**Options**:

* `--help`: Show this message and exit.

**Commands**:

* `part1`: Calculate the sum of all the scratch card...
* `part2`: Count the total number of scratch cards

### `aoc day4 part1`

Calculate the sum of all the scratch card scores

**Usage**:

```console
$ aoc day4 part1 [OPTIONS] [INPUT_PATH]
```

**Arguments**:

* `[INPUT_PATH]`: Path to file containing the input [default: data/day4.txt]

**Options**:

* `--help`: Show this message and exit.

### `aoc day4 part2`

Count the total number of scratch cards

**Usage**:

```console
$ aoc day4 part2 [OPTIONS] [INPUT_PATH]
```

**Arguments**:

* `[INPUT_PATH]`: Path to file containing the input [default: data/day4.txt]

**Options**:

* `--help`: Show this message and exit.

## `aoc debug-version-info`

Print debug information to terminal.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/day4/part1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## ::: aoc.day4.part1
1 change: 1 addition & 0 deletions docs/docs/day4/part2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## ::: aoc.day4.part2
3 changes: 3 additions & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ nav:
- Day 2:
- Part 1: day2/part1.md
- Part 2: day2/part2.md
- Day 4:
- Part 1: day4/part1.md
- Part 2: day4/part2.md

theme:
name: "material"
Expand Down
36 changes: 36 additions & 0 deletions src/aoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from aoc.day1.part2 import word_calibration_sum
from aoc.day2.part1 import sum_valid_game_ids
from aoc.day2.part2 import sum_game_powers
from aoc.day4.part1 import card_points
from aoc.day4.part2 import count_cards

__version__ = version(__package__)

Expand Down Expand Up @@ -143,3 +145,37 @@ def day2part2(


cli.add_typer(day2app, name="day2")

day4app = typer.Typer(
help="Command for running day4 stars",
rich_markup_mode="rich",
)


@day4app.command(name="part1")
def day4part1(
input_path: Path = typer.Argument(
Path("data/day4.txt"), help="Path to file containing the input"
),
) -> None:
"""Calculate the sum of all the scratch card scores"""
with open(input_path) as infile:
input_text = infile.read()
total_sum = card_points(input_text)
print(f"The sum of card scores is: [bold red]{total_sum}[/bold red]")


@day4app.command(name="part2")
def day4part2(
input_path: Path = typer.Argument(
Path("data/day4.txt"), help="Path to file containing the input"
),
) -> None:
"""Count the total number of scratch cards"""
with open(input_path) as infile:
input_text = infile.read()
total_sum = count_cards(input_text)
print(f"The final number of scratch cards is: [bold red]{total_sum}[/bold red]")


cli.add_typer(day4app, name="day4")
Empty file added src/aoc/day4/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions src/aoc/day4/part1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
# Day 4: Scratchcards
The gondola takes you up. Strangely, though, the ground doesn't seem to be coming with you; you're not climbing a mountain. As the circle of Snow Island recedes below you, an entire new landmass suddenly appears above you! The gondola carries you to the surface of the new island and lurches into the station.
As you exit the gondola, the first thing you notice is that the air here is much warmer than it was on Snow Island. It's also quite humid. Is this where the water source is?
The next thing you notice is an Elf sitting on the floor across the station in what seems to be a pile of colorful square cards.
"Oh! Hello!" The Elf excitedly runs over to you. "How may I be of service?" You ask about water sources.
"I'm not sure; I just operate the gondola lift. That does sound like something we'd have, though - this is Island Island, after all! I bet the gardener would know. He's on a different island, though - er, the small kind surrounded by water, not the floating kind. We really need to come up with a better naming scheme. Tell you what: if you can help me with something quick, I'll let you borrow my boat and you can go visit the gardener. I got all these scratchcards as a gift, but I can't figure out what I've won."
The Elf leads you over to the pile of colorful cards. There, you discover dozens of scratchcards, all with their opaque covering already scratched off. Picking one up, it looks like each card has two lists of numbers separated by a vertical bar (|): a list of winning numbers and then a list of numbers you have. You organize the information into a table (your puzzle input).
As far as the Elf has been able to figure out, you have to figure out which of the numbers you have appear in the list of winning numbers. The first match makes the card worth one point and each match after the first doubles the point value of that card.
For example:
```
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
```
In the above example, card 1 has five winning numbers (41, 48, 83, 86, and 17) and eight numbers you have (83, 86, 6, 31, 17, 9, 48, and 53). Of the numbers you have, four of them (48, 83, 17, and 86) are winning numbers! That means card 1 is worth 8 points (1 for the first match, then doubled three times for each of the three matches after the first).
Card 2 has two winning numbers (32 and 61), so it is worth 2 points.
Card 3 has two winning numbers (1 and 21), so it is worth 2 points.
Card 4 has one winning number (84), so it is worth 1 point.
Card 5 has no winning numbers, so it is worth no points.
Card 6 has no winning numbers, so it is worth no points.
So, in this example, the Elf's pile of scratchcards is worth 13 points.
Take a seat in the large pile of colorful cards. How many points are they worth in total?
# Solution
"""


def card_points(input_text: str) -> int:
"""Split the input into lines, convert each line into a pair of string sets.
Count the size of the intersection of the right from the left. Add 2^(|intersection| -1)
to the cumulative score if the intersection is nonzero
Args:
input_text: Raw input text.
Returns:
sum of all scratch card scores.
"""
lines = input_text.splitlines()
cum_score = 0
for line in lines:
numbers = line.split(": ")[1]
game, chosen = map(set, map(str.split, map(str.strip, numbers.split(" | "))))
intersection = game.intersection(chosen)
if len(intersection) > 0:
cum_score += 2 ** (len(intersection) - 1)
return cum_score
76 changes: 76 additions & 0 deletions src/aoc/day4/part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time.
There's no such thing as "points". Instead, scratchcards only cause you to win more scratchcards equal to the number of winning numbers you have.
Specifically, you win copies of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15.
Copies of scratchcards are scored like normal scratchcards and have the same card number as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.)
This time, the above example goes differently:
```
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
```
- Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5.
- Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4.
- Your copy of card 2 also wins one copy each of cards 3 and 4.
- Your four instances of card 3 (one original and three copies) have two matching numbers, so you win four copies each of cards 4 and 5.
- Your eight instances of card 4 (one original and seven copies) have one matching number, so you win eight copies of card 5.
- Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards.
- Your one instance of card 6 (one original) has no matching numbers and wins no more cards.
Once all of the originals and copies have been processed, you end up with 1 instance of card 1, 2 instances of card 2, 4 instances of card 3, 8 instances of card 4, 14 instances of card 5, and 1 instance of card 6. In total, this example pile of scratchcards causes you to ultimately have 30 scratchcards!
Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, how many total scratchcards do you end up with?
# Solution
"""

from dataclasses import dataclass


@dataclass
class Card:
"""Dataclass for a scratch card with id, game numbers, and winning numbers."""

game_id: int
game_nums: set[str]
chosen_nums: set[str]

def num_copies(self) -> int:
return len(self.chosen_nums.intersection(self.game_nums))


def count_cards(input_text: str) -> int:
"""Split the input into lines, convert each line into a Card object and append to a list of cards.
Iterate over the list of cards and add copies to the list as needed modifying the list inplace.
Return the length of the final list
Args:
input_text: Raw input text.
Returns:
Total number of created scratch cards.
"""
lines = input_text.splitlines()
cards = []
for game_id, line in enumerate(lines):
numbers = line.split(": ")[1]
cards.append(
Card(
game_id + 1,
*map(set, map(str.split, map(str.strip, numbers.split(" | ")))),
)
)
for card in cards:
num_copies = card.num_copies()
cards.extend(cards[card.game_id : card.game_id + num_copies])

return len(cards)
17 changes: 17 additions & 0 deletions tests/test_day4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from aoc.day4.part1 import card_points
from aoc.day4.part2 import count_cards

input_text = """Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"""


def test_card_points() -> None:
assert card_points(input_text) == 13


def test_count_cards() -> None:
assert count_cards(input_text) == 30

0 comments on commit 918cc20

Please sign in to comment.