Created
September 25, 2013 01:26
-
-
Save kmdsbng/6693887 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| require 'pp' | |
| class Edge | |
| attr_accessor :sides | |
| def initialize(a, b) | |
| @sides = [a, b] | |
| end | |
| def inspect | |
| "Edge#{sides.inspect}" | |
| end | |
| def another_side(input) | |
| sides = @sides.dup | |
| sides.delete(input) | |
| return sides[0] if sides.size == 1 | |
| raise "Unknown side #{input.inspect} for #{sides.inspect}" | |
| end | |
| end | |
| class Cursor | |
| attr_accessor :pos, :vector, :history | |
| def initialize | |
| @pos = 'A' | |
| @history = [@pos] | |
| @vector = 'r' | |
| end | |
| def exec!(command) | |
| return unless @pos | |
| case command | |
| when 'L' | |
| turn_left! | |
| when 'R' | |
| turn_right! | |
| when /[0-9a-f]/ | |
| walk!(command.to_i(16)) | |
| end | |
| end | |
| def turn_left! | |
| table = { | |
| 't' => 'l', | |
| 'l' => 'b', | |
| 'b' => 'r', | |
| 'r' => 't', | |
| } | |
| @vector = table[@vector] | |
| end | |
| def turn_right! | |
| table = { | |
| 't' => 'r', | |
| 'l' => 't', | |
| 'b' => 'l', | |
| 'r' => 'b', | |
| } | |
| @vector = table[@vector] | |
| end | |
| def walk!(n) | |
| return if n == 0 | |
| edge = $edge_hash[@pos + @vector] | |
| unless edge | |
| @history << '?' | |
| @pos = nil | |
| return | |
| end | |
| another_side = edge.another_side(@pos + @vector) | |
| next_pos, entry_vector = another_side.split(//) | |
| @pos = next_pos | |
| @vector = reverse_side(entry_vector) | |
| @history << @pos | |
| walk!(n - 1) | |
| end | |
| def reverse_side(v) | |
| table = { | |
| 't' => 'b', | |
| 'l' => 'r', | |
| 'b' => 't', | |
| 'r' => 'l', | |
| } | |
| table[v] | |
| end | |
| def result | |
| @history.join | |
| end | |
| end | |
| $data = <<EOS | |
| ABCDEFGHIJK | |
| LMNOPQRSTUV | |
| WXYZabcdefg | |
| hij | |
| klm | |
| nop | |
| qrs | |
| tuv | |
| wxy | |
| z01 | |
| 234 | |
| 567 | |
| EOS | |
| $edges = [] | |
| $edge_hash = {} | |
| def transpose_force(matrix) | |
| max_len = matrix.map(&:size).max | |
| result = [] | |
| (0...max_len).each {|i| | |
| result << matrix.map {|l| l[i]} | |
| } | |
| result | |
| end | |
| def build_matrix | |
| matrix = [] | |
| $data.split(/\n/).each {|line| | |
| matrix << line.chomp.split(//) | |
| } | |
| matrix.each {|line| | |
| line.each_cons(2) {|left, right| | |
| $edges << Edge.new(left + 'r', right + 'l') | |
| } | |
| } | |
| transpose_force(matrix).each {|col| | |
| col.each_cons(2) {|top, bottom| | |
| if top && bottom | |
| $edges << Edge.new(top + 'b', bottom + 't') | |
| end | |
| } | |
| } | |
| $edges << Edge.new('5b', 'gb') | |
| $edges << Edge.new('6b', 'fb') | |
| $edges << Edge.new('7b', 'eb') | |
| $edges.each {|e| | |
| e.sides.each {|s| | |
| $edge_hash[s] = e | |
| } | |
| } | |
| end | |
| build_matrix | |
| def test(commands, expect) | |
| cursor = Cursor.new | |
| commands.split(//).each {|command| | |
| cursor.exec!(command) | |
| } | |
| p([cursor.result == expect, cursor.result, expect]) | |
| end | |
| def main | |
| test( "2RcL3LL22", "ABCNYjmpsvy147edcbcdef" ) | |
| test( "L3R4L5RR5R3L5", "A?" ) | |
| test( "2ReLLe", "ABCNYjmpsvy147eTITe741yvspmjYNC" ) | |
| test( "1ReRRe", "ABMXilorux036fUJUf630xuroliXMB" ) | |
| test( "ReRRe", "ALWhknqtwz25gVKVg52zwtqnkhWLA" ) | |
| test( "f", "ABCDEFGHIJK?" ) | |
| test( "Rf", "ALWhknqtwz25gVK?" ) | |
| test( "1Rf", "ABMXilorux036fUJ?" ) | |
| test( "2Rf", "ABCNYjmpsvy147eTI?" ) | |
| test( "aR1RaL1LaR1R2L1L2", "ABCDEFGHIJKVUTSRQPONMLWXYZabcdefg567432" ) | |
| test( "2R1R2L1L2R1R2L1L2R1R2L1L2R1R2L1L2", "ABCNMLWXYjihklmponqrsvutwxy" ) | |
| test( "2R4R2L4L2R4R2L4L2R4R2L4L2", "ABCNYjmlknqtwxy147efgVK?" ) | |
| test( "R1L2R4R2L4L2R4R2L4L2R4R2L4L2", "ALMNYjmponqtwz0147eTUVK?" ) | |
| test( "R2L2R4R2L4L2R4R2L4L2R4R2L4L2", "ALWXYjmpsrqtwz2347eTIJK?" ) | |
| test( "R3L2R4R2L4L2R4R2L4L2R4R2L4L2", "ALWhijmpsvutwz2567eTI?" ) | |
| test( "R5L2L5L1LaR1L4L5", "ALWhknopmjYNCBMXilorux0325gVKJIHGF" ) | |
| test( "1R2L4L2R4R2L4L2R4", "ABMXYZabQFGHIJUfg?" ) | |
| test( "2R2L4L2R4R2L4L2R4", "ABCNYZabcRGHIJKVg?" ) | |
| test( "3R2L4L2R4R2L4L2R4", "ABCDOZabcdSHIJK?" ) | |
| test( "4R2L4L2R4R2L4L2R4", "ABCDEPabcdeTIJK?" ) | |
| test( "5R2L4L2R4R2L4L2R4", "ABCDEFQbcdefUJK?" ) | |
| test( "LLL1RRR1LLL1RRR2R1", "ALMXYZ?" ) | |
| test( "R3RRR3", "ALWhij?" ) | |
| test( "1LLL4RRR1LR1RL1", "ABMXilm?" ) | |
| test( "R2L1R2L1R3R4", "ALWXilmpsvut?" ) | |
| test( "7R4f47LLLc6R9L", "ABCDEFGHSd?" ) | |
| test( "5RR868L8448LL4R6", "ABCDEFEDCBA?" ) | |
| test( "42Rd1RLLa7L5", "ABCDEFGRc?" ) | |
| test( "RRLL6RLR1L5d12LaLRRL529L", "ABCDEFGRSTUV?" ) | |
| test( "RLR7L6LL1LRRRcRL52R", "ALWhknqtuv?" ) | |
| test( "1RLR8RLR1R437L99636R", "ABMXiloruxwtqnkhWLA?" ) | |
| test( "LLL2L3La9Le5LRR", "ALWXYZOD?" ) | |
| test( "R1LcRR491", "ALMNOPQRSTUV?" ) | |
| test( "R8L1R1R512L8RLLReRf", "ALWhknqtwx0z?" ) | |
| test( "1RcL8f1L29a5", "ABMXilorux036fedcbaZYXW?" ) | |
| test( "R822LeL46LL39LL", "ALWhknqtwz25gfedcbaZYXW?" ) | |
| test( "9R3L5LRRLb5R3L7cLLLR4L", "ABCDEFGHIJUf65?" ) | |
| test( "7LLRRR2R3R69Lf76eR2L", "ABCDEFGHSdcbaPE?" ) | |
| test( "8RRRLL3Le", "ABCDEFGHITe765?" ) | |
| test( "8R5RLL6LbL4LL5bL", "ABCDEFGHITe7410z?" ) | |
| test( "6LR2R1LR5LRLRL484L63", "ABCDEFGHITe741yxw?" ) | |
| end | |
| case $0 | |
| when __FILE__ | |
| main | |
| when /spec[^\/]*$/ | |
| # {spec of the implementation} | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment