From 223ad87e08354d237793ce1cd20fb416f32487fb Mon Sep 17 00:00:00 2001 From: Arthur Date: Sat, 2 Dec 2023 19:13:36 +0100 Subject: [PATCH] mis en fonction --- d02/run1.py | 64 ++++++++++++++++++++++++++--------------------------- d02/run2.py | 57 ++++++++++++++++++++++------------------------- 2 files changed, 58 insertions(+), 63 deletions(-) diff --git a/d02/run1.py b/d02/run1.py index bc29512..1458fa1 100644 --- a/d02/run1.py +++ b/d02/run1.py @@ -1,35 +1,33 @@ -filename = "exemple1.txt" +def main(filename, bag, result = 0): + with open(filename, 'r') as f: + for line in f.readlines(): + game = line.split(": ") + p = game[1].replace("\n","").split("; ") + i = int(game[0].split(" ")[1]) + result += i + #print(p) + # par pack + for c in p: + nb = [0, 0, 0] + #print(c) + # par jeton + for j in c.split(", "): + k = j.split(" ") + #print(j) + if k[1] == 'red': + nb[0] += int(k[0]) + elif k[1] == 'green': + nb[1] += int(k[0]) + elif k[1] == 'blue': + nb[2] += int(k[0]) + else: + print("error") + if nb[0] > bag[0] or nb[1] > bag[1] or nb[2] > bag[2]: + result -= i + break -#red / green / blue -bag = [12, 13, 14] + return result -result = 0 - -with open(filename, 'r') as f: - for line in f.readlines(): - game = line.split(": ") - p = game[1].replace("\n","").split("; ") - i = int(game[0].split(" ")[1]) - result += i - #print(p) - # par pack - for c in p: - nb = [0, 0, 0] - #print(c) - # par jeton - for j in c.split(", "): - k = j.split(" ") - #print(j) - if k[1] == 'red': - nb[0] += int(k[0]) - elif k[1] == 'green': - nb[1] += int(k[0]) - elif k[1] == 'blue': - nb[2] += int(k[0]) - else: - print("error") - if nb[0] > bag[0] or nb[1] > bag[1] or nb[2] > bag[2]: - result -= i - break - -print(result) \ No newline at end of file +if __name__ == '__main__': + #red / green / blue + print(main("input.txt", [12, 13, 14])) \ No newline at end of file diff --git a/d02/run2.py b/d02/run2.py index 3e5af0a..345f152 100644 --- a/d02/run2.py +++ b/d02/run2.py @@ -1,31 +1,28 @@ -filename = "input.txt" +def main(filename, bag, result = 0): + with open(filename, 'r') as f: + for line in f.readlines(): + game = line.split(": ") + p = game[1].replace("\n","").split("; ") + i = int(game[0].split(" ")[1]) + #print(p) + max = [0, 0, 0] + # par pack + for c in p: + #print(c) + # par jeton + for j in c.split(", "): + k = j.split(" ") + #print(j) + if k[1] == 'red' and max[0] < int(k[0]): + max[0] = int(k[0]) + elif k[1] == 'green' and max[1] < int(k[0]): + max[1] = int(k[0]) + elif k[1] == 'blue' and max[2] < int(k[0]): + max[2] = int(k[0]) + somme = max[0] * max[1] * max[2] + result += somme + return result -#red / green / blue -bag = [12, 13, 14] - -result = 0 - -with open(filename, 'r') as f: - for line in f.readlines(): - game = line.split(": ") - p = game[1].replace("\n","").split("; ") - i = int(game[0].split(" ")[1]) - #print(p) - max = [0, 0, 0] - # par pack - for c in p: - #print(c) - # par jeton - for j in c.split(", "): - k = j.split(" ") - #print(j) - if k[1] == 'red' and max[0] < int(k[0]): - max[0] = int(k[0]) - elif k[1] == 'green' and max[1] < int(k[0]): - max[1] = int(k[0]) - elif k[1] == 'blue' and max[2] < int(k[0]): - max[2] = int(k[0]) - somme = max[0] * max[1] * max[2] - result += somme - -print(result) \ No newline at end of file +if __name__ == '__main__': + #red / green / blue + print(main("input.txt", [12, 13, 14])) \ No newline at end of file