From f4e155263e8031ba29d122cba34e5cf2eaea9dcb Mon Sep 17 00:00:00 2001 From: augustin64 Date: Mon, 5 Dec 2022 10:08:26 +0100 Subject: [PATCH] Add 2022 day 05 --- 2022/day05.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 2022/day05.py diff --git a/2022/day05.py b/2022/day05.py new file mode 100755 index 0000000..c6e0946 --- /dev/null +++ b/2022/day05.py @@ -0,0 +1,69 @@ +#!/usr/bin/python3 +""" +Jour 05 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/day05.txt', 'r') as f: + sample = f.read().split('\n\n') + + base = [[] for _ in range(int(len(sample[0][:-1].split("\n")[0])/4)+1)] + for line in sample[0].split("\n"): + for j in range(0, len(line) -1, 4): + if line[j+1] != " ": + base[int(j/4)].append(line[j+1]) + + for i in base: + i.reverse() + + moves = [ (int(i.split(" ")[1]), int(i.split(" ")[3]), int(i.split(" ")[5])) for i in sample[1].split("\n") if i != '' ] + return (base, moves) + +def move1(nb, init, fin, base): + sp = base[init][-nb:] + base[init] = base[init][:-nb] + sp.reverse() + for i in sp: + base[fin].append(i) + return base + +def part1(sample): + """Partie 1 du défi""" + base, moves = sample + for (number, init, fin) in moves: + base = move1(number, init-1, fin-1, base) + + text = "" + for i in base: + text += i[-1] + + return text + +def move2(nb, init, fin, base): + sp = base[init][-nb:] + base[init] = base[init][:-nb] + for i in sp: + base[fin].append(i) + return base + +def part2(sample): + """Partie 1 du défi""" + base, moves = sample + for (number, init, fin) in moves: + base = move2(number, init-1, fin-1, base) + + text = "" + for i in base: + text += i[-1] + + return text + + +def main(): + """Fonction principale""" + print(f"part1: {part1(read_sample())}") + print(f"part2: {part2(read_sample())}") + +if __name__ == "__main__": + main() \ No newline at end of file