From eff16cced47dfb2b602a1e3c277f7d501efd7fde Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 9 Dec 2024 21:45:42 +0100 Subject: [PATCH] add 2023/day1-2 --- 2023/01/main.py | 58 ++++++++++++++++++++++++++++++++ 2023/02/main.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 2023/01/main.py create mode 100644 2023/02/main.py diff --git a/2023/01/main.py b/2023/01/main.py new file mode 100644 index 0000000..9e7424f --- /dev/null +++ b/2023/01/main.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + +import re + + +def readinput(): + with open("input", "r", encoding="utf-8") as file: + lines = file.readlines() + return lines + + +def get_number(line: str) -> int: + regex = r"\d" + matches = re.findall(regex, line) + new_number = matches[0] + matches[-1] + return int(new_number) + + +def get_spelled_number(line: str) -> int: + number_map = { + "one": "o1e", + "two": "t2o", + "three": "t3e", + "four": "f4r", + "five": "f5e", + "six": "s6x", + "seven": "s7n", + "eight": "e8t", + "nine": "n9e", + } + + for k, v in number_map.items(): + if k in line: + line = line.replace(k, v) + + return get_number(line) + + +def main(): + lines = readinput() + + # part1 + count = sum(get_number(line) for line in lines) + print("Sum: %d" % count) + + # part1 + count = sum(get_spelled_number(line) for line in lines) + print("Sum (literals): %d" % count) + + +if __name__ == "__main__": + main() diff --git a/2023/02/main.py b/2023/02/main.py new file mode 100644 index 0000000..8b25efd --- /dev/null +++ b/2023/02/main.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + +import re + + +def readinput(): + with open("input", "r", encoding="utf-8") as file: + lines = file.readlines() + return lines + + +def is_playable(line: str, max_cubes: tuple) -> bool: + games = line.split(";") + + for game in games: + cube_count = {"red": 0, "green": 0, "blue": 0} + + regex = r"(\d+) (\w+)" + matches = re.findall(regex, game) + + for m in matches: + color = m[1] + count = int(m[0]) + cube_count[color] += count + + for color, count in cube_count.items(): + if count > max_cubes[color]: + return False + + return True + + +def get_game_nr(line: str) -> int: + regex = r"Game (\d+):" + matches = re.findall(regex, line) + return int(matches[0]) + + +def get_cube_power(line: str) -> int: + + cube_count = {"red": 0, "green": 0, "blue": 0} + + regex = r"(\d+) (\w+)" + matches = re.findall(regex, line) + + for m in matches: + color = m[1] + count = int(m[0]) + if count > cube_count[color]: + cube_count[color] = count + + product = 1 + for value in cube_count.values(): + product *= value + + return product + + +def main(): + lines = readinput() + + max_cubes = { + "red": 12, + "green": 13, + "blue": 14, + } + + # part 1 + count = 0 + for line in lines: + if is_playable(line, max_cubes): + count += get_game_nr(line) + print("Playable games: %d" % count) + + # part 2 + count = 0 + for line in lines: + count += get_cube_power(line) + print("Set power sum: %d" % count) + + +if __name__ == "__main__": + main()