DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 5: Sunny with a Chance of Asteroids

Collapse
 
clareconstantine profile image
Clare Constantine (they/them) • Edited

I found the directions for this challenge much more confusing - and my confusion led to some weird bugs! But ultimately I'm pretty happy with my solution (in ruby):

def position? mode
  mode == 0
end

# return an array with the modes for the params (always length 3)
def get_modes instruction
  modes = instruction.to_s.chars.map(&:to_i)
  while modes.length < 5
    modes.unshift 0
  end
  modes[0..2]
  # flip the order so they are in order of the param order
  # [param_1_mode, param_2_mode, param_3_mode]
  modes = [modes[2], modes[1], modes[0]]
end

def compute input
  memory = [3,225,1,225,6,6,1100,1,238,225,104,0,2,136,183,224,101,-5304,224,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1101,72,47,225,1101,59,55,225,1101,46,75,225,1101,49,15,224,101,-64,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,102,9,210,224,1001,224,-270,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,101,14,35,224,101,-86,224,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1102,40,74,224,1001,224,-2960,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1101,10,78,225,1001,39,90,224,1001,224,-149,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1002,217,50,224,1001,224,-1650,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1102,68,8,225,1,43,214,224,1001,224,-126,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1102,88,30,225,1102,18,80,225,1102,33,28,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,677,677,224,102,2,223,223,1005,224,329,1001,223,1,223,1107,677,226,224,102,2,223,223,1006,224,344,1001,223,1,223,108,226,226,224,102,2,223,223,1005,224,359,1001,223,1,223,1108,677,226,224,102,2,223,223,1006,224,374,101,1,223,223,108,677,226,224,102,2,223,223,1006,224,389,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,404,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,419,101,1,223,223,1107,677,677,224,102,2,223,223,1006,224,434,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,449,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1108,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,494,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,509,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,524,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,539,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,554,101,1,223,223,107,226,677,224,1002,223,2,223,1005,224,569,1001,223,1,223,1108,677,677,224,1002,223,2,223,1006,224,584,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,599,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,614,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,107,677,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,677,224,1002,223,2,223,1005,224,659,1001,223,1,223,1007,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226]
  i = 0
  diagnostic_code = nil

  while i < memory.length
    opcode = memory[i] % 100 # the opcode is the 1's and 10's place

    modes = get_modes(memory[i])
    mode_1 = modes[0]
    mode_2 = modes[1]
    mode_3 = modes[2]

    if [1, 2, 4, 5, 6, 7, 8].include? opcode
      param_1 = memory[(position?(mode_1) ? memory[i+1] : i+1)]
    end

    if [1, 2, 5, 6, 7, 8].include? opcode
      param_2 = memory[(position?(mode_2) ? memory[i+2] : i+2)]
    end

    if opcode == 1
      # add
      memory[(position?(mode_3) ? memory[i+3] : i+3)] = param_1 + param_2
      i += 4
    elsif opcode == 2
      # multiply
      memory[(position?(mode_3) ? memory[i+3] : i+3)] = param_1 * param_2
      i += 4
    elsif opcode == 3
      # input
      memory[(position?(mode_1) ? memory[i+1] : i+1)] = input
      i += 2
    elsif opcode == 4
      # output
      diagnostic_code = param_1
      i += 2
    elsif opcode == 5
      # jump-if-true
      if param_1 != 0
        i = param_2
      else
        i += 3
      end
    elsif opcode == 6
      # jump-if-false
      if param_1 == 0
        i = param_2
      else
        i += 3
      end
    elsif opcode == 7
      # less than
      memory[(position?(mode_3) ? memory[i+3] : i+3)] = (param_1 < param_2 ? 1 : 0)
      i += 4
    elsif opcode == 8
      # equals
      memory[(position?(mode_3) ? memory[i+3] : i+3)] = (param_1 == param_2 ? 1 : 0)
      i += 4
    elsif opcode == 99
      # halt
      break
    end
  end
  diagnostic_code
end

puts compute 1
puts compute 5