Skip to content

Commit

Permalink
Day 04
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesmag committed Dec 4, 2023
1 parent f6e3a7e commit 9f5b027
Show file tree
Hide file tree
Showing 8 changed files with 468 additions and 9 deletions.
6 changes: 6 additions & 0 deletions 2023/examples/day04-1.txt
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
6 changes: 6 additions & 0 deletions 2023/examples/day04-2.txt
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
219 changes: 219 additions & 0 deletions 2023/inputs/day04.txt

Large diffs are not rendered by default.

143 changes: 143 additions & 0 deletions 2023/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions 2023/pyproject.toml
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"
21 changes: 15 additions & 6 deletions 2023/solutions/day01.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re


def solution(input, skip_spelled=False):
number_mapping = {
"one": 1,
Expand All @@ -10,30 +11,38 @@ def solution(input, skip_spelled=False):
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9
"nine": 9,
}
total = 0
for line in input.strip().splitlines():
digit_indices = [(idx, c) for idx, c in enumerate(line) if c.isdigit()]
spelled_indices = [
(match.start(), str(number_mapping[num]))
for num in number_mapping.keys()
for match in re.finditer(num, line)
] if not skip_spelled else []
spelled_indices = (
[
(match.start(), str(number_mapping[num]))
for num in number_mapping.keys()
for match in re.finditer(num, line)
]
if not skip_spelled
else []
)

combined = sorted(digit_indices + spelled_indices)
result = int(combined[0][1] + combined[-1][1])
total += result
return total


def part_1(input):
return solution(input, skip_spelled=True)


def example_1(input):
return solution(input, skip_spelled=True)


def example_2(input):
return solution(input)


def part_2(input):
return solution(input)
56 changes: 56 additions & 0 deletions 2023/solutions/day04.py
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)
10 changes: 7 additions & 3 deletions 2023/solutions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
import importlib


def main():
days = sorted([p.stem for p in Path(__file__).parent.glob("day*.py")])

Expand All @@ -13,15 +14,18 @@ def main():
print(message)
print("=" * len(message))
for idx, example in enumerate(examples):
result = getattr(module, example)(open(EXAMPLES_DIR / f"{day}-{idx+1}.txt").read())
result = getattr(module, example)(
open(EXAMPLES_DIR / f"{day}-{idx+1}.txt").read()
)
print(f" Example {idx+1}:", result)

for idx, part in enumerate(parts):
result = getattr(module, part)(open(INPUTS_DIR / f"{day}.txt").read())
print(f" Part {idx+1} :", result)

if didx > 0:
print()
if didx >= 0:
print("\n")


if __name__ == "__main__":
main()

0 comments on commit 9f5b027

Please sign in to comment.