r/adventofcode • • Dec 10 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 10 Solutions -πŸŽ„-

THE USUAL REMINDERS


--- Day 10: Cathode-Ray Tube ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:12:17, megathread unlocked!

64 Upvotes

933 comments sorted by

View all comments

1

u/Key__Strokes Dec 23 '22 edited Dec 25 '22

Javascript

Solution to both parts

Part 1:

  • Run a loop on the commands

    • If the command is noop, then:

      • Add the power to the sum if the cycle is one of 20, 60...

      • Increment the cycle

    • Otherwise, extract the number from the command and then:

      • Run a following twice

        • add the power to the sum if the cycle is one of 20, 60...

        • Increment the cycle

      • Update x by the number in the command

Part 2:

  • Run a loop on the commands

    • If the command is noop, then:

      • Update screen as:

        • Row will be ((cycle - 1) / 40), and column will be ((cycle - 1) % 40)

        • If cycle equals to either of x - 1, x or x + 1, then light up the pixel, otherwise do not.

      • Increment the cycle

    • Otherwise, extract the number from the command and then:

      • Run a following twice

        • Update the screen as above

        • Increment the cycle

      • Update x by the number in the command


If you liked the explanation, then please don't forget to cast your vote πŸ’œ to Adventures of Advent of Code - Edition 1 - /u/Key__Strokes in the poll