{{:treffen:fir-tree-branch-with-needles-close-up.jpg?400|}}
[[https://www.freepik.com/photos/christmas|Christmas photo created by senivpetro - www.freepik.com]]
====== 004: XMAS Tree ======
| Serie | [[treffen:codegolfing|CodeGolfings]] |
| Von | [[user:nopx|nopx]] |
| Datum | 28. Dezember 2019 |
| Download | {{ :treffen:004.pdf |Aufgabenbeschreibung [pdf]}} |
| Schweirigkeit | {{:treffen:problemsolving:progress2.png?nolink&160 |}} |
CodeGolfing bis zu den [[treffen:lightningtalks20191228|Lightningtalks II]] am 28.Dez 2019.
{{ :treffen:004.pdf |Aufgabenstellung}}
===== Lösungen =====
Perl, nopx, 191B, {{ :treffen:004_gc_solution_nopx.pdf |Beschreibung}}
Eine lesbare Lösung mit diesem Ansatz gibt es ganz unten auf dieser Seite. ;)
$m=$ARGV[0]-1;sub p{($h,$s)=@_;$_=$"x($m**2+$m+2-$s-$h)."#"x$h.$"x$s;print;chop;print~~reverse.$/}for(0..$m){$w=$m*$_;p(1,$w+$_)for(0..$m);$b=($w+1)*($_!=$m);p($m+2+$w-$b,$b)}p($m)for(0..$m)
in 'schön':
$m=$ARGV[0]-1;
sub p{
($h,$s) = @_;
$_ = $"x($m**2+$m+2-$s-$h)
."#"x$h
.$"x$s;
print;
chop;
print~~reverse.$/
}
for(0..$m){
$w = $m*$_;
p( 1, $w + $_ ) for(0..$m);
$b = ($w + 1) * ($_ != $m);
p( $m+2+$w-$b, $b )
}
p( $m ) for(0..$m)
Ruby, Weber, 198B
n=ARGV[0].to_i
m=n-1
$b=n*n-n+2
a=(0..m)
def p(f , t)
s=" "*f+"#"*t+" "*($b-f-t)
puts s.reverse+s[1..-1]
end
a.each{|i|
c=(n-1)*i
a.each{|j|p(c+j,1)}
p(* i==m ?[0,c+n+1]:[c+1,n])
}
a.each{|i|p(0,m)}
Hatte Tomaten auf den Augen. Nachgereicht 2022-07-01 (190B):
n=ARGV[0].to_i;m=n-1;$b=n*n-n+2;a=(0..m);def p(f,t);s=" "*f+"#"*t+" "*($b-f-t);puts s.reverse+s[1..-1]end;a.each{|i|c=(n-1)*i;a.each{|j|p(c+j,1)};p(*i==m ?[0,c+n+1]:[c+1,n])};a.each{p(0,m)}
Python, nopx, 224B (nachgereicht)
import sys
n=int(sys.argv[1])
m=n-1
w=m*m+m+2
r=range(n)
def p(h,s=0):
l=" "*(w-h-s)+"#"*h+" "*s
print(l[:-1]+l[::-1])
for e in r:
for l in r:
p(1,m*e+l)
p(w)if e==m else p(m+1,m*e+1)
for _ in r:p(m)
Python, v0tti, 226B
import sys;n=int(sys.argv[1]);s="#";m=n+1
for x in range(m*m-1):
e=x//m;z=x%m;a=2*n*e-2*e+2*z-1;o=1;l=" "
if z==n:
o=n
if e==n-1:l=s
print((s*(2*n-3) if e==n else s if x==0 else s*o+l*(a-2*~-o)+s*o).center(2*(n*n-n+2)))
Python, Okulus, 292B
import sys;n=int(sys.argv[1]);i=1-n+n**2;p,r,x=print,range,0;s,w=" #"
for a in r(n):
for b in r(0,n+1):
if x==0:p((i)*s+w)
elif x==i:p((i*2+1)*w)
elif b==n:p((i-x)*s+w*n+(2*x-2*n+1)*s+w*n)
else:p((i-x)*s+w+(2*x-1)*s+w)
x+=1
x-=2
x+=1
for c in r(n):p(((2*(n-1)-1)*w).center(i*2+1))
C, Kai, 323B
#include
#include
#define p(a)printf(a)
int main(int c,char**g){int b,n,i,x,a,e,y=0;if(c==2){n=atoi(g[1]);for(e=0,b=n*n-(n-1);e<=n;e++,y-=2)for(a=0;a<=n;a++,p("\n"),y++)for(i=-b;x=abs(i),i<=b;i++)e==n?a!=n&x<=n-2?p("#"):p(" "):a==0&e==0&x==0|x==y|e==n-1&a==n&xy-n?p("#"):p(" ");}return 0;}
Java, Hanna, 636B
public class X{public static void main(String[]b){int n=Integer.valueOf(b[0]);int s=n*(n-1)+1;int r=s*2;char a[]=new char[r+1];for(int k=0;k
Python in schön, nopx
#!/usr/bin/python
import sys
# Read command line argument:
n = int(sys.argv[1])
# We often use n-1, so lets use a variable for that:
m = n-1
# Calculate length of left part
l_left = m**2 + m + 2
# Helper function to plot a row
def plotline(num_hashes :int ,num_spaces :int =0):
line = " "*(l_left - num_hashes - num_spaces) + "#"*num_hashes + " "*num_spaces
# Remove last column, mirror, concatenate together and plot:
print(line[:-1] + line[::-1])
# Print tree part
for etagenumber in range(n):
# You may set w = m * etagenumber as we often use that in the following.
for linenumber in range(n):
# Plot lines with on #
plotline(1, m*etagenumber + linenumber)
# Plot line with multiple #
if etagenumber == m:
# Something special for the last etage:
plotline( l_left )
else:
# For all other etages:
plotline( m+1, m*etagenumber + 1)
# Print trunk
for _ in range(n): plotline( m )