Created
September 2, 2009 23:05
-
-
Save sr3d/180031 to your computer and use it in GitHub Desktop.
This file contains 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
# http://code.google.com/codejam/contest/dashboard?c=32003#s=p1 | |
#2 | |
#WRWWLWWLWWLWLWRRWRWWWRWWRWLW WWRRWLWLWWLWWLWWRWWRWWLW | |
#WW WW | |
require 'ruby-debug' | |
class Cell | |
attr_accessor :x, :y, :walls | |
def initialize( x, y, walls ) | |
@x, @y, @walls = x, y, walls | |
end | |
end | |
def bin_to_direction ( b ) | |
south = 0b0010 | |
north = 0b0001 | |
west = 0b0100 | |
east = 0b1000 | |
case b | |
when south then 'SOUTH' | |
when north then 'NORTH' | |
when west then 'WEST' | |
when east then 'EAST' | |
end | |
end | |
def extract_maze( cells ) | |
min = 99999999999 | |
max = -min | |
min_row = min | |
max_row = -min | |
min_col = min | |
max_col = -min | |
cells.each do |key, cell| | |
min_row = cell.x if cell.x < min_row | |
max_row = cell.x if cell.x > max_row | |
min_col = cell.y if cell.y < min_col | |
max_col = cell.y if cell.y > max_col | |
end | |
maze = [] | |
cells.each do |key, cell| | |
#cell = cells[ key ] | |
cell.x = cell.x - min_row | |
cell.y = cell.y - min_col | |
if( !maze[ cell.y ] ) | |
maze[ cell.y ] = [] | |
end | |
puts cell.inspect | |
maze[ cell.y ][ cell.x ] = cell | |
end | |
return maze | |
end | |
def format_results( maze ) | |
for i in maze[0].length do # |i| # row | |
cell = '' | |
for j in maze.length do # |j| # col | |
cell << maze[ i ][ j ].walls.to_s( base = 16 ) | |
end | |
puts cell | |
end | |
end | |
def process( input ) | |
az, za = input.split( ' ' ) | |
az = az.split('') | |
za = za.split('') | |
maze = [] | |
maze[0] = [ 0b0000 ] # entrance | |
maze_za = [] | |
maze_za[0] = [ 0b0010 ] | |
cells = {} | |
# Mask for the direction | |
south = 0b0010 # 2 | |
north = 0b0001 # 1 | |
west = 0b0100 # | |
east = 0b1000 | |
cell = nil | |
row = 0 | |
col = 0 | |
heading = south | |
cells[ "0_-1" ] = Cell.new( row, -1, north ) | |
az.each_with_index do |c, index| | |
puts "row: " + row.to_s + " col " + col.to_s + " Heading: " + bin_to_direction( heading ) | |
next if index == az.length - 1 | |
case c | |
when 'W' then | |
case heading | |
when south then | |
col = col - 1 | |
when north then | |
col = col + 1 | |
when west then | |
row = row -1 | |
when east then | |
row = row + 1 | |
end | |
next_cell_heading = case heading | |
when south then north | |
when north then south | |
when west then east | |
when east then west | |
end | |
unless cells[ row.to_s + "_" + col.to_s ] # or index == az.length - 1 | |
cell = cells[ row.to_s + '_' + col.to_s ] = Cell.new( row, col, next_cell_heading ) | |
else | |
cell = cells[ row.to_s + "_" + col.to_s ] | |
cell.walls = cell.walls | next_cell_heading | |
end | |
#puts cell | |
when 'R' then | |
case heading | |
when south then | |
heading = west | |
when north then | |
heading = east | |
when west then | |
heading = north | |
when east then | |
heading = south | |
end | |
when 'L' then | |
case heading | |
when south then | |
heading = east | |
when north then | |
heading = west | |
when west then | |
heading = south | |
when east then | |
heading = north | |
end | |
end | |
# peak here | |
if( az[ index + 1 ] and az[ index + 1 ] == 'W' ) | |
cell.walls = cell.walls | heading | |
end | |
end | |
"Done" | |
#puts extract_maze( cells ).inspect | |
format_results( extract_maze( cells ) ) | |
end | |
#puts | |
#process( 'WRWWLWWLWWLWLWRRWRWWWRWWRWLW WWRRWLWLWWLWWLWWRWWRWWLW' ) | |
process( 'WW WW' ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment