An Explanation of my "Mathematical Forest" Solution

Shlomi Fish <[email protected]>
Newsgroups gmane.comp.lang.perl.golf
Message-ID <[email protected]>
My Solution was:

#!perl -l
($},$h,$x,$s)=@ARGV;$s-=print
map$s|($t=$_-$x-1)?(grep!($s%$_|$t%$_),2..99)?$":"*":"@",1..$}for 1..$h

Here's an explanation:

($},$h,$x,$s)=@ARGV;

This reads the width, height, x and y positions from the command line
arguments. Note that instead of $w I used $} so I'll later be able to
append "for" after it without a whitespace.

I also named $s $s instead of $y, because that's a leftover from an older
code, where I used $s as an iterator.

Now:

$s-=print

This prints the stuff and decrements $s (as print returns 1 on success).
Because I used the -l flag, this automatically prints a newline at the
end.

map... ,1..$}

A mapping of the numbers to what has to be printed. This generates a list
that is later passed to print for printing.

$s|($t=$_-$x-1)?(grep!($s%$_|$t%$_),2..99)?$":"*":"@"

$t accepts the normalized x position, and s is the normalized y position.
The first test see if they are both 0. ($s|$t).
The second test checks if they are both divisable by a common denominator
(grep!....).
Then the character that has to be printed is printed according to both
tests. $" is whitespace.

for 1..$h
This entire thing is done for height lines.

Regards,

	Shlomi Fish

----------------------------------------------------------------------
Shlomi Fish        [email protected]
Home Page:         http://t2.technion.ac.il/~shlomif/

An apple a day will keep a doctor away. Two apples a day will keep two
doctors away.

	Falk Fish
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.