Skip to content

Instantly share code, notes, and snippets.

@marioaquino
Created September 25, 2010 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioaquino/596724 to your computer and use it in GitHub Desktop.
Save marioaquino/596724 to your computer and use it in GitHub Desktop.
/* Implementation by Jesse Phelps & Tomasz Mozolewski */
DECLARE @maxGen int
set @maxgen = (select MAX(generation) from Cells)
--insert into cells
;WITH cte AS
(
SELECT -1 x
UNION ALL
SELECT x + 1
FROM cte
WHERE x < 1
)
insert into Cells
select newGen, g.xcoord, g.ycoord
from (
select @maxgen + 1 as newGen, xcoord - coordPairs.x as xcoord, ycoord - coordPairs.y as ycoord
from cells, (SELECT cte.x, cte2.x as y
FROM cte, cte as cte2
where not (cte.x = 0 and cte2.x = 0)
) coordPairs
where generation = @maxGen
) g left outer join Cells c
on c.xcoord = g.xcoord and c.ycoord = g.ycoord and g.newGen -1 = c.generation
group by g.xcoord, g.ycoord, newGen, c.xcoord
having (COUNT(*) = 3) or (COUNT(*) = 2 and c.xcoord is not null)
select generation, xcoord, ycoord from cells
--DELETE FROM Cells
--where generation > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment