17
\$\begingroup\$
     +--+
+--+ |  | +--+ +--+
|  | |  | |  | |  |
+--+ +--+ +--+ +--+

The people of ASCIIville are rebuilding their city and are sketching out new blueprints. Your job is to draw their new city based on how many buildings they want.

The input will be how many buildings there are. Each building is half the hight of the input (rounded up if odd), not including the top and bottom of the building.

Let's draw a basic example of 4

+--+ +--+ +--+ +--+
|  | |  | |  | |  |
|  | |  | |  | |  |
+--+ +--+ +--+ +--+

As you can see here, there are two |s in every building because the input was four. But there's a catch! (meaning the example above is incorrect, and the example at the top of this post is the real output for 4)

For each building that isn't a prime number, its height goes down by one. If the number is divisible by 3, 5, or 10, it goes down one more. If it's divisible by two or more of those numbers, the amount deducted adds up (10 is divisible by 10 and 5, and it's not a prime, so it gets deducted by 3).

Let's look at an example with an input of 5.

     +--+
+--+ |  | +--+ +--+ +--+
|  | |  | |  | |  | |  |
|  | |  | |  | |  | |  |
+--+ +--+ +--+ +--+ +--+

Here's an example of 7

     +--+                     +--+
+--+ |  | +--+ +--+ +--+      |  |
|  | |  | |  | |  | |  | +--+ |  |
|  | |  | |  | |  | |  | |  | |  |
|  | |  | |  | |  | |  | |  | |  |
+--+ +--+ +--+ +--+ +--+ +--+ +--+

And an example of 10

     +--+                     +--+
+--+ |  | +--+ +--+ +--+      |  | +--+
|  | |  | |  | |  | |  | +--+ |  | |  | +--+
|  | |  | |  | |  | |  | |  | |  | |  | |  | +--+
|  | |  | |  | |  | |  | |  | |  | |  | |  | |  |
|  | |  | |  | |  | |  | |  | |  | |  | |  | |  |
+--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+ +--+

And back to 6

     +--+
+--+ |  | +--+ +--+ +--+
|  | |  | |  | |  | |  | +--+
|  | |  | |  | |  | |  | |  |
+--+ +--+ +--+ +--+ +--+ +--+

Rules:

  • If a number gets deducted so much that it's height is less than or equal to zero, it is not shown but a space is left for it (I don't believe this is possible, 6 is the closest I've found to reaching zero).
  • Trailing spaces are allowed.
  • The width of each building must be 2.
  • Your input can be from stdin or encoded into the program.
  • Your program must be a full program, no functions.
  • Your program must be able 4-15 builds. 0-3 and negative numbers aren't needed and are not specified to do anything, meaning they can throw an error, print garbage, or do nothing.
\$\endgroup\$
8
  • \$\begingroup\$ @isaacg Yup! Just fixed it \$\endgroup\$
    – jado
    Jul 14, 2015 at 4:43
  • 1
    \$\begingroup\$ 1 is not a prime number. From the examples, it looks like you deduct 1 from the height if the (1-based) index is composite rather than non-prime. \$\endgroup\$
    – Dennis
    Jul 14, 2015 at 5:42
  • \$\begingroup\$ Note that a standard 80-character console window will only fit 16 buildings, which could make the 20 building requirement a bit ugly. \$\endgroup\$ Jul 14, 2015 at 5:54
  • \$\begingroup\$ @Hand-E-Food Thanks for mentioning that! I've change it so you put the buildings on new lines. \$\endgroup\$
    – jado
    Jul 14, 2015 at 6:01
  • \$\begingroup\$ @Dennis I wasn't sure whether to do Fibonacci or prime numbers, so the one was kept from that. Just fixed it! \$\endgroup\$
    – jado
    Jul 14, 2015 at 6:03

6 Answers 6

4
\$\begingroup\$

CJam, 68 66 58 55 bytes

This is too long for now. But a start.

ri:K"!;tK"206b4b<{S5*a*"+--+ "a:I+K)2/)"|  | "e]I+}%zN*

UPDATE: Now hardcoding for inputs till 15 instead of calculating the offset. Suggestion by Sp3000

Try it online here

\$\endgroup\$
0
3
\$\begingroup\$

Python 2, 247 245 237 229 bytes

I=input()
R=range
B=[(-~I/2)-(x%3<1)-(x%5<1)-(x%10<1)-(~all([x%a for a in R(2,x)]or[x==2])+2)for x in R(1,I+1)]
E='+--+'
O=['\n']*I
for r in R(I):
 for h in B:O[r]+=['|  |',' '*4,E][(r==h)+(r>=h)]
print ''.join(O[::-1])+'\n'+E*I
\$\endgroup\$
3
\$\begingroup\$

C#, 223 205 bytes

This takes advantage of the need to only go to 15 buildings.

class P{
    static void Main(string[]a){
        int w=int.Parse(a[0]),
            h=(w+3)/2,
            b,
            x,
            y=-1;
        for(;++y<=h;)
            for(x=-2;++x<w;)
                System.Console.Write(x<0?"\n":(b=y-("012021101211102"[x]&7))<0?"     ":b>0&y<h?"|  | ":"+--+ ");
    }
}

260 bytes

And a more generic answer that will work for any number of buildings.

class P{
    static void Main(string[]a){
        int w=int.Parse(a[0]),
            h=(w+3)/2,
            b,
            x,
            y=-1;
        for(;++y<=h;)
            for(x=-2;++x<w;){
                if(x<1)
                    b=-1;
                else
                    for(b=1;++b<x;)
                        if((x+1)%b<1)break;
                System.Console.Write(x<0?"\n":(b=y-x%3/2-x%5/4-x%10/9-(b<x?0:1))<0?"     ":b>0&y<h?"|  | ":"+--+ ");
            }
    }
}
\$\endgroup\$
2
\$\begingroup\$

Python 2, 163 bytes

n=input()
k=-~n/2
for i in range(k+2):
 s="";p=j=1
 while~n+j:a=(j%3<1)+(j%5<1)+(j%10<1)+(p%j<1);s+=" |+  -  - |+   "[(i>=a)+(i in{a,k+1})::3];p*=j*j;j+=1
 print s

The primality checking part borrows @xnor's algorithm from here.

If we hardcode the first 15 offsets, we can get 137 bytes:

n=input()
k=-~n/2
for i in range(k+2):
 s=""
 for j in range(n):a=881740113>>2*j&3;s+=" |+  -  - |+   "[(i>=a)+(i in{a,k+1})::3]
 print s

I'm assuming trailing spaces at the end of each line is okay, but if the question meant trailing spaces after the entire output then it's +9 bytes for .rstrip().

\$\endgroup\$
2
\$\begingroup\$

Groovy, 234, 225, 223 219 bytes

Abusing the 15 building limit

b=args[0]as int
m={n->(n!=2&(2..n-1).any{n%it==0}?1:0)+([3,5,10].sum{n%it==0?1:0})}
(9..0).each{println((1..b).collect{d=((b/2)as int)+1-m(it);(0..9).collect{it!=d&it!=0?it<d?"|  |":"    ":"+--+"}}*.get(it).join(" "))}
\$\endgroup\$
2
\$\begingroup\$

Swift, 375, 350 bytes

import Darwin;let(B,u,z,d)=(4.0,1.0,0.0,2.0);let H=ceil(B/d);func p(n:Double)->Double{var D=d;while D<n{if n%D==0{return 1};D++};return n==1 ?1:0};for var h=z;h<H+2;h++ {for var b=u;b<=B;b++ {var m=b%3==0 ?u:z+b%5==0 ?1:0;m=min(2,b%10==0 ?m+1:m);m += p(b);if h<m {print("     ")}else if h==H+1||h==m {print("+--+ ")}else{print("|  | ")}};print("\n")}

Here is the indented code

import Darwin
let(B,u,z,d)=(4.0,1.0,0.0,2.0)
let H=ceil(B/d)
func p(n:Double)->Double{
     var D=d
    while D<n{
        if n%D==0{
            return 1
        }
        D++
    }
    return n==1 ?1:0
}
for var h=z;h<H+2;h++ {
    for var b=u;b<=B;b++ {
        var m=b%3==0 ?u:z+b%5==0 ?1:0
        m=min(2,b%10==0 ?m+1:m)
        m += p(b)
        if h<m {
            print("     ")
        }
        else if h==H+1||h==m {
            print("+--+ ")
        }
        else{
            print("|  | ")
        }
    }
    print("\n")
}

B contains the number of buildings.
p returns 1 when a number is not prime.

I need to import Foundation in order to use the ceil function.

I didn't managed to optimize the code for just the fifteen cases, but I will eventually make it later.

edit: Taking @Kametrixom advice and optimize the mod part (I forgot to reduce var name length).

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can replace Foundation with Darwin to save some bytes \$\endgroup\$
    – Kametrixom
    Jul 16, 2015 at 15:46
  • \$\begingroup\$ -1 with import UIKit instead of import Darwin. \$\endgroup\$
    – Cœur
    Jul 18, 2019 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.