Skip to content

Instantly share code, notes, and snippets.

@rickhull
Created December 22, 2014 20:11
Show Gist options
  • Save rickhull/d8573de7667b004dc81f to your computer and use it in GitHub Desktop.
Save rickhull/d8573de7667b004dc81f to your computer and use it in GitHub Desktop.
simple parse, 3 tokens at a time
# parse a string like "acorn 12 22 block 5 0 p 1 2 p 3 4 p 56 78"
# add known shapes
def self.add(board, str, val = BoardState::ALIVE)
tokens = str.split
points = []
classic = self.classic
while !tokens.empty?
shape = tokens.shift.downcase
raise "no coordinates for #{shape}" if tokens.length < 2
x = tokens.shift.to_i
y = tokens.shift.to_i
case shape.downcase
when 'p'
points << [x, y]
else
found = classic[shape] || self.discovered.fetch(shape)
board.add_points(found, x, y, val)
end
end
board.add_points(points, 0, 0, val)
end
# e.g. parse_shapes(String.split(shape_str))
# returns a list of 2-lists e.g. [[1,2], [2,3], [3,4]]
defp parse_shapes([shape, x, y | tail]) do
Enum.reduce(tail, [], fn () -> end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment