From e802ba1c58b5313d60dfb171945dd036ca57ffd6 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Thu, 1 Dec 2022 18:11:23 +0100 Subject: [PATCH] Add 2022 day 01 --- .gitignore | 3 +++ 2022/day01.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 .gitignore create mode 100755 2022/day01.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8aaf49 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +utils/cookie.txt + +*/inputs \ No newline at end of file diff --git a/2022/day01.py b/2022/day01.py new file mode 100755 index 0000000..37152ae --- /dev/null +++ b/2022/day01.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +""" +Jour 01 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/day01.txt', 'r') as f: + sample = f.read().split('\n\n') + sample = [ [int(j) for j in i.split("\n") if j != ''] for i in sample if i != '' ] + return sample + +def part1(sample): + """Partie 1 du défi""" + return max([sum(i) for i in sample]) + +def part2(sample): + """Partie 2 du défi""" + sums = [sum(i) for i in sample] + sums.sort() + return sums[-1] + sums[-2] + sums[-3] + + +def main(): + """Fonction principale""" + sample = read_sample() + print(f"part1: {part1(sample)}") + print(f"part2: {part2(sample)}")