46
\$\begingroup\$

Your goal is to display ASCII art of a formation in ten-pin bowling where only some of the pins remain. Fewest bytes wins.

The tens pins are in a triangular formation:

O O O O O O O O O O 

Pins are labelled from 1 to 10 as:

7 8 9 10 4 5 6 2 3 1 

Drawing pins as O and missing pins as ., the formation 1 3 5 6 9 10 is:

. . O O . O O . O O 

Input:

A space-separated string that lists a nonempty subset of the numbers 1 though 10 in order.

Output:

Print the corresponding formation or output it as a string with linebreaks.

The formation should be flush with the left of the screen. Any whitespace is fine as long as the visible image is correct. Empty lines before and after are fine too.

Test cases:

>> 1 2 3 4 5 6 7 8 9 10 O O O O O O O O O O >> 7 10 O . . O . . . . . . >> 3 5 7 9 10 O . O O . O . . O . >> 1 . . . . . . . . . O 
\$\endgroup\$
3
  • \$\begingroup\$ Can you guarantee that there will be at least one pin? It will save characters for me if I can choke on empty input. \$\endgroup\$ Commented Jan 6, 2015 at 19:39
  • 1
    \$\begingroup\$ @undergroundmonorail It's already guaranteed: "nonempty subset" \$\endgroup\$ Commented Jan 6, 2015 at 19:40
  • 4
    \$\begingroup\$ [code-bowling]? :P \$\endgroup\$ Commented Jul 25, 2018 at 14:08

35 Answers 35

1
2
1
\$\begingroup\$

Haskell, 96 bytes

main=interact$ \a->do c<-"7 8 9 0\n 4 5 6\n 2 3\n 1" max['O'|elem c$last<$>words a]$min"."[c] 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 73 69 bytes

Thanks @l4m2 for saving 4 bytes

s=>`7890 456 23 1`.replace(/\d/g,d=>s.match(d+"\\b")?"0 ":". ") 

Attempt This Online!

Explanation:

  • s=>... Anonymous function taking string argument s
  • `7890...` Multiline string
  • .replace(/\d/g,d=>...) Replace each digit d with:
  • s.match(d+"\\b") Check if s contains d followed by a word boundary
  • ?"0 ":". " If it does, replace with "0 ", else with ". "
\$\endgroup\$
2
0
\$\begingroup\$

Java - 371 316 294 chars

public class Bowling{public static void main(String[] a){boolean[] o=new boolean[10];int i;for(String s:a){i=Integer.parseInt(s)-1;o[i]=true;}for(int j=9;j>=0;j--){p((o[j]?"0 ":". "));p(j==6?"\n ":"");p(j==3?"\n ":"");p(j==1?"\n ":"");}p("\n");}static void p(String l){System.out.print(l);}} 

First time doing this, I'm pretty sure it's shitty, but I'm a novice. It also works when the numbers aren't ordered. The numbering is wrong but I don't have time to find out how to fix it...

public class Bowling { public static void main(String[] args) { boolean[] ordened = new boolean[10]; int i; for (String s : args) { i = Integer.parseInt(s) - 1; ordened[i] = true; } for (int j = 9; j >= 0; j--) { p((ordened[j] ? "0 " : ". ")); p(j == 6 ? "\n " : ""); p(j == 3 ? "\n " : ""); p(j == 1 ? "\n " : ""); } p("\n"); } static void p(String l){ System.out.print(l); } } 

input is given by java B 1 2 3 5 10 for example. Output will then be:

0 . . . . 0 . 0 0 0 
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The numbering is wrong. \$\endgroup\$ Commented Dec 29, 2014 at 16:27
  • \$\begingroup\$ I'll try to fix it \$\endgroup\$ Commented Dec 29, 2014 at 16:28
0
\$\begingroup\$

05AB1E, 15 bytes

TLå4L£R».cT„O.‡ 

Input as a list (if input as a space-delimited string is mandatory, a leading # (+1 byte) can be added to split the implicit input-string by spaces to the necessary list).

Try it online or verify all test cases.

Explanation:

 # (e.g. input = [2,7,10]) TL # Push a list in the range [1,10] # STACK: [1,2,3,4,5,6,7,8,9,10] å # Check for each whether it's in the (implicit) input-list, # resulting in a list of 1s and 0s # STACK: [0,1,0,0,0,0,1,0,0,1] 4L # Push a list in the range [1,4] # STACK: [0,1,0,0,0,0,1,0,0,1],[1,2,3,4] £ # Split the earlier list into parts of those sizes: # STACK: [[0],[1,0],[0,0,0],[1,0,0,1]] R # Reverse it # STACK: [[1,0,0,1],[0,0,0],[1,0],[0]] » # Join each inner list by spaces, and then each line by newlines # STACK: "1 0 0 1\n # 0 0 0\n # 1 0\n # 0" .c # Centralize it by prepending spaces where necessary # STACK: "1 0 0 1\n # 0 0 0\n # 1 0\n # 0" ‡ # Transliterate T # the digits of "10" „O. # to the characters of "O." # (which replaces all "1" with "O" and "0" with "." respectively) # STACK: "O . . O\n # . . .\n # O .\n # ." # (after which the result is output implicitly) 
\$\endgroup\$
0
\$\begingroup\$

R 4.2, 159 bytes

f=function(x)cat(sapply(X=4:1,FUN=function(X){i=((X^2-X)/2+1);c(rep(" ",4-X),paste0(ifelse(1:10%in%strsplit(x," ")[[1]],"0",".")[i:(i+X-1)]," "),"\n")}),sep="") 

Try it online!

# ungolfed: f = function(s) { nums = strsplit(s, " ")[[1]] # split input string idx = 1:10 %in% nums # convert 1:10 to logical vector for given pins ind01 = ifelse(idx,"0",".") # convert to ASCII characters cat( # concatenate rows of output sapply(X=4:1, # iterate through rows FUN=function(X){ i=((X^2-X)/2+1); # same hack as @Sp3000 (nice!) c(rep(x=" ",times=4-X), # add leading spaces paste0(ind01[i:(i+X-1)]," "), # print ASCII chars separated by space "\n") # new line after each row } ), sep="") # remove default space between elements } f("1 2 3 4 5 6 7 8 9") f("7 10") f("3 5 7 9 10") f("1") 
\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.