Gistflow Xmas Quiz! by releu

What will we do until Xmas? Of course we will prepare for it!

Let's print Xmas tree by your favourite language!

All winners will get a present ;)

Here is the tree:

        ()
        /\
       / *\
      / *  \
     / * * *\
    /*   *  *\
   /   *  *  *\
  /  *   *  *  \
 /  *   *     * \
        []

Decorations * should be placed randomly, but they can't be near each other for each line and their count should be equal 3 or less if they doesn't fit.

Shorter solution for each language wins.

#quiz #xmas #ruby #js #haskell #python :)

Similar posts

Comments

Maysora commented 5 months ago

56cc2fd07abe1db82fcbb1af33ffd6c4?size=52

not really a short solution :P http://jsfiddle.net/maysora/QUvvK/

edit: oops.. i guess i misunderstood the question, i should have evenly divide the inside by 3 instead of just separate decoration by 1 space :/

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Cool :+1:

Maysora commented 5 months ago

56cc2fd07abe1db82fcbb1af33ffd6c4?size=52

@killthekitten the decoration maxed at 2 per line? i can't read coffeescript though so i don't know where it's wrong..

makaroni4 commented 5 months ago

E302c3320cd14b02cbe237b479d7f884?size=52

Here is #ruby solution, it puts toys randomly with frequency you pass as an argument.

def draw_fir_tree size = 10, frequency = 45
  elka = (0..size).map do |i|
    Array.new(i + 1) do |j|
      rand(100) > frequency ? ' ' : '*'
    end
  end

  elka.each do |row|
    puts row.join(' ').prepend('/').concat('\\').center(size * 2 + 3)
  end  
end

draw_fir_tree

produces:

          / \          
         /* *\         
        /     \        
       /*   *  \       
      /         \      
     /  * *   * *\     
    /* * * *      \    
   /  * *   * * * *\   
  /        *   * *  \  
 /    *             *\ 
/    * *       *     *\

makaroni4 commented 5 months ago

E302c3320cd14b02cbe237b479d7f884?size=52

Also it would be nice to animate christmas tree, but for mac terminal system 'clear' doesn't work, I made smth like:

require 'appscript'

class Terminal
  include Appscript

  def hacker_clear
    app('System Events').application_processes['Terminal.app'].keystroke('k', :using => :command_down)
  end
end

but with big fps (delay 0.1 sec for example) it locks keyboard :smile:

skammer commented 5 months ago

856bf8b84a440d6bcfb6f892689d0f4b?size=52

This almost works as requested, but not quite :/

To print a tree of arbitrary height:

def print_tree th = 8
  c = ->(s){ " " * th + s }
  puts c["()"]
  th.times do |i|
    star = ->{ Array.new(2){" " * (i*2-6>0 ? rand(i*2-6)/2 : rand(2))}.join "* " }
    stars = Array.new(3){ star.call }.join
    row = stars.chars.first(i*2).join.center(i*2)
    left_side = " "*(th-i) + "/"
    right_side = "\\"
    puts left_side + row + right_side
  end
  puts c["[]"]
end

100.times do
  system 'clear'
  print_tree 10
  sleep 0.1
end

Or as a oneliner (220 chars):

puts " "*8+"()"+"\n"+Array.new(8){|i|Array.new(3){Array.new(2){" "*(i*2-6>0 ? rand(i*2-6)/2 : rand(2))}.join "* "}.join.chars.first(i*2).join.center(i*2).prepend(" "*(8-i)+?/).concat(?\\)}.join("\n")+"\n"+" "*8+"[]"+"\n"

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

Here is mine:

t = ['()', '/\\']
1.upto(8).map do |n|
  if (l = n * 2) < 6
    t<<"/#{(Array.new(l - 1, " ") << '*').shuffle.join}\\"
  else
    while a = (Array.new(l - 3, " ") + Array.new(3, '*')).shuffle.join
      a =~ /\*\*/ ? next : (t<<"/#{a}\\"; break)
    end
  end
end
(t<<'[]').each { |r| puts r.center(18) }

and compact (250 chars):

t = ['()', '/\\']
1.upto(8).map{|n|(l = n * 2) < 6 ? t<<"/#{(Array.new(l-1, " ")<<'*').shuffle.join}\\" : while a=(Array.new(l - 3, " ")+Array.new(3, '*')).shuffle.join;a=~/\*\*/ ? next : (t<<"/#{a}\\";break) end}
(t<<'[]').each{|r|puts r.center(18)}
        ()        
        /\        
       /* \       
      /  * \      
     /* * * \     
    /*    * *\    
   /  *    * *\   
  / *    *    *\  
 /     *   *   *\ 
/*           * * \

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

And here is animated tree:

WIDTH = `stty size`[/\d\s(\d+)/, 1].to_i

def tree
t = ['()', '/\\']
1.upto(8).map{|n|(l = n * 2) < 6 ? t<<"/#{(Array.new(l-1, " ")<<'*').shuffle.join}\\" : while a=(Array.new(l - 3, " ")+Array.new(3, '*')).shuffle.join;a=~/\*\*/ ? next : (t<<"/#{a}\\";break) end}
(t<<'[]').map{|r|r.center(WIDTH)}.join
end

loop do
  t = tree
  print t
  t.size.times { print "\b" }
  sleep 0.4
end

drakmail commented 5 months ago

558e7fdf5c8c73f03f5bc645d16afa21?size=52

Some #python code for diversity.

Formally, all rules met, but decorations placing in clusters (random clusters) :)

import random
def draw_level(n):
  line, rs = ''.ljust(9-n)+'/', 0
  if n > 4: rs = random.randint(0, n-4)*2 #random shift :)
  for i in range(1,n*2-1):
    if i in [2+rs,4+rs,6+rs]:
      line += "*"
    else:
      line += " "
  print line + '\\'
print "()".center(18,' ')
for i in range(1,10): draw_level(i)
print "[]".center(18)
        ()        
        /\
       / *\
      / * *\
     / * * *\
    / * * *  \
   / * * *    \
  /       * * *\
 / * * *        \
/ * * *          \
        []

Link to online repl with code

pashh commented 5 months ago

65c917eb513b191da67180ab8048e943?size=52

Hello everyone

it just a refactoring makaroni4 #ruby solution. this code is not perfect and I try to fix it. But it most shorter (154 chars):

puts [" "*11+"()"]+(0..10).map{|i| Array.new(i*2){|j| j=['*', " "].shuffle[1..2].join }.push('/').reverse.push('\\').join("").center(25)}.push(" "*11+"[]")

makaroni4 commented 5 months ago

E302c3320cd14b02cbe237b479d7f884?size=52

@pashh what do you mean 'is not perfect' and what have you fixed in it?

Man, negativity is for the lazy and to say that someone's code is not perfect you at least should say why.

pashh commented 5 months ago

65c917eb513b191da67180ab8048e943?size=52

no, I mean my version is not perfect, because the "**" is exist near each other for each line.

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

By the way @makaroni4 solution is not correct in terms of the rules.

makaroni4 commented 5 months ago

E302c3320cd14b02cbe237b479d7f884?size=52

@pashh OK, so it was me who misunderstood the message :blush: I am sorry for this.

drakmail commented 5 months ago

558e7fdf5c8c73f03f5bc645d16afa21?size=52

Could be ruby code obfuscated? Yes, it could! :smile:

Perl-style one-line ruby xmas tree:

W=20;def l q=0;if q==0;(puts "/\\".center W)||(l'')else;return if q.size==W-4;f=" #{q} ";e=f.gsub('*','').size;r,s,i,n,t=e-3,[],-1,0,'';if r>0;while i do i,_=f.index(' ',i+1),s<<i end;q=s[0..-2].sort{|a,b|rand(2)<=>rand(2)}[0..r-1];q.each{|i|f[i]='X'};end;if e>3;f.each_char{|c|c==' '&&n<3&&t[-1]!='*'?(t+='*';n+=1):t+=' '}&&f=t.gsub('X',' ')end;(puts "/#{f}\\".center(W))||l(f);end;end;(puts"()".center(W))||l||puts("[]".center(W))

output

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

OMG. Your code can be used for interview. "Please tell me what this code is doing" :D

pashh commented 5 months ago

65c917eb513b191da67180ab8048e943?size=52

the last revision in concise, functional mood(170 characters):

puts [" "*10+"()"]+(0..10).map{|i| (Array.new(2*i,' ')+['*','*','*']).shuffle[1..2*i].push('/').reverse.push('\\').join.gsub(/\*\*/ ," "*2).center(22)}.push(" "*10+"[]")

all conditionals was done

pashh commented 5 months ago

65c917eb513b191da67180ab8048e943?size=52

and this latest tiny fix (167 characters)

puts [" "*10+"()"]+(0..10).map{|i| (Array.new(2*i,' ')+['*','*','*']).shuffle[1..2*i].join.gsub(/\*\*/ ," "*2).insert(0,'/').concat('\\').center(22)}.push(" "*10+"[]")

pashh commented 5 months ago

65c917eb513b191da67180ab8048e943?size=52

and of course Xmas tree for hipsters:

puts [" "*10+"()"]+(0..10).map{|i| (Array.new(2*i,' ')+['*','*','*']).shuffle[1..2*i].join.gsub(/\*\*/ ," "*2).insert(0,'\\').concat('/').center(22)}.push(" "*10+"[]")

          ()
          \/          
         \  /         
        \  * /        
       \   * */       
      \   *  * /      
     \      *   /     
    \  *         /    
   \   *   *      /   
  \ *              /  
 \  *        *      / 
\    *    *   *      /
          []

powder96 commented 5 months ago

934c8d38555e09ad1fbc224b4948403a?size=52

PHP, 245 bytes: https://gist.github.com/4413869 Source code: https://gist.github.com/4413821

<?php p('()');p('/\\');for($i=1;$i&lt;8;++$i){$l=array_fill(0,min($i,3),'* ');if($i>3)$l=array_merge($l,array_fill(0,$i-3,'  '));shuffle($l);p('/'.implode('',$l).'\\');}p('[]');function p($t){echo  str_pad($t,16,' ',STR_PAD_BOTH)."\n";}?>
       ()
       /\
      /* \
     /* * \
    /* * * \
   /* *   * \
  /* *     * \
 /*   * *     \
/    * *     * \
       []

atzkey commented 5 months ago

504f5a0d635a63b0a2fe50e6567ae9eb?size=52

Ruby, 111 bytes: puts *['()',*0.step(15,2).map{|i|'/'+('* '*3+' '*i)[0,i].scan(/../).shuffle*''+'\\'},'[]'].map{|t|t.center(16)}

releu commented 5 months ago

757fb0d5ec7560b6f25f5bd98eadc020?size=52

:+1:

pashh commented 5 months ago

65c917eb513b191da67180ab8048e943?size=52

impressive, I'm give up :)

Maysora
agentcooper
atzkey
drakmail
killthekitten
makaroni4
pashh
powder96
releu
skammer