From c8a01d5df74138187bd38a01bb8a6652bd520118 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 3 Dec 2024 10:31:48 +0100 Subject: [PATCH] Add 2024 day 03 --- 2024/day03.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 2024/day03.py diff --git a/2024/day03.py b/2024/day03.py new file mode 100755 index 0000000..87817f3 --- /dev/null +++ b/2024/day03.py @@ -0,0 +1,43 @@ +#!/usr/bin/python3 +""" +Jour 03 du défi Advent Of Code pour l'année 2024 +""" +import os + +def read_sample(): + """récupère les entrées depuis le fichier texte correspondant""" + filename = os.path.join(os.path.dirname(__file__ ), "inputs", "day03.txt") + with open(filename, 'r') as f: + sample = f.read() + return sample + +def part1(sample): + """Partie 1 du défi""" + tot = 0 + spaces = sample.split("mul(")[1:] + for space in spaces: + space = space.split(")")[0] + try: + i, j = map(int, space.split(",")) + tot += i*j + except ValueError: + continue + return tot + + +def part2(sample): + """Partie 2 du défi""" + tot = 0 + for sample in sample.split("do()"): + tot += part1(sample.split("don't()")[0]) + return tot + + +def main(): + """Fonction principale""" + sample = read_sample() + print(f"part1: {part1(sample)}") + print(f"part2: {part2(sample)}") + +if __name__ == "__main__": + main() \ No newline at end of file