-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
468 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[tool.poetry] | ||
name = "advent of code 2023" | ||
version = "1.0.0" | ||
description = "Personal solutions for the Advent of Code 2023" | ||
authors = ["Gilles Magalhaes <[email protected]>"] | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.10" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
black = "^23.11.0" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import re | ||
|
||
|
||
def parse_cards(input): | ||
cards = {} | ||
for line in input.splitlines(): | ||
card_number = int(line.split(":")[0].split(" ")[-1]) | ||
winning_numbers, my_numbers = [ | ||
set([int(n.strip()) for n in re.sub(" +", " ", arr).strip().split(" ")]) | ||
for arr in line.split(":")[1].split("|") | ||
] | ||
cards[card_number] = (winning_numbers, my_numbers) | ||
return cards | ||
|
||
|
||
def solution(input): | ||
total = 0 | ||
cards = parse_cards(input) | ||
for winning_numbers, my_numbers in cards.values(): | ||
r = len(my_numbers.intersection(winning_numbers)) | ||
if r == 0: | ||
continue | ||
total += 2 ** (r - 1) | ||
|
||
return total | ||
|
||
|
||
def solution_2(input): | ||
cards = list(parse_cards(input).items()) | ||
stack = [v for v in cards] | ||
card_count = {} | ||
while len(stack) > 0: | ||
card_number, (winning_numbers, my_numbers) = stack.pop() | ||
wins = len(my_numbers.intersection(winning_numbers)) | ||
if card_number not in card_count: | ||
card_count[card_number] = 0 | ||
card_count[card_number] += 1 | ||
stack.extend([cards[card] for card in range(card_number, card_number + wins)]) | ||
|
||
return sum(card_count.values()) | ||
|
||
|
||
def part_1(input): | ||
return solution(input) | ||
|
||
|
||
def part_2(input): | ||
return solution_2(input) | ||
|
||
|
||
def example_1(input): | ||
return solution(input) | ||
|
||
|
||
def example_2(input): | ||
return solution_2(input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters