From 999f1e2f19d0d57d6cbe1136d1db56a0b2fd6427 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 21 Dec 2016 00:14:03 -0500 Subject: [PATCH] [2016][ruby][21.0] --- 2016/input/day_21.txt | 100 ++++++++++++++++++++++++++++++++++++++++++ 2016/ruby/day_21.rb | 37 ++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 2016/input/day_21.txt create mode 100644 2016/ruby/day_21.rb diff --git a/2016/input/day_21.txt b/2016/input/day_21.txt new file mode 100644 index 0000000..4870930 --- /dev/null +++ b/2016/input/day_21.txt @@ -0,0 +1,100 @@ +move position 0 to position 3 +rotate right 0 steps +rotate right 1 step +move position 1 to position 5 +swap letter h with letter b +reverse positions 1 through 3 +swap letter a with letter g +swap letter b with letter h +rotate based on position of letter c +swap letter d with letter c +rotate based on position of letter c +swap position 6 with position 5 +rotate right 7 steps +swap letter b with letter h +move position 4 to position 3 +swap position 1 with position 0 +swap position 7 with position 5 +move position 7 to position 1 +swap letter c with letter a +move position 7 to position 5 +rotate right 4 steps +swap position 0 with position 5 +move position 3 to position 1 +swap letter c with letter h +rotate based on position of letter d +reverse positions 0 through 2 +rotate based on position of letter g +move position 6 to position 7 +move position 2 to position 5 +swap position 1 with position 0 +swap letter f with letter c +rotate right 1 step +reverse positions 2 through 4 +rotate left 1 step +rotate based on position of letter h +rotate right 1 step +rotate right 5 steps +swap position 6 with position 3 +move position 0 to position 5 +swap letter g with letter f +reverse positions 2 through 7 +reverse positions 4 through 6 +swap position 4 with position 1 +move position 2 to position 1 +move position 3 to position 1 +swap letter b with letter a +rotate based on position of letter b +reverse positions 3 through 5 +move position 0 to position 2 +rotate based on position of letter b +reverse positions 4 through 5 +rotate based on position of letter g +reverse positions 0 through 5 +swap letter h with letter c +reverse positions 2 through 5 +swap position 7 with position 5 +swap letter g with letter d +swap letter d with letter e +move position 1 to position 2 +move position 3 to position 2 +swap letter d with letter g +swap position 3 with position 7 +swap letter b with letter f +rotate right 3 steps +move position 5 to position 3 +move position 1 to position 2 +rotate based on position of letter b +rotate based on position of letter c +reverse positions 2 through 3 +move position 2 to position 3 +rotate right 1 step +move position 7 to position 0 +rotate right 3 steps +move position 6 to position 3 +rotate based on position of letter e +swap letter c with letter b +swap letter f with letter d +swap position 2 with position 5 +swap letter f with letter g +rotate based on position of letter a +reverse positions 3 through 4 +rotate left 7 steps +rotate left 6 steps +swap letter g with letter b +reverse positions 3 through 6 +rotate right 6 steps +rotate based on position of letter c +rotate based on position of letter b +rotate left 1 step +reverse positions 3 through 7 +swap letter f with letter g +swap position 4 with position 1 +rotate based on position of letter d +move position 0 to position 4 +swap position 7 with position 6 +rotate right 6 steps +rotate based on position of letter e +move position 7 to position 3 +rotate right 3 steps +swap position 1 with position 2 \ No newline at end of file diff --git a/2016/ruby/day_21.rb b/2016/ruby/day_21.rb new file mode 100644 index 0000000..8b9c137 --- /dev/null +++ b/2016/ruby/day_21.rb @@ -0,0 +1,37 @@ +input = 'abcdefgh' +ARGF.read.split("\n").each do |instruction| + case instruction + when /swap position (\d+) with position (\d+)/ + x = $1.to_i + y = $2.to_i + input[x], input[y] = input[y], input[x] + when /swap letter (\w+) with letter (\w+)/ + x = $1 + y = $2 + input.tr!("#{x}#{y}", "#{y}#{x}") + when /rotate (left|right) (\d+) steps?/ + steps = $2.to_i + steps = -steps if $1 == 'right' + input = input.chars.rotate(steps).join + when /rotate based on position of letter (\w+)/ + index = input.index($1) + index += 1 if index >= 4 + index += 1 + index = -index + input = input.chars.rotate(index).join + when /reverse positions (\d+) through (\d+)/ + x = $1.to_i + y = $2.to_i + input[x..y] = input[x..y].reverse + when /move position (\d+) to position (\d+)/ + x = $1.to_i + x = input[x] + input[x] = '' + + y = $2.to_i + input.insert(y, x) + else + raise "invalid instruction: '#{instruction}'" + end +end +puts input