SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Puzzle, I used SQL to see what happend next.


Puzzle, I used SQL to see what happend next.

Author
Message
ben.brugman
ben.brugman
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1048 Visits: 2391
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
ben.brugman
ben.brugman
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1048 Visits: 2391
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

ben.brugman
ben.brugman
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1048 Visits: 2391
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::cryingSmile



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



samalex
samalex
SSC-Addicted
SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)SSC-Addicted (421 reputation)

Group: General Forum Members
Points: 421 Visits: 1061
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.


:-D 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
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1574 Visits: 3317
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


ben.brugman
ben.brugman
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1048 Visits: 2391
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search