Wheeeeee another Easy Mode/Hard Mode day. Ok, the hard mode is, debatable-ish. It was pretty tedious to get working, though the basic idea was pretty obvious.

Today’s puzzle, the communicator from a previous puzzle is broken, and the screen needs fixed. So you get an input of data off the broken communicator. For Part 1, you find some totals of the fluctuating signal level and add them up for a solution. Pretty simple honestly.

Then part two. Whoo boy, Part 2.

For Part 2, the data needs decoded into a series of letters. The biggest hassle, was figuring out exactly what the instructions were asking. There was a stepped through example, but it ended a bit too early to really get an idea of what was happening. This visualization someone posted to Reddit, helps a LOT. It made it much more clear how to process the data.

I still wasn’t getting a proper answer. I wasn’t even getting the proper result for the sample data set, but, oddly, I was ALMOST getting the sample data set result. Like, the first line was fine, halfway through the second line, it just went off and became spotty. I solved that, but more on that in a bit, because it was the end.

I also feel like I overly complicated things by using a 2×2 matrix of data, instead of just a long string then splitting the result to print it. But the 2×2 Matrix seemed more fun. It would almost be fun to connect this code up to an LED Matrix screen for the result.

Anyway, after lots and lots of tweaking to my original loop for Part 1, which is probably non existent now, I manage to get the solution. I had to change how I was handling the signals, because one type used 2 cycles, and the other took one. I had to add several variables to account for the position in the matrix. Though tracking that wasn’t too hard using the mod (remainder) operator and some basic division.

I’m a little disappointed in my result, because I used an import on “math” so I could round the division result down. One thing I usually try to do with these is just use, Pure Python. A lot of replies on Mastodon to previous posts suggest different modules, most often Numpy, but I really like the extra little challenge of “No Imports, pure Python”. The annoying part is, I could modify the code a bit to make the math work without using “floor” from “math”, but I’ve been working on this off and on all day, and it works, so I don’t really care to bother.

I also realized I needed to account for looping the pixel placer around. Something I would not have needed had I just used long string instead of a 2×2 matrix.

Anyway, After sons of modifications, and maybe I need a +1 here or a -1 here, I managed to solve it, which I said I would get to. I had enclosed the Pixel Placing algorithm inside the signal generating commands only. I had stupidly assumed, “noop” (No Operation) meant “Do nothing”. It just means, the signal doesn’t change. So I rearranged things a bit so the loop always ran, just sometimes once, sometimes twice, and poof, there it was, the answer! Mostly, there was a pixel off at the end, probably side effect of my looping, but I had the result and it worked.

import math

with open("Day10Input.txt") as file:
    data = file.read()

moves = data.split('\n')

def make_display(pixels,lines):
    display = []
    display_line = []
    for n in range(pixels):
        display_line.append(" ")
    for i in range(lines):
        display.append(display_line.copy())
    return display

def show_display(display):
    for each in display:
        print("".join(each))
    print("\n")

signal_strengths = [0, 1]
cycles = 0
signal = 1 # X In the instructions
line = 0

display = make_display(40, 6)

for move in moves:
    command = move.split(" ")
    signal_strengths.append(signal)
    if command[0] == "noop":
        rangeend = 2
    else:
        rangeend = 3
    for p in range(1, rangeend):
        cycles += 1
        pixel = (cycles)%40
        line = math.floor((cycles)/40)

        if p == 2:
            signal += int(command[1])
            signal_strengths.append(signal)
        first = signal - 1
        last = signal + 1
        if first < 0:
            first = 39
            line -= 1
        if last > 39:
            last = 0
            line += 1
        if pixel in [first, signal, last]:
            display[line][pixel] = "#"

check_values = [20, 60, 100, 140, 180, 220]
total = 0
for each in check_values:
    # print(f"{signal_strengths[each-1]} | {signal_strengths[each]} | {signal_strengths[each]}")
    total += each * signal_strengths[each]
print(total)
# 14060

show_display(display)
## PAPKFKEJ