rubocopと共にそそり立つ十字の壁を歩む
背景
rubocopをいれた
bbatsov/rubocop · GitHub
やったこと
「rubyっぽくないところがあるからなおそうね」と言われたので直した
cmdから実行しているがatomから実行できるようにしたい。
なんかうまく動かない。謎。
成果
# # manage wall and walked cells # class World attr_accessor :field MAX = 100 def initialize(input) n, e, s, w = input @field = {} s = -s w = -w s.upto(n) do |y| set_wall(0, y) end w.upto(e) do |x| set_wall(x, 0) end end def wall?(x, y) key = key_of(x, y) @field[key] end def set_wall(x, y) key = key_of(x, y) @field[key] = true end private def key_of(x, y) half = MAX / 2 x += half y = half - y debug if x < 0 || x >= MAX || y < 0 || y >= MAX "#{x},#{y}" end end # # walking simulator # class Person attr_reader :point VECT = [[0, 1], [1, 0], [0, -1], [-1, 0]] DIR = :NESW def initialize(w) @wall = w @point = [1, 1] @wall.set_wall(1, 1) @front = 1 end def step v = VECT[togo_dir] nx, ny = add_point(v) @wall.set_wall(nx, ny) @point = [nx, ny] end def togo DIR[togo_dir] end private def right (@front + 1) % 4 end def left (@front + 3) % 4 end def try_step(x, y) !@wall.wall?(x, y) end def togo_dir v = VECT[right] nx, ny = add_point(v) if try_step(nx, ny) @front = right return @front end v = VECT[@front] nx, ny = add_point(v) try_step(nx, ny) ? @front : left end def add_point(v) x = @point[0] + v[0] y = @point[1] + v[1] [x, y] end end # # answerer # class Solver attr_writer :input def solve arr = @input.split(',') arr[3], day = arr[3].split(':') arr.map!(&:to_i) d = day.to_i w = World.new(arr) me = Person.new(w) d.times do me.step end me.togo end end