Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve 2023 day 02 #32

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
23 changes: 23 additions & 0 deletions puzzles/solutions/2023/d01/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re
import sys


def calculate_calibration_values_sum(input_text: str) -> int:
calibration_values_sum = 0
for line in input_text.splitlines():
numbers = re.findall(r"\d", line)
calibration_value = int(numbers[0]) * 10 + int(numbers[-1])
calibration_values_sum += calibration_value
return calibration_values_sum


def get_answer(input_text: str) -> int:
"""Return the calibrations values sum."""
return calculate_calibration_values_sum(input_text)


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
38 changes: 38 additions & 0 deletions puzzles/solutions/2023/d01/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

import p1


DIGITS_NAMES_TO_NUMBERS = dict(
zip(
("one", "two", "three", "four", "five", "six", "seven", "eight", "nine"),
(map(str, range(1, 10))),
)
)


def _convert_digits_names_to_numbers(input_text: str) -> str:
new_text = ""
for line in input_text.splitlines():
new_line = line[0]
for i in range(1, len(line)):
current_part_of_line = line[: i + 1]
new_line += line[i]
for digit_name in DIGITS_NAMES_TO_NUMBERS:
if current_part_of_line.endswith(digit_name):
new_line += DIGITS_NAMES_TO_NUMBERS[digit_name]
new_text += new_line + "\n"
return new_text


def get_answer(input_text: str):
"""Return the calibrations values sum where digits names are replaced with numbers."""
input_text = _convert_digits_names_to_numbers(input_text)
return p1.calculate_calibration_values_sum(input_text)


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
42 changes: 42 additions & 0 deletions puzzles/solutions/2023/d02/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sys


MAXIMAL_AMOUNTS = {"red": 12, "green": 13, "blue": 14}


def get_games_info(input_text: str) -> list[[list[dict[str, int]]]]:
games_info = []
for line in input_text.splitlines():
game_info = []
rounds = line.split(": ")[1].split("; ")
for round_info in rounds:
round_color_amounts = {}
color_amounts = round_info.split(", ")
for color in color_amounts:
amount, color = color.split()
round_color_amounts[color] = int(amount)
game_info.append(round_color_amounts)
games_info.append(game_info)

return games_info


def get_answer(input_text: str) -> int:
"""Return the sum of numbers of possible games."""
games_info = get_games_info(input_text)
possible_games_numbers_sum = 0
for game_number, game in enumerate(games_info, start=1):
if all(
amount <= MAXIMAL_AMOUNTS[color]
for round_info in game
for color, amount in round_info.items()
):
possible_games_numbers_sum += game_number
return possible_games_numbers_sum


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
30 changes: 30 additions & 0 deletions puzzles/solutions/2023/d02/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import collections
import functools
import operator
import sys

import p1


def get_answer(input_text: str):
"""Return sum of power of set of cubes, where set is the fewest amounts of cubes colors to be used in that game."""
games_info = p1.get_games_info(input_text)
set_cubes_power_sum = 0
for game in games_info:
color_to_maximal_amount = collections.defaultdict(int)
for round_info in game:
for color, amount in round_info.items():
color_to_maximal_amount[color] = max(
amount, color_to_maximal_amount[color]
)
set_cubes_power_sum += functools.reduce(
operator.mul, color_to_maximal_amount.values()
)
return set_cubes_power_sum


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.