Created
December 6, 2015 04:51
-
-
Save AndrewBurian/91e08ca2d015b7d91198 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
class Simulator | |
def initialize(seating_arrangement) | |
@curState = seating_arrangement | |
@nextState = @curState | |
end | |
def verdict | |
opinion = { :hard => 0, :soft => 0, :none => 0 } | |
@curState.each do |col| | |
col.each do |row| | |
opinion[row] += 1 | |
end | |
end | |
return :hard if opinion[:hard] > opinion[:soft] | |
return :soft if opinion[:soft] > opinion[:hard] | |
return :push | |
end | |
def state | |
return @curState | |
end | |
def next | |
@nextState = Marshal.load(Marshal.dump(@curState)) | |
(0..(@curState.length - 1)).each do |i| | |
(0..(@curState[i].length - 1)).each do |j| | |
opinion(i,j) | |
end | |
end | |
@curState = @nextState.clone | |
end | |
def opinion(i, j) | |
neighbours = {:hard => 0, :soft => 0, :none => 0} | |
(-1..1).each do |iN| | |
(-1..1).each do |jN| | |
iPos = i + iN | |
jPos = j + jN | |
next if jN == 0 and iN == 0 | |
next if not iPos.between?(0, @curState.length - 1) | |
next if not jPos.between?(0, @curState[iPos].length - 1) | |
neighbours[@curState[iPos][jPos]] += 1 | |
end | |
end | |
# fewer than 2 opinionated neighbours, lose opinion | |
if neighbours[:hard] + neighbours[:soft] < 2 then | |
@nextState[i][j] = :none | |
return | |
end | |
# more than 3 opinionated, lose opinion | |
if neighbours[:hard] + neighbours[:soft] > 3 then | |
@nextState[i][j] = :none | |
return | |
end | |
# 3 with >= 2 hard, become hard if no opinion | |
if neighbours[:hard] + neighbours[:soft] == 3 and neighbours[:hard] >= 2 and @curState[i][j] == :none then | |
@nextState[i][j] = :hard | |
return | |
end | |
# 3 with >= 2 soft, become soft if no opinion | |
if neighbours[:hard] + neighbours[:soft] == 3 and neighbours[:soft] >= 2 and @curState[i][j] == :none then | |
@nextState[i][j] = :soft | |
return | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment