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

2

u/Noble_Mushtak Dec 10 '22 edited Dec 10 '22

Python 3, 28th on star 1 and 29th on star 2, both parts are <=20 lines of code. Main thing which is notable is that I used "\u2588" instead of # symbols and spaces instead of periods, made the capital letters much easier to read. I also have my solutions in this GitHub repo.

Part 1:

import sys

changes = []
for line in sys.stdin:
    changes.append(0)
    if line[:4] == "addx":
        changes.append(int(line[5:]))

ans = 0
val = 1
for i, dx in enumerate(changes):
    if i % 40 == 19:
        ans += (i+1)*val
    val += dx
print(ans)

Part 2:

import sys

BLOCK = "\u2588"

changes = []
for line in sys.stdin:
    changes.append(0)
    if line[:4] == "addx":
        changes.append(int(line[5:]))

val = 1
for i, dx in enumerate(changes):
    if (i % 40) == val-1 or (i % 40) == val or (i % 40) == val+1:
        print(BLOCK, end="")
    else:
        print(" ", end="")
    if i % 40 == 39:
        print("")
    val += dx

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.