• # Plus simple

    Posté par (site web personnel) . En réponse au message Avent du Code, jour 10. Évalué à 3.

    J'ai trouvé ce jour assez facile.
    J'ai pas modélisé un CPU complet comme Tanguy, c'est un peu l'enclume pour écraser la mouche.
    L'astuce pour rester simple, c'est de remplacer une instruction add (sur deux cycles) par une instruction noop et une instruction add (sur un cycle). Après ça roule tout seul.

    #!/usr/bin/python3
    def yield_input():
     import sys
     with open(sys.argv[1]) as f:
     for l in f:
     l = l.strip()
     yield l
    def yield_command():
     for line in yield_input():
     if line == "noop":
     yield None
     if line.startswith("addx"):
     v = int(line[5:])
     yield None
     yield v
    def round_1():
     X = 1
     result = 0
     for cycle, command in enumerate(yield_command(), 1):
     if cycle in (20, 60, 100, 140, 180, 220):
     result += cycle*X
     if command is not None:
     X += command
     print("Round 1 :", result)
    def round_2():
     X = 1
     result = 0
     for cycle, command in enumerate(yield_command()):
     current_pos = cycle%40
     char = "#" if abs(current_pos-X) <= 1 else " "
     end = "\n" if current_pos == 39 else ""
     print(char, end=end)
     if command is not None:
     X += command
    round_1()
    round_2()

    Matthieu Gautier|irc:starmad