input = File.read("input-16.txt").strip.split(",") #input = "s1,x3/4,pe/b".split(",") matcher = /([s,p,x])(?:(\w*\/\w*)|(\w+))/ line = ('a'..'p').to_a #line = ('a'..'e').to_a input.each do |s| match_data = (s.match matcher).to_a match_data[1] = match_data[1].to_sym case match_data[1] when :s line.rotate! -(match_data[3].to_i) when :x positions = match_data[2].split "/" line[positions[0].to_i], line[positions[1].to_i] = line[positions[1].to_i], line[positions[0].to_i] when :p buds = match_data[2].split "/" index_0, index_1 = line.find_index(buds[0]), line.find_index(buds[1]) line[index_0], line[index_1] = line[index_1], line[index_0] end end p line.join