From 8be95055108b046bd106cfbf91d0a2f4959c7b3b Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 3 Dec 2022 14:09:23 +0100 Subject: [PATCH] Add 2022 day 03 --- 2022/day03.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 2022/day03.py diff --git a/2022/day03.py b/2022/day03.py new file mode 100755 index 0000000..6343d36 --- /dev/null +++ b/2022/day03.py @@ -0,0 +1,59 @@ +#!/usr/bin/python3 +""" +Jour 03 du défi Advent Of Code pour l'année 2022 +""" + +def read_sample(): + """récupère les entrées depuis le fichier texte correspondant""" + with open('inputs/day03.txt', 'r') as f: + sample = f.read().split('\n') + sample = [ i for i in sample if i != '' ] + return sample + +def split(text): + half = len(text)/2 + return text[:int(half)], text[int(half):] + +def commun(tup): + p1, p2 = tup + for i in p1: + if i in p2: + return i + raise Exception("Pas de lettre en commun") + +def priority(letter): + if ord(letter) <= 90: + return ord(letter)-65+27 + return ord(letter)-97+1 + +def part1(sample): + """Partie 1 du défi""" + return sum(priority(commun(split(i))) for i in sample) + + +def count(item, iterable): + nb = 0 + for i in iterable: + if i == item: + nb += 1 + return nb + +def part2(sample): + """Partie 2 du défi""" + somme = 0 + for i in range(0, len(sample), 3): + comm = list(sample[i]) + for j in comm: + if j in sample[i+1] and j in sample[i+2]: + break + somme += priority(j) + return somme + +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