aoc_omni/ruby/2017/5/problem.rb
2025-11-30 21:22:21 -05:00

49 lines
750 B
Ruby

class CPU
attr_reader :jumps, :steps
attr_accessor :stranger_jumps
def initialize(jumps)
@jumps = jumps
@pc = 0
@steps = 0
end
def self.for(string)
new string.split.map(&:to_i)
end
def step!
@steps += 1
if stranger_jumps && jumps[@pc] >= 3
jumps[@pc] -= 1
@pc += jumps[@pc] + 1
else
jumps[@pc] += 1
@pc += jumps[@pc] - 1
end
end
def run!
step! until done_running?
end
def done_running?
@pc < 0 || @pc >= jumps.length
end
end
jump_string = STDIN.read.chomp
cpu = CPU.for jump_string
cpu_2 = CPU.new cpu.jumps.clone
cpu.run!
part_1 = cpu.steps
puts "Part 1: #{part_1}"
cpu_2.stranger_jumps = true
cpu_2.run!
part_2 = cpu_2.steps
puts "Part 2: #{part_2}"