diff --git a/puzzles/solutions/2023/d01/p1.py b/puzzles/solutions/2023/d01/p1.py new file mode 100644 index 0000000..a8ad35a --- /dev/null +++ b/puzzles/solutions/2023/d01/p1.py @@ -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. diff --git a/puzzles/solutions/2023/d01/p2.py b/puzzles/solutions/2023/d01/p2.py new file mode 100644 index 0000000..e74d699 --- /dev/null +++ b/puzzles/solutions/2023/d01/p2.py @@ -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. diff --git a/puzzles/solutions/2023/d02/p1.py b/puzzles/solutions/2023/d02/p1.py new file mode 100644 index 0000000..3d9259a --- /dev/null +++ b/puzzles/solutions/2023/d02/p1.py @@ -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. diff --git a/puzzles/solutions/2023/d02/p2.py b/puzzles/solutions/2023/d02/p2.py new file mode 100644 index 0000000..729eb98 --- /dev/null +++ b/puzzles/solutions/2023/d02/p2.py @@ -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.