[2016][ruby][11.0] make a Floor object

profile
Alpha Chen 8 years ago
parent eeb4f8366c
commit b7fb46656a

@ -1,3 +1,5 @@
require 'delegate'
class State class State
attr_reader :floors, :elevator attr_reader :floors, :elevator
@ -5,27 +7,48 @@ class State
@floors = input.lines.reverse.map.with_index {|line, index| @floors = input.lines.reverse.map.with_index {|line, index|
_, e, *items = line.split(/\s+/) _, e, *items = line.split(/\s+/)
@elevator = index if e == ?E @elevator = index if e == ?E
items.reject {|item| item == ?. } Floor.new(items.reject {|item| item == ?. })
} }
end end
end end
class Floor < SimpleDelegator
attr_reader :source
def initialize(items)
@source = super(items)
end
def microchips
source.select {|item| item.end_with?(?M) }.map {|item| item.chomp(?M) }
end
def generators
source.select {|item| item.end_with?(?G) }.map {|item| item.chomp(?G) }
end
end
require 'minitest/autorun' require 'minitest/autorun'
class TestDay11 < Minitest::Test class TestDay11 < Minitest::Test
def test_initialize INPUT = <<-INPUT
input = <<-INPUT
F4 . . . . . F4 . . . . .
F3 . . . LG . F3 . . . LG .
F2 . HG . . . F2 . HG . . .
F1 E . HM . LM F1 E . HM . LM
INPUT INPUT
state = State.new(input) def setup
assert_equal 4, state.floors.size @state = State.new(INPUT)
assert_equal 0, state.elevator end
def test_initialize
assert_equal 4, @state.floors.size
assert_equal 0, @state.elevator
floor = state.floors[0] floor = @state.floors[0]
assert_equal 2, floor.size assert_equal 2, floor.size
assert_equal %w[ H L ], floor.microchips
assert_empty floor.generators
end end
end end

Loading…
Cancel
Save