def run(lines, input) output = [] of Int32 pos = 0 get = ->(x : Char, p : Int32) { x == '0' ? lines[lines[pos+p]] : lines[pos+p] } loop do str = "000" + lines[pos].to_s case str when .ends_with?("1") lines[lines[pos+3]] = get.call(str[-3], 1) + get.call(str[-4], 2) pos += 4 when .ends_with?("2") lines[lines[pos+3]] = get.call(str[-3], 1) * get.call(str[-4], 2) pos += 4 when .ends_with?("3") lines[lines[pos+1]] = input.pop pos += 2 when .ends_with?("4") output << get.call(str[-3], 1) pos += 2 when .ends_with?("5") if get.call(str[-3], 1) != 0 pos = get.call(str[-4], 2) else pos += 3 end when .ends_with?("6") if get.call(str[-3], 1) == 0 pos = get.call(str[-4], 2) else pos += 3 end when .ends_with?("7") lines[lines[pos+3]] = (get.call(str[-3], 1) < get.call(str[-4], 2)) ? 1 : 0 pos += 4 when .ends_with?("8") lines[lines[pos+3]] = (get.call(str[-3], 1) == get.call(str[-4], 2)) ? 1 : 0 pos += 4 when .ends_with?("99") break else raise "Invalid instruction" end end {output, lines} end