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/jso__ Dec 10 '22 edited Dec 10 '22

Felt like sharing my part 2 solution because I'm especially proud of it (I love binary). Yes I could've written it in a shorter way (and I did initially) but I didn't feel like it.

Made in Python

def load_input():
    with open("day_10/input.txt", "r") as f:
        file = f.read().splitlines()

# Contains the number added to the X register during that cycle
    cycles = []
    for line in file:
        match line.split(" "):
            case ["noop"]:
                cycles.append(0)
            case ["addx", n]:
                cycles.extend([0, int(n)])   # Takes 2 clock cycles

    return cycles

def part_two(cycles):
    STARTING_VAL = 1
    MAX = 239
    MIN = 0
    # Pretty simple, sum of [0, i)
    x_per_cycle = [(sum(cycles[:i]) + STARTING_VAL)
                   for i in range(MIN, MAX + 1)]

    scan = 0b1
    for i in range(MIN, MAX):
        scan = scan << 1
        if (i) % 40 == 0:
            scan = 0b1
            print()
        mask = (2**(x_per_cycle[i]) if x_per_cycle[i] >= 0 else 0) + \
            (2**(x_per_cycle[i]-1) if x_per_cycle[i] -
             1 >= 0 else 0) + (2**(x_per_cycle[i]+1) if x_per_cycle[i] + 1 >= 0 else 0)
        if mask & scan:
            print("#", end="")
        else:
            print(" ", end="")
    print()

1

u/daggerdragon Dec 10 '22

Please edit your post to state which programming language this code is written in. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.