Code Golf: Print ASCII Building

Hi everyone!

Another Code Golf that I wanted to share today. Had some community help on this one too as my initial solution was far from the smallest it could be.

Okay. Lets define the challenge:

Display to the screen or write to stdout this exactly:

    **
    **
   ****
   ****
   ****
   ****
   ****
   ****
   ****
**********
**********
**********

I’ll leave a link to this post here!

Lets get started! 🎬

My first solution was 85 bytes long and included all the required inputs into an array:

foreach($n in 4,4,3,3,3,3,3,3,3,0,0,0){"".PadRight($n-'0').PadRight(10-($n-'0'),'*')}

This successfully outputted the correct ASCII building however, I knew it could be shortened, I just couldn’t get the syntax to work.

Here comes Julian!

Julian found a way to condense the initial code down to just 64 bytes!

4,4,(,3*7),0,0,0|%{$_}|%{""|% *ht($_-'0')|% ht(10-($_-'0'))''}

List of improvements:

  • Converted 7x 3’s, Julian instead used (,3*7)
  • The foreach was replaced with a pipeline
  • PadRight replaced with |% *ht . This is something I had never seen before

I hope you learned something with this post, I certainly learned some interesting ways to shorten code thanks to Julian and the community.

Enjoy! 🎉

Leave a Comment

Your email address will not be published. Required fields are marked *

email popup image
Mark Harwood
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Fill Out This Form, And I Will Be In Touch Shortly
Contact form image
I'll Be In Touch Soon