From 33da7d80939ba1f44c3851bbb08c28d216f49f6b Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 1 Dec 2023 10:49:10 +0100 Subject: [PATCH] Add 2023 day 01 --- 2023/day01.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 2023/day01.py diff --git a/2023/day01.py b/2023/day01.py new file mode 100755 index 0000000..9f6f1e2 --- /dev/null +++ b/2023/day01.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 +""" +Jour 01 du défi Advent Of Code pour l'année 2023 +""" + +def read_sample(): + """récupère les entrées depuis le fichier texte correspondant""" + with open('inputs/day01.txt', 'r') as f: + sample = f.read().split('\n') + sample = [ i for i in sample if i != '' ] + return sample + +def number(word): + nbs = (0, 0) + seen = 0 + for i in word: + if i >= '0' and i <= '9': + if seen == 0: + nbs = (int(i), int(i)) + seen += 1 + else: + nbs = (nbs[0], int(i)) + return 10*nbs[0]+nbs[1] + +def part1(sample): + """Partie 1 du défi""" + return sum([ + number(word) for word in sample + ]) + +def treat_input(sample): + pairs = [ + ("one", "o1e"), + ("two", "t2o"), + ("three", "th3ee"), + ("four", "fo4r"), + ("five", "f5ve"), + ("six", "s6x"), + ("seven", "se7en"), + ("eight", "eig8ht"), + ("nine", "ni9ne"), + ("zero", "ze0ro") + ] + for base, rep in pairs: + sample = [i.replace(base, rep) for i in sample] + + return sample + +def part2(sample): + """Partie 2 du défi""" + return sum([ + number(word) for word in treat_input(sample) + ]) + + +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