nothe

She's a Nerd!

  • she/her

Knitter, sewist, Seattle Soccer Fan, occasional tech stuff, whatever else tickles my fancy. Cilantro is fine, but pickles are a waste of a perfectly good cucumber.
@nothe@notacult.social


Today's was relatively simple and straightforward, today, which was a relief! (Also solved my weird timing bug at work today so that's nice!) I don't have any complaints about this one, although a couple hot tips, maybe...

below the fold, of course!


First interesting thing was Hot Tip from The Boss - since it's on StringProtocol, not String, I hadn't noticed that enumerateLines(invoking:) existed, so I didn't have to load my whole input into a string and split it. We haven't had to work with inputs so big that it would matter (yet!), but I live in fear! ;)

The nice part about that was looking up the api reminded me about inout parameters, which I used heavily this solution! :)

I again set up some easy-reading/parsing code, specifically an enum of instruction types, and a struct for the instruction, along with the convenient perform() method, which updated the clock, the x register, and for part 2, the screen pixel index.

    func perform(xRegister: inout Int) -> Int { // consumed cycles
        switch(instruction) {
        case .noop:
            return 1
        case .addx:
            xRegister += count
            return 2
        }
    }

Instead of just writing out the 6 register checks we needed for pt 1, I did it slightly algorithmically, which made me feel fancy and tidy. (It's not actually impressive, it's late, and I'm a dork.)

For part 2, I decided not to even worry about where the next row fell, which was a mistake, because it throws off your concept of where the heck the sprite is, in relation to the currently being animated pixel, since it doesn't care about it's vertical position, only that it has wrapped. My solution? (I'm not proud, but it does the job.)

        if pixel == 41 || pixel == 40 {
            pixel -= 40
        }

The way I wrote it, there's a chance that the first pixel of subsequent rows is wrong, which can be seen in my output, with the dodgy resulting R, but I called it a victory none the less. I don't think there's anything particularly interesting in the actual algorithm, but you can see it in github, as per usual!

(https://github.com/nothes/AdventOfCode/blob/main/AoC/AoC/Day%2010/day10.swift)


You must log in to comment.