DEV Community

Cover image for Cathode-Ray Tube
Robert Mion
Robert Mion

Posted on

Cathode-Ray Tube

Advent of Code 2022 Day 10

Part 1

  1. Ugh, assembly code
  2. Tracking each cycle: initial thoughts
  3. My algorithm in JavaScript
  4. My algorithm in correct pseudocode

Ugh, assembly code

  • Thankfully, there are only two operations
  • And only a few hundred cycles are needed to be run
  • So it seems doable for me

However, I sense a performance test in Part 2 that I will find more confusing and/or too difficult to troubleshoot and solve.

Hopefully I can earn at least one gold star today, too!

Tracking each cycle: initial thoughts

  • addx takes two cycles
  • noop takes one cycle
  • I need to complete 220 cycles
  • And I need to know the value of X at the end of each cycle

Hmmmm.

I think I need these variables:

Set X to 1
Set cycles to 0
Set timer to 0
Set line to -1
Set records to an empty list
Set checkpoints to [20, 60, 100, 140, 180, 220]
Enter fullscreen mode Exit fullscreen mode

I need to iterate through the input line by line, but some lines will need an additional turn, so I can't use a normal loop. I think I'll need a while loop.

I'm still not sure what order of operations I need inside the while loop.

Here's where my head is at before moving to write code:

While cycles <= 220
  Increment cycles by 1
  If cycles is in checkpoints
    Add the product of X and cycles to records
  If timer is 0
    Increment line
    Check the current operation (at index line)
    If it is noop
      Set timer to 1
    Else - it is addx
      Set timer to 2
  Else - timer is > 0
    Decrement timer by 1
Enter fullscreen mode Exit fullscreen mode

I know it's not right. But I need to start playing with code. Writing pseudocode isn't helping.

My algorithm in JavaScript

let program = input
  .split('\n')
  .map(line => {
    if (line.split(' ').length == 1) {
      return false
    } else {
      return +line.split(' ')[1]
    }
  })
let X = 1
let cycle = 0
let index = -1
let records = []
let timer = 1
let checkpoints = [20,60,100,140,180,220]
while (cycle <= 220) {
  if (checkpoints.includes(cycle)) records.push(cycle * X)
  cycle++
  timer--
  if (timer == 0) {
    X += typeof program[index] == 'number' ? program[index] : 0
    index++
    timer += typeof program[index] == 'boolean' ? 1 : 2
  }
}
return records.reduce((sum, num) => sum + num)
Enter fullscreen mode Exit fullscreen mode

My algorithm in correct pseudocode

Set X to 1
Set cycles to 0
Set timer to 1
Set line to -1
Set records to an empty list
Set checkpoints to [20, 60, 100, 140, 180, 220]

While cycles <= 220
  If cycles is in checkpoints
    Add the product of X and cycles to records
  Increment cycles by 1
  Decrement timer by 1 unless doing that would cause it to become less than 1 - in which case, keep it 0
  If timer is 0
    Increment X depending on the operation:
      If it's noop, by 0
      If it's addx, by the number
    Move forward one operation
    Increment timer depending on the now-current operation:
      If it's noop, by 1
      If it's addx, by 2
Return the sum of all values in records
Enter fullscreen mode Exit fullscreen mode

Part 2

  1. Enter: position tracker, range and modulo
  2. My updated algorithm in JavaScript

Enter: position tracker, range and modulo

  • Position tracker will go from 0 to 239, one less than cycle
  • I need it to go from 0-39 six times
  • And I'll check whether it is one less than, equal to, or one greater than the value of X each cycle

Hopefully this will be as easy as it seems to incorporate into my Part 1 algorithm.

My updated algorithm in JavaScript

// Existing variables
let pixels = []
while (cycle < 240) {
  cycle++
  timer--
  if (timer == 0) {
    X += typeof program[index] == 'number' ? program[index] : 0
    index++
    timer += typeof program[index] == 'boolean' ? 1 : 2
  }
  pixels.push(
    [X - 1, X, X + 1].includes((cycle - 1) % 40) ? '#': '.'
  )
}
let CRT = []
for (let i = 0; i < pixels.length; i += 40) {
  CRT.push(pixels.slice(i, i + 40).join(''))
}
return CRT.join('\n')
Enter fullscreen mode Exit fullscreen mode

I used the example input to get it to output the expected 40x6 pixel grid.

Then I swapped to my input and got the expected 8-capital-letter code!

I did it!!

  • I solved both parts!
  • I used a combination of pseudocode and trial-and-error programming to work my way toward the answer to Part 1!
  • I only had to make a few edits and additions to reveal the answer to Part 2!

I've made a few message-revealing simulators in past years.

Thus, I'm not too excited to make another one for this article.

I'll hang my head high having earned two stars and rest my head for tomorrow's puzzle.

Top comments (0)