r/adventofcode • u/daggerdragon • Dec 10 '22
SOLUTION MEGATHREAD -π- 2022 Day 10 Solutions -π-
THE USUAL REMINDERS
All of our rules, FAQs, resources, etc. are in our community wiki.
Signal boost: Reminder 1: unofficial AoC Survey 2022 (closes Dec 22nd)
πΏπ MisTILtoe Elf-ucation π§βπ« is OPEN for submissions!
--- Day 10: Cathode-Ray Tube ---
Post your code solution in this megathread.
Read the full posting rules in our community wiki before you post!
Include what language(s) your solution uses
Format your code appropriately! How do I format code?
Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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!
61
Upvotes
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__Strokesin the poll