From 1d7b0f481812256b926e5487fe47a480bb7a027e Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 1 Dec 2023 14:46:30 +0200 Subject: [PATCH 01/13] Create solution file for part 1 --- puzzles/solutions/2023/d01/p1.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 puzzles/solutions/2023/d01/p1.py diff --git a/puzzles/solutions/2023/d01/p1.py b/puzzles/solutions/2023/d01/p1.py new file mode 100644 index 00000000..c3bbf496 --- /dev/null +++ b/puzzles/solutions/2023/d01/p1.py @@ -0,0 +1,12 @@ +import sys + + +def get_answer(input_text: str): + raise NotImplementedError + + +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. From 6678cb84744002b94e9770e66fc066ca7ed21efd Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 12:56:35 +0200 Subject: [PATCH 02/13] Add function that calculates calibration values sum --- puzzles/solutions/2023/d01/p1.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/puzzles/solutions/2023/d01/p1.py b/puzzles/solutions/2023/d01/p1.py index c3bbf496..17c16652 100644 --- a/puzzles/solutions/2023/d01/p1.py +++ b/puzzles/solutions/2023/d01/p1.py @@ -1,6 +1,16 @@ +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): raise NotImplementedError From 118e2b6af4f6a7d3268cb24aa4b180e057b7d2de Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 1 Dec 2023 14:47:42 +0200 Subject: [PATCH 03/13] Add function that returns the calculated sum --- puzzles/solutions/2023/d01/p1.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/puzzles/solutions/2023/d01/p1.py b/puzzles/solutions/2023/d01/p1.py index 17c16652..a8ad35a5 100644 --- a/puzzles/solutions/2023/d01/p1.py +++ b/puzzles/solutions/2023/d01/p1.py @@ -11,8 +11,9 @@ def calculate_calibration_values_sum(input_text: str) -> int: return calibration_values_sum -def get_answer(input_text: str): - raise NotImplementedError +def get_answer(input_text: str) -> int: + """Return the calibrations values sum.""" + return calculate_calibration_values_sum(input_text) if __name__ == "__main__": From 547c9327cffba404542e655761f59e7be177ac74 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 1 Dec 2023 14:48:10 +0200 Subject: [PATCH 04/13] Create solution file for part 2 --- puzzles/solutions/2023/d01/p2.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 puzzles/solutions/2023/d01/p2.py diff --git a/puzzles/solutions/2023/d01/p2.py b/puzzles/solutions/2023/d01/p2.py new file mode 100644 index 00000000..c3bbf496 --- /dev/null +++ b/puzzles/solutions/2023/d01/p2.py @@ -0,0 +1,12 @@ +import sys + + +def get_answer(input_text: str): + raise NotImplementedError + + +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. From d3d98413ae8bf5d9a6028d6fbce94e438fce69af Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 1 Dec 2023 14:52:42 +0200 Subject: [PATCH 05/13] Create mapping from digits names to numbers --- puzzles/solutions/2023/d01/p2.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/puzzles/solutions/2023/d01/p2.py b/puzzles/solutions/2023/d01/p2.py index c3bbf496..4824175a 100644 --- a/puzzles/solutions/2023/d01/p2.py +++ b/puzzles/solutions/2023/d01/p2.py @@ -1,5 +1,15 @@ 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 get_answer(input_text: str): raise NotImplementedError From 610d068bf3529a93459aa2889666ba3296598491 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 1 Dec 2023 20:32:03 +0200 Subject: [PATCH 06/13] Add function that converts digits names to numbers --- puzzles/solutions/2023/d01/p2.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/puzzles/solutions/2023/d01/p2.py b/puzzles/solutions/2023/d01/p2.py index 4824175a..00360c6b 100644 --- a/puzzles/solutions/2023/d01/p2.py +++ b/puzzles/solutions/2023/d01/p2.py @@ -11,6 +11,20 @@ ) +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): raise NotImplementedError From 232e2dd13594da929bdbac38337071207a20438d Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Fri, 1 Dec 2023 20:33:35 +0200 Subject: [PATCH 07/13] Add function that returns calculated sum after new rules --- puzzles/solutions/2023/d01/p2.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/puzzles/solutions/2023/d01/p2.py b/puzzles/solutions/2023/d01/p2.py index 00360c6b..e74d6992 100644 --- a/puzzles/solutions/2023/d01/p2.py +++ b/puzzles/solutions/2023/d01/p2.py @@ -26,7 +26,9 @@ def _convert_digits_names_to_numbers(input_text: str) -> str: def get_answer(input_text: str): - raise NotImplementedError + """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__": From 3584e9b616a6407e8cfddef6e5079da3045943b8 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 13:27:11 +0200 Subject: [PATCH 08/13] Create solution file for part 1 --- puzzles/solutions/2023/d02/p1.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 puzzles/solutions/2023/d02/p1.py diff --git a/puzzles/solutions/2023/d02/p1.py b/puzzles/solutions/2023/d02/p1.py new file mode 100644 index 00000000..c3bbf496 --- /dev/null +++ b/puzzles/solutions/2023/d02/p1.py @@ -0,0 +1,12 @@ +import sys + + +def get_answer(input_text: str): + raise NotImplementedError + + +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. From 9b12179f8eb8fd95b7c8b30ddfd00009fbf77ce3 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 13:47:16 +0200 Subject: [PATCH 09/13] Add function that parses games info from the input --- puzzles/solutions/2023/d02/p1.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/puzzles/solutions/2023/d02/p1.py b/puzzles/solutions/2023/d02/p1.py index c3bbf496..288edc72 100644 --- a/puzzles/solutions/2023/d02/p1.py +++ b/puzzles/solutions/2023/d02/p1.py @@ -1,6 +1,23 @@ import sys +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): raise NotImplementedError From 0ed8d3c142530824f3d732f5abcffd98ff178c0c Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 14:00:12 +0200 Subject: [PATCH 10/13] Add maximal color amounts constant --- puzzles/solutions/2023/d02/p1.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/puzzles/solutions/2023/d02/p1.py b/puzzles/solutions/2023/d02/p1.py index 288edc72..46213b20 100644 --- a/puzzles/solutions/2023/d02/p1.py +++ b/puzzles/solutions/2023/d02/p1.py @@ -1,6 +1,9 @@ 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(): From c57056bcb1a55de2af5f3025987386431c42b457 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 14:00:48 +0200 Subject: [PATCH 11/13] Add function that returns the sum of numbers of possible games --- puzzles/solutions/2023/d02/p1.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/puzzles/solutions/2023/d02/p1.py b/puzzles/solutions/2023/d02/p1.py index 46213b20..3d9259aa 100644 --- a/puzzles/solutions/2023/d02/p1.py +++ b/puzzles/solutions/2023/d02/p1.py @@ -21,8 +21,18 @@ def get_games_info(input_text: str) -> list[[list[dict[str, int]]]]: return games_info -def get_answer(input_text: str): - raise NotImplementedError +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__": From 092af436b5ddb39e020ee395967e212e24706138 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 14:03:55 +0200 Subject: [PATCH 12/13] Create solution file for part 2 --- puzzles/solutions/2023/d02/p2.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 puzzles/solutions/2023/d02/p2.py diff --git a/puzzles/solutions/2023/d02/p2.py b/puzzles/solutions/2023/d02/p2.py new file mode 100644 index 00000000..c3bbf496 --- /dev/null +++ b/puzzles/solutions/2023/d02/p2.py @@ -0,0 +1,12 @@ +import sys + + +def get_answer(input_text: str): + raise NotImplementedError + + +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. From 8ce22486165af703945bdd6f7c488e364f8a9de4 Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 2 Dec 2023 14:36:15 +0200 Subject: [PATCH 13/13] Add function that returns sum of powers of sets of cubes --- puzzles/solutions/2023/d02/p2.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/puzzles/solutions/2023/d02/p2.py b/puzzles/solutions/2023/d02/p2.py index c3bbf496..729eb984 100644 --- a/puzzles/solutions/2023/d02/p2.py +++ b/puzzles/solutions/2023/d02/p2.py @@ -1,8 +1,26 @@ +import collections +import functools +import operator import sys +import p1 + def get_answer(input_text: str): - raise NotImplementedError + """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__":