Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Puzzle, I used SQL to see what happend next. Expand / Collapse
Author
Message
Posted Friday, January 18, 2013 8:48 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, August 2, 2014 1:47 AM
Points: 246, Visits: 1,168
Some time ago I came across this puzzle.
I do not want to spoil the satisfaction of solving this puzzle yourself.
So I'll give the solution to the puzzle later on.


-- Puzzle.
--
-- Using SQL I wanted to see what happend with the following puzzle.
--
--
--
-- 1
-- 1 1
-- 2 1
-- 1 2 1 1
-- 1 1 1 2 2 1
-- 3 1 2 2 1 1
--
--
-- Question is wat is the next line?
-- And the line after that?
--
-- How does this progress when you continue?
-- For the last question I used SQL.


I used SQL to see what was happening further, but with primitive code, I would like to see other solutions with SQL.

For the basic solution to the puzzel, I'll post later on, with a spoiler warning in the message.

Always learning from the anwsers,
ben brugman
Post #1408954
Posted Saturday, January 19, 2013 3:59 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, August 2, 2014 1:47 AM
Points: 246, Visits: 1,168
Spoiler alert, Here is the solution to the puzzle.
Spoiler alert, Here is the solution to the puzzle.

ben.brugman (1/18/2013)

-- Puzzle.
--
-- Using SQL I wanted to see what happend with the following puzzle.
--
--
--
-- 1
-- 1 1
-- 2 1
-- 1 2 1 1
-- 1 1 1 2 2 1
-- 3 1 2 2 1 1
--
--
-- Question is wat is the next line?
-- And the line after that?
--
-- How does this progress when you continue?
-- For the last question I used SQL.


First line contains a 1, each next line describes the the line above.
So the first line contains one 1. (1 1).
The second line contains two 1's (2 1)
The third line contains one 2 and one 1 (1 2 1 1)
etc.

I used replaces to continue this trend, just to see what was happening. I'll post the script for that in a next episode, not to give away part of the 'knowledge'. I used some of the kwowledge to build the solution. I would like to see other solutions, often others can come up with a far more elegant (or more efficient or both) solutions.


Although posts not always contain have a usable solution, I always learn from the anwsers, often showing solution methods and techniques which open my mind.

ben brugman
Post #1409174
Posted Monday, January 21, 2013 1:35 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, August 2, 2014 1:47 AM
Points: 246, Visits: 1,168
Because off another thread I thougth about this puzzle.
Because the puzzle is solved mainly using a lot of REPLACE functions.

Analytical I had allready determined that there would never be a four in the string. But I still build in a safeguard in case I had made a mistake in the analys or otherwise.

Also I build in a length restriction and a number of loops restriction.
The string does not continue to grow (was supprised a little bit by that). It starts 'repeating' itself after a few loops.
ADDITIONAL, my conclusion that the string does not continue to grow was not correct. So I should have investigated further, before jumping to that conclusion. 20130124 13:47 (Sorry:)



Maybe my next 'project' is building a Turing machine based on the replace only. (The length of the bittape will be limited and not be endless as in the Turing machine).

Ben Brugmen.



declare @continue int = 1
declare @DeString varchar(4000) = '1'
declare @counter int = 0

While @continue = 1
begin

set @DeString = REPLACE(@destring, '1111','fourone')
set @DeString = REPLACE(@destring, '111','threeone')
set @DeString = REPLACE(@destring, '11','twoone')
set @DeString = REPLACE(@destring, '1','oneone')
set @DeString = REPLACE(@destring, '2222','fourtwo')
set @DeString = REPLACE(@destring, '222','threetwo')
set @DeString = REPLACE(@destring, '22','twotwo')
set @DeString = REPLACE(@destring, '2','onetwo')
set @DeString = REPLACE(@destring, '3333','fourthree')
set @DeString = REPLACE(@destring, '333','threethree')
set @DeString = REPLACE(@destring, '33','twothree')
set @DeString = REPLACE(@destring, '3','onethree')
set @DeString = REPLACE(@destring, 'three','3')
set @DeString = REPLACE(@destring, 'two','2')
set @DeString = REPLACE(@destring, 'one','1')

print convert(varchar(4),@counter)+' '+convert(varchar(20),datalength(@destring)) +' ' +
convert(varchar(200),@destring)
set @counter = @counter + 1
if @counter > 100 begin set @continue = 0 end
if @DeString like '%four%' begin set @continue = 0 end
if datalength(@destring)>2000 begin set @continue = 0 end
end


Post #1409400
Posted Monday, January 21, 2013 8:34 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 8:31 AM
Points: 204, Visits: 902
ben.brugman (1/18/2013)
Some time ago I came across this puzzle.
I do not want to spoil the satisfaction of solving this puzzle yourself.
So I'll give the solution to the puzzle later on.


I never thought of using SQL to 'solve' this puzzle. I first saw it while reading Cliff Stoll's book The Cuckoo's Egg in the late 90's and it's been a fun one to share ever since.

Sam
Post #1409581
Posted Monday, January 21, 2013 4:33 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 10:43 PM
Points: 982, Visits: 2,974
Always love a puzzle so here's my solution. I'm not sure if my grouping method will work further down the line, however it seems to work for 10 iterations. There is probably a better way of doing this.

I have made use of the delimitedSplit8k function ... http://www.sqlservercentral.com/articles/Tally+Table/72993/

declare @s varchar(4000) = '1'
declare @iterations int = 10
declare @i int = 1

print cast(@i as varchar(10)) + ': ' + @s
while @i <= @iterations
begin
select @s =
ltrim(
(
select ' ' + cast(count(*) as varchar(10)) + ' ' + item as [text()]
from (
select itemnumber, item, itemnumber - rank() over (partition by item order by itemnumber) g
from dbo.delimitedSplit8k(@s,' ')
) a
group by item, g
order by min(itemnumber)
FOR XML PATH (''), TYPE).value('text()[1]', 'varchar(8000)')
)
--select itemnumber, item, itemnumber - row_number() over (order by item) g
--from dbo.delimitedSplit8k(@s,' ')
set @i += 1
print cast(@i as varchar(10)) + ': ' + @s
end

Post #1409748
Posted Thursday, January 24, 2013 5:51 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, August 2, 2014 1:47 AM
Points: 246, Visits: 1,168
mickyT (1/21/2013)
Always love a puzzle so here's my solution. I'm not sure if my grouping method will work further down the line, however it seems to work for 10 iterations. There is probably a better way of doing this.
[/code]


Thanks for your contribution,
Using your solution I saw that the conclusion of that the string is going to loop is not correct. (And therefore my suprise was not correct).
So I have to retract that conclusion now. (Sorry).

Yes it is a very elegant puzzle. (Is my opinion).
Ben Brugman
Post #1411089
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse