January 18, 2013 at 8:48 am
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
January 19, 2013 at 3:59 am
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
January 21, 2013 at 1:35 am
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::crying:)
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
January 21, 2013 at 8:34 am
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
January 21, 2013 at 4:33 pm
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-2 varchar(4000) = '1'
declare @iterations int = 10
declare @i int = 1
print cast(@i as varchar(10)) + ': ' + @s-2
while @i <= @iterations
begin
select @s-2 =
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-2
end
January 24, 2013 at 5:51 am
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
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply