Christmas photo created by senivpetro - www.freepik.com

004: XMAS Tree

Serie	CodeGolfings
Von	nopx
Datum	28. Dezember 2019
Download	Aufgabenbeschreibung [pdf]
Schweirigkeit

CodeGolfing bis zu den Lightningtalks II am 28.Dez 2019.

Aufgabenstellung

Lösungen

Perl, nopx, 191B, 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, 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<stdio.h> #include<stdlib.h> #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&x<y|a==n&x<y&x>y-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<n;k++){for(int j=0;j<n;j++){for(int i=0;i<r+1;i++)a[i]=' ';int t=k*n+j-k;a[s-t]='#';a[s+t]='#';p(s,a);l();}if(k==n-1){for(int i=0;i<r+1;i++)a[i]='#';p(s,a);}else{int x=0;for(int i=0;i<r+1;i++){while(x<n){a[s-n-(k*n-k)+x]='#';a[s+n+(k*n-k)-x]='#';x++;}}p(s,a);l();}}for(int i=0;i<r+1;i++)a[i]=' ';int x=0;while(x<n){l();for(int i=s-(n-2);i<=s+(n-2);i++)a[i]='#';p(s,a);x++;}}static void p(int s,char a[]){for(int i=0;i<(s*2)+1;i++){System.out.print(a[i]);}}static void l(){System.out.println();}}

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,num_spaces): 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( m + 2 + m * etagenumber, 0) else: # For all other etages: plotline( m + 1, m * etagenumber + 1) # Print trunk for _ in range(n): plotline( m, 0)