From 334f4717ec3d24ac94d38d81e1204ee0ead0c32f Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Wed, 21 Dec 2016 14:12:12 +0100 Subject: day21.rb --- day21 | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day21.rb | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 day21 create mode 100644 day21.rb diff --git a/day21 b/day21 new file mode 100644 index 0000000..b39b884 --- /dev/null +++ b/day21 @@ -0,0 +1,100 @@ +reverse positions 1 through 6 +rotate based on position of letter a +swap position 4 with position 1 +reverse positions 1 through 5 +move position 5 to position 7 +swap position 4 with position 0 +swap position 4 with position 6 +rotate based on position of letter a +swap position 0 with position 2 +move position 5 to position 2 +move position 7 to position 1 +swap letter d with letter c +swap position 5 with position 3 +reverse positions 3 through 7 +rotate based on position of letter d +swap position 7 with position 5 +rotate based on position of letter f +swap position 4 with position 1 +swap position 3 with position 6 +reverse positions 4 through 7 +rotate based on position of letter c +move position 0 to position 5 +swap position 7 with position 4 +rotate based on position of letter f +reverse positions 1 through 3 +move position 5 to position 3 +rotate based on position of letter g +reverse positions 2 through 5 +rotate right 0 steps +rotate left 0 steps +swap letter f with letter b +rotate based on position of letter h +move position 1 to position 3 +reverse positions 3 through 6 +rotate based on position of letter h +swap position 4 with position 3 +swap letter b with letter h +swap letter a with letter h +reverse positions 1 through 6 +swap position 3 with position 6 +swap letter e with letter d +swap letter e with letter h +swap position 1 with position 5 +rotate based on position of letter a +reverse positions 4 through 5 +swap position 0 with position 4 +reverse positions 0 through 3 +move position 7 to position 2 +swap letter e with letter c +swap position 3 with position 4 +rotate left 3 steps +rotate left 7 steps +rotate based on position of letter e +reverse positions 5 through 6 +move position 1 to position 5 +move position 1 to position 2 +rotate left 1 step +move position 7 to position 6 +rotate left 0 steps +reverse positions 5 through 6 +reverse positions 3 through 7 +swap letter d with letter e +rotate right 3 steps +swap position 2 with position 1 +swap position 5 with position 7 +swap letter h with letter d +swap letter c with letter d +rotate based on position of letter d +swap letter d with letter g +reverse positions 0 through 1 +rotate right 0 steps +swap position 2 with position 3 +rotate left 4 steps +rotate left 5 steps +move position 7 to position 0 +rotate right 1 step +swap letter g with letter f +rotate based on position of letter a +rotate based on position of letter b +swap letter g with letter e +rotate right 4 steps +rotate based on position of letter h +reverse positions 3 through 5 +swap letter h with letter e +swap letter g with letter a +rotate based on position of letter c +reverse positions 0 through 4 +rotate based on position of letter e +reverse positions 4 through 7 +rotate left 4 steps +swap position 0 with position 6 +reverse positions 1 through 6 +rotate left 2 steps +swap position 5 with position 3 +swap letter b with letter d +swap letter b with letter d +rotate based on position of letter d +rotate based on position of letter c +rotate based on position of letter h +move position 4 to position 7 diff --git a/day21.rb b/day21.rb new file mode 100644 index 0000000..dea375b --- /dev/null +++ b/day21.rb @@ -0,0 +1,86 @@ +s = "abcdefgh" + +def rl(s, c) + s = s.dup + a = s.index c + s = s.slice!(-1) + s + a.times { + s = s.slice!(-1) + s + } + s = s.slice!(-1) + s if a >= 4 + s +end + +File.readlines("day21").each { |line| + case line + when /reverse positions (\d+) through (\d+)/ + a, b = $1.to_i, $2.to_i + s[a..b] = s[a..b].reverse + when /rotate based on position of letter (.)/ + s = rl(s, $1) + when /rotate right (\d+) step/ + $1.to_i.times { + s = s.slice!(-1) + s + } + when /rotate left (\d+) step/ + $1.to_i.times { + s = s + s.slice!(0) + } + when /swap position (\d+) with position (\d+)/ + a, b = $1.to_i, $2.to_i + s[a], s[b] = s[b], s[a] + when /move position (\d+) to position (\d+)/ + s.insert($2.to_i, s.slice!($1.to_i)) + when /swap letter (.) with letter (.)/ + s.tr! $1+$2, $2+$1 + else + abort "what is #{line.dump}" + end +} + +p s # gfdhebac + +def brute(s, a) + s = s.dup + (1..s.size).each { |i| + t = s.dup + i.times { + t = t.slice!(-1) + t + } + if rl(t, a) == s + return t + end + } + + abort "oops" +end + +s = "fbgdceah" +File.readlines("day21").reverse_each { |line| + case line + when /reverse positions (\d+) through (\d+)/ #idem + a, b = $1.to_i, $2.to_i + s[a..b] = s[a..b].reverse + when /rotate based on position of letter (.)/ #brute + s = brute(s, $1) + when /rotate left (\d+) step/ #swap + $1.to_i.times { + s = s.slice!(-1) + s + } + when /rotate right (\d+) step/ #swap + $1.to_i.times { + s = s + s.slice!(0) + } + when /swap position (\d+) with position (\d+)/ #idem + a, b = $1.to_i, $2.to_i + s[a], s[b] = s[b], s[a] + when /move position (\d+) to position (\d+)/ #swap + s.insert($1.to_i, s.slice!($2.to_i)) + when /swap letter (.) with letter (.)/ #idem + s.tr! $1+$2, $2+$1 + else + abort "what is #{line.dump}" + end +} + +p s -- cgit 1.4.1