about summary refs log tree commit diff
path: root/day21.rb
diff options
context:
space:
mode:
Diffstat (limited to 'day21.rb')
-rw-r--r--day21.rb86
1 files changed, 86 insertions, 0 deletions
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