From cf78ef9f04c2dc5a9d6f19a0edbdd908d4558bae Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 1 Dec 2024 17:38:01 +0100 Subject: [PATCH] Add 2024 day 01 --- 2024/day01.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 2024/day01.py diff --git a/2024/day01.py b/2024/day01.py new file mode 100755 index 0000000..a729a70 --- /dev/null +++ b/2024/day01.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 +""" +Jour 01 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", "day01.txt") + with open(filename, 'r') as f: + sample = f.read().split('\n') + sample = [ list(map(int, i.split())) for i in sample if i != '' ] + return sample + +def part1(sample): + """Partie 1 du défi""" + l1, l2 = sorted([l[0] for l in sample]), sorted([l[1] for l in sample]) + + return sum(abs(l1[i]-l2[i]) for i in range(len(l1))) + +def part2(sample): + """Partie 2 du défi""" + l1, l2 = sorted([l[0] for l in sample]), sorted([l[1] for l in sample]) + pres = {} + for i in l2: + if i not in pres: + pres[i] = 0 + pres[i] += 1 + + score = 0 + for i in l1: + if i in pres: + score += pres[i]*i + return score + + +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