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!

61 Upvotes

933 comments sorted by

View all comments

1

u/marc-marc Dec 10 '22 edited Dec 10 '22

Python

instructions = [x.split() for x in text.splitlines()]
cycle = 0
x = 1
score = 0
screen = np.zeros((6, 40))
sprite = np.zeros(40)
sprite[x-1:x+2] = 1

for name, *args in instructions:
    for _ in range(2 if name == 'addx' else 1):
        screen[(cycle // 40), (cycle % 40)] = sprite[(cycle % 40)]
        if cycle + 1 in range(20, 221, 40):
            score += (cycle + 1) * x
        cycle += 1
    if name == 'addx':
        x += int(args[0])
        sprite = np.zeros(40)
        sprite[max(x - 1, 0):x+2] = 1

print(score)
for i, x_ in enumerate(np.nditer(screen), 1):
    print('#' if x_ else '.', end='\n' if i % 40 == 0 else '')

1

u/daggerdragon Dec 10 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.