-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elixir Christmas Tree Puzzle #3
Comments
Assuming you have import String;a=duplicate(" ",h-1);IO.puts"#{a}*";for i<-1..h do IO.puts"#{duplicate(" ",h-i)}#{duplicate("0",i*2-1)}"end;IO.puts"#{a}H" I believe 136 characters. This goes down to 134 when replacing h with 9 (h-1 then cancels down to 8). |
Great @notexactlyawe you don't need the import String;a=duplicate " ",8;IO.puts"#{a}*";for i<-1..h do IO.puts"#{duplicate " ",9-i}#{duplicate "0",i*2-1}"end;IO.puts"#{a}H" So you're at 131 chars! |
Cool, thanks :) |
Got it down to 101 chars using
I think there might be a way to get a little lower with @vanstee on twitter as well. Edit: D'oh not sure why I included the spaces on the right side. |
My "own" solution, 104 characters (the linebreak is significant and counts as a single character): d=&String.duplicate/2;s=d.(" ",8);IO.puts [s,"*
",(for i<-0..8,do: [d.(" ",8-i),d.("0",i*2+1),10]),s,?H]
If I borrow @vanstee's d=&List.duplicate/2;s=d.(32,8);IO.puts [s,"*
",(for i<-0..8,do: [d.(32,8-i),d.(?0,i*2+1),10]),s,?H]
All these solutions make use of Elixir's wonderful chardata concept, where
|
My feeble attempts started out something like this (before I realized I was printing 17 lines instead of 17 zeroes...): *
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
H which evolved into this (I think I've got the beginnings of a nice console game though) *
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
H and then into the most lopsided tree ever: *
0
00
000
0000
00000
000000
0000000
00000000
000000000
0000000000
00000000000
000000000000
0000000000000
00000000000000
000000000000000
0000000000000000
H It was cracking me up quite a bit though. 😂 |
And the hilarity just continues! *
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
H *
00
0
0 0
0
0 0
0
0 0
0
0 0
0
0 0 |
Finally! My humble solution of way too many chars (I have yet to look at the other solutions mind you): for x <- 0..10, y = (x < 1 && "*" || x > 9 && "H" || "0"), z = 1 < x && x < 10 && String.ljust("0", x - 1, ?0)||"", do: IO.puts(String.rjust(z, 8) <> y <> z) |
...and a solution that's 2 chars shorter than my previous one! (well aware that I can shave whitespace and similar from my previous solution) But this one is so "stupid" I have to post it. :) IO.puts " *
0
000
00000
0000000
000000000
00000000000
0000000000000
000000000000000
00000000000000000
H" |
My original solution with trimmings (pun intended), 117 chars (which I'm pretty happy with): for x<-0..10,y=x<1&&"*"||x>9&&"H"||"0",z=1<x&&x<10&& String.rjust("0",x-1,?0)||"",do: IO.puts String.rjust(z,8)<>y<>z edit: @stoftis on twitter |
And now reading through the comments I realized I probably duplicated @vanstee 's solution he mentioned but never posted. :) |
That's great... although I think the tree needs to be a bit more festive: d=&List.duplicate/2;s=d.(32,8);IO.puts [s,"* ",(for i<-0..8,do: [d.(32,8-i),d.(Enum.random(~W(> = < ~ ~)),i*2+1),10]),s,?H] |
Oh I agree! d=&List.duplicate/2;s=d.(32,8);IO.puts [s,IO.ANSI.yellow <> "* ",(for i<-0..8,do: [d.(32,8-i),d.(Enum.random([IO.ANSI.green, IO.ANSI.red]) <> Enum.random(~W(> = < ~ ~)),i*2+1),10]),s,?H] 🎅 🎄 ⛄ 🎁 |
Or this one is even better... d=&List.duplicate/2;s=d.(32,8);IO.puts [s,"* ",(for i<-0..8,do: [d.(32,8-i),(for x<-1..i*2+1,into: [],do: Enum.random ~W(~ x o ~ ~)),10]),s,?H] Which gives: *
~ox
~o~~~
~~~~~~o
~~o~~~~~~
~x~~~x~x~x~
~x~~x~~oxo~o~
~~~ox~o~o~~~~ox
~~~ooox~~o~o~~~xx
H |
Hehe, I've also been meaning to make a festive one. This one assumes your terminal allows ANSI blinking. Did some things to keep it short but I haven't gone full golf on it: import IO.ANSI;r=reset;b=blink_slow;g=green;y=yellow;d=&List.duplicate/2;s=d.(32,8);t=Stream.cycle([[g,?/],[red,?o],[g,?\\],[y,b,?i,r]]);IO.puts [s,y,b,"*\n",r,(for i<-0..8,do: [d.(32,8-i),Enum.take(t,i*2+1),10]),s,r,?H] Even without blinking enabled in the terminal, you could probably use |
Best I can do so far is 103 characters: c=&[:string.centre(&1,17),'\n'];IO.puts c.('*')++(for i<-0..8,do: c.(List.duplicate 48,2*i+1))++c.('H') Note that it has an extra blank line below the trunk. If that is not acceptable, I could switch |
@rubysolo Oh, neat discovery with |
@rubysolo thanks for your solution. Great that you used Erlang. I think it's all to easy to think of elixir as one language, and forget that you have this whole other ecosystem. Well done! |
:io.format("~10.1c~s~n~10.1c~n",[?*,(for n<-1..9,do: :io_lib.format("~n~#{9+n}.#{2*n-1}c",'0')),?H]) 89 characters We did this at the Elixir Southampton Meetup |
Excellent thanks a lot. @fishcakez could you add your Twitter handle and I'll link the newsletter to you, if you are ok with it? Thanks a lot. Maybe you could come up to the Edinburgh elixir meet up 😉 |
@fishcakez Very cool solution! I count 100 chars, but you can get it to 96 by trimming some parentheses and spaces: :io.format"~10.1c~s~n~10.1c~n",[?*,(for n<-1..9,do: :io_lib.format"~n~#{9+n}.#{2*n-1}c",'0'),?H] |
:io.format"~10s~s~n~10s~n",["*",(for n<-1..9,do: :io_lib.format"~n~#{9+n}.#{2*n-1}c",'0'),"H"] 94 by using ~10s. |
p=&(IO.puts :string.centre&1,17);p.('*');for n<-1..9,do: p.(List.duplicate ?0,n*2-1);p.('H') 92 characters. I did this after the Southampton Elixir Meetup. Thanks for the great time guys and specially @fishcakez for coming all the way there! Twitter handle: |
@ventsislaf Wow. You can shave another two off for an even 90: p=&IO.puts :string.centre&1,17;p.('*');for n<-1..9,do: p.(List.duplicate ?0,n*2-1);p.('H') I intentionally did not try for centering since the example output of the challenge had no right-side spaces, but the visual output is the same – it's fun seeing all kinds of solutions. |
Great solution well done... for a moment I thought you could change the Erlang char |
@ventsislaf well done! |
@emson I don't think it's so much about being an Erlang function – The reason I could use |
Here's a different approach -- it's not anywhere near competitive at 150-ish characters, but it was fun to play with, so I thought I'd share it. 😄 m=' *H0'
c=fn
x,x when x<0->x*-1
_,x when x<0->0
_,_->3 end
z=&Enum.at m,c.(&1,&1-abs &2-9)
for i<-1..11,do: IO.puts Enum.map 1..17, &z.(rem(i,11)-2,&1) |
Use your elixir coding skills to create a Christmas tree
Your code should create a triangle of
0
characters that is9
rows high. At the top of the tree should be a star*
and at the bottom there should be a trunkH
. The star and trunk should be aligned, and the start should be directly above the top of the tree. The start and trunk are addional rows, therefore the entire tree is a total of 11 characters high.http://elixirgolf.com/articles/elixir-christmas-tree-puzzle/
Many thanks, Ben
When the puzzle is over I'll write them up on http://elixirgolf.com and link back to your Twitter handle
The text was updated successfully, but these errors were encountered: