Crown hundred crownty crown

26

3

I noticed a certain game had a peculiar life counter, which instead of stopping at 999, gained a new digit – the next number was crown hundred or 00. After 99 came crown hundred crownty (0) and the last number, after 9, was crown hundred crownty crown or , which would be 1110 in decimal.

Your task is to write a program or a function that outputs this counter.

Given an integer from the range [0,1110] (inclusive on both ends), output a three character string where

  • every character is from the list 0123456789
  • the crown () can only appear as the leftmost character or when there's a crown to the left of it
  • when this number is read as a decimal number but with the crown counting as 10, you get back the original number

Test cases

   0 → "000"
  15 → "015"
 179 → "179"
 999 → "999"
1000 → "00"
1097 → "97"
1100 → "0"
1108 → "8"
1110 → ""

You may use any non-decimal character instead of the crown. To encourage pretty printing, the crown character (UTF8 byte sequence "\240\159\145\145") counts as one byte instead of four. Your program doesn't have to work for numbers outside the valid range.

This is , so the shortest answer, measured in bytes, wins!

Angs

Posted 2018-09-08T08:10:34.243

Reputation: 4 825

4Oh, Super Mario 3D Land! – Deusovi – 2018-09-08T09:38:20.577

2@Deusovi I was actually thinking about the follow-up game, Super Mario 3D World, but well guessed! – Angs – 2018-09-08T10:00:32.820

3This should be the IMO number for Boaty McBoatFace. – Mr Lister – 2018-09-08T11:19:21.530

The bonus is multiplied by the number of crowns in the code, right? – Erik the Outgolfer – 2018-09-08T14:13:03.080

@EriktheOutgolfer As I understand it, if the output contains the crown, you may subtract 3 from the byte count. – Mr. Xcoder – 2018-09-08T15:34:56.467

@Mr.Xcoder I mean, a crown is 4 bytes, so, if the bonus is only applied once, but you need to use many crowns, you need to use a variable (if that's possible). – Erik the Outgolfer – 2018-09-08T15:36:10.027

@EriktheOutgolfer I changed the wording so the crown counts as one byte, no matter how many there are. – Angs – 2018-09-08T15:39:24.870

The "numbering" system here appears to be internally inconsistent - why is 999 followed by C00 (C is the crown), but C99 is followed by CC0, but 99 is not followed by C0, but… this looks like it's trying to look like base 11, with the crown being the ten-value digit. – Jeff Zeitlin – 2018-09-08T19:42:18.813

3@JeffZeitlin it's a redundant decimal system, where a number may have more than one representation (even disregarding leading zeroes). The crown is then reserved as a surprise element, only used when absolutely needed. – Angs – 2018-09-08T20:32:26.203

@JeffZeitlin It's a reference to SMB. That would make a more complex version of this challenge...

– Bob – 2018-09-10T04:41:24.063

Answers

2

05AB1E, 20 18 bytes

₄‹i₄+¦ëTð.;„1 „  :

Uses spaces for crowns.

Try it online or verify all test cases.

Explanation:

₄‹i               # If the (implicit) input is smaller than 1000:
   ₄+             #  Add 1000 to the (implicit) input
     ¦            #  And remove the leading 1 (so the leading zeros are added)
                  #   i.e. 17 → 1017 → "017"
  ë               # Else:
   Tð.;           #  Replace the first "10" with a space " "
                  #   i.e. 1010 → " 10"
                  #   i.e. 1101 → "1 1"
                  #   i.e. 1110 → "11 "
       „1 „  :    #  Replace every "1 " with "  " (until it no longer changes)
                  #   i.e. " 10" → " 10"
                  #   i.e. "1 1" → "  1"
                  #   i.e. "11 " → "   "

Kevin Cruijssen

Posted 2018-09-08T08:10:34.243

Reputation: 67 575

12

JavaScript (Node.js), 50 bytes

f=(n,p=1e3)=>n<p?(n+p+'').slice(1):'#'+f(n-p,p/10)

Try it online!

TIO was based on Arnauld's answer. Show as #.

tsh

Posted 2018-09-08T08:10:34.243

Reputation: 13 072

3Nice recursive approach! – Arnauld – 2018-09-08T09:43:15.517

9

JavaScript (ES6),  62 46  44 bytes

Saved 2 bytes thanks to @nwellnhof

Outputs crowns as x characters.

n=>(n+1e4+'').replace(/1+0/,'xxx').slice(-3)

Try it online!

How?

We add \$10000\$ to the input, coerce it to a string, look for the /1+0/ pattern and replace it with xxx. Finally, we return the 3 trailing characters.

Examples:

$$\begin{align}&0 &\rightarrow &\text{ "}\color{red}{\text{10}}\text{000"} &\rightarrow &\text{ "xxx000"} &\rightarrow &\text{ "000"}\\ &123 &\rightarrow &\text{ "}\color{red}{\text{10}}\text{123"} &\rightarrow &\text{ "xxx123"} &\rightarrow &\text{ "123"}\\ &1023 &\rightarrow &\text{ "}\color{red}{\text{110}}\text{23"} &\rightarrow &\text{ "xxx23"} &\rightarrow &\text{ "x23"}\\ &1103 &\rightarrow &\text{ "}\color{red}{\text{1110}}\text{3"} &\rightarrow &\text{ "xxx3"} &\rightarrow &\text{ "xx3"}\\ &1110 &\rightarrow &\text{ "}\color{red}{\text{11110}}\text{"} &\rightarrow &\text{ "xxx"} &\rightarrow &\text{ "xxx"} \end{align} $$

Arnauld

Posted 2018-09-08T08:10:34.243

Reputation: 111 334

s.replace(/./g,\#`)is neat... I hadArray(s.length+1).join`#``, and my regex was longer too! Nice work, +1 – Mr. Xcoder – 2018-09-08T09:23:01.457

@Mr.Xcoder It was actually a terrible idea. But it took more than one year to fix it. :D – Arnauld – 2019-11-17T09:31:45.030

8

Shakespeare Programming Language, 763 692 690 689 683 bytes

,.Ajax,.Ford,.Page,.Act I:.Scene I:.[Enter Ajax and Ford]Ford:Listen tothy!Ajax:You big big cat.Scene V:.Ajax:Remember the remainder of the quotient betweenI twice the sum ofa cat a big big cat.Ford:You be the quotient betweenyou twice the sum ofa cat a big big cat.Ajax:You be the sum ofyou a pig.Be you nicer zero?If solet usScene V.Ford:You big big cat.[Exit Ajax][Enter Page]Page:Recall.Ford:You be I.Scene X:.Page:Recall.Am I nicer zero?If notopen heart.If notlet usScene L.Ford:You big big big big big cat.Speak thy.Am I worse a cat?If soyou zero.Scene L:.[Exit Page][Enter Ajax]Ford:You be the sum ofyou a pig.Is you nicer a cat?[Exit Ajax][Enter Page]Ford:If solet usScene X.

Try it online!

Uses " " instead of crowns. At the cost of 4 more bytes, this could be modified to show a "visible" character instead.

Explanation:

,.Ajax,.Ford,.Page,.Act I:.Scene I:.[Enter Ajax and Ford]

    Boilerplate, introducing the characters.

Ford:Listen tothy!

    Input a value to Ajax.

Ajax:You big big cat.

    Set Ford's value to 4 (we will be pushing 4 digits from Ajax onto Ford's personal stack).

Scene V:.Ajax:Remember the remainder of the quotient betweenI twice the sum ofa cat a big big cat.Ford:You be the quotient betweenyou twice the sum ofa cat a big big cat.

    DIGIT-PUSHING LOOP: Push Ajax's last digit onto Ford's stack; divide Ajax by 10.

Ajax:You be the sum ofyou a pig.Be you nicer zero?If solet usScene V.

    Decrement Ford; loop until Ford is 0.

Ford:You big big cat.

    Set Ajax's value to 4 (we will pop 3 digits from Ford's stack in the next loop).

[Exit Ajax][Enter Page]Page:Recall.Ford:You be I.

    Pop the top value off Ford's stack, and store that into Page.
    Here, Page will contain 0 if there are no crowns to be drawn,
    and 1 if there are crowns to be drawn.

Scene X:.Page:Recall.Am I nicer zero?If notopen heart.If notlet usScene L.

    DIGIT-DRAWING LOOP: Pop the top value off of Ford's stack and set Ford equal to that value.
    If there are no crowns to be drawn, output Ford's literal value here, and skip the crown-drawing section.

Ford:You big big big big big cat.Speak thy.Am I worse a cat?If soyou zero.

    Draw crown.
    If we are drawing crowns, and Ford contains 0 here, then we are now done drawing crowns, and thus we store 0 into Page.
    (Put in one more "big" for the crown to look like an @ symbol.)

Scene L:.[Exit Page][Enter Ajax]Ford:You be the sum ofyou a pig.Is you nicer a cat?[Exit Ajax][Enter Page]Ford:If solet usScene X.

    Decrement Ajax; loop until Ajax is 1 (i.e. 3 times).

JosiahRyanW

Posted 2018-09-08T08:10:34.243

Reputation: 2 600

683 bytes – Hello Goodbye – 2019-11-20T14:50:01.933

@HelloGoodbye Thank you, I forgot to get rid of some spaces. – JosiahRyanW – 2019-11-21T08:21:41.527

5

Python 2, 53 bytes

Hats off to Arnauld for -22 bytes. Recursion still wins, though.

lambda k:re.sub("1+0","CCC",`k+10000`)[-3:]
import re

Try it online!

Python 2, 51 bytes

This instead implements tsh's recursive method. Saved 2 bytes thanks to ovs.

f=lambda n,p=1000:n/p and'C'+f(n-p,p/10)or`n+p`[1:]

Try it online!

Mr. Xcoder

Posted 2018-09-08T08:10:34.243

Reputation: 39 774

54 bytes by updating the 1st solution the same way as I did with my JS answer. Seems like recursion still wins in Python, though. – Arnauld – 2019-11-17T09:39:25.023

1@Arnauld Thanks :D. This edit was really a SGITW – Mr. Xcoder – 2019-11-17T09:42:20.257

nwellnhof has since pointed out that adding 10000 leads to the more straightforward pattern 1+0. Hence this 53 bytes version.

– Arnauld – 2019-11-20T08:27:19.187

@Arnauld Thanks ;) That's rather impressive. – Mr. Xcoder – 2019-11-20T10:08:28.133

4

Python 2, 52 bytes

lambda n:['%03d'%n,'%3s'%`n`.lstrip('1')[1:]][n>999]

Try it online!

ovs

Posted 2018-09-08T08:10:34.243

Reputation: 21 408

3

Retina 0.8.2, 41 bytes

\b((.)|..)\b
$#2$*00$&
T`d`_#`(?=....)1+0

Try it online! Uses #s instead of s. Link includes test cases. Explanation:

\b((.)|..)\b
$#2$*00$&

Pad 1- and 2-digit numbers to three digits.

T`d`_#`(?=....)1+0

Change leading 1s of 4-digit numbers to #s and delete the next 0.

Neil

Posted 2018-09-08T08:10:34.243

Reputation: 95 035

3

Jelly, 19 bytes - 0 = 19

<ȷ¬ȧDi0ɓ⁶ẋ⁹Ḋ;+ȷDḊṫʋ

A full program printing the result using a space character as the crown.
(As a monadic Link a mixed list of integer digits and space characters is yielded)

Try it online! Or see the test-suite.

...maybe a recursive implementation will be shorter.

How?

<ȷ¬ȧDi0ɓ⁶ẋ⁹Ḋ;+ȷDḊṫʋ - Main Link: integer, N    e.g. 1010       or   10
 ȷ                  - literal 1000                  1000            1000
<                   - less than?                    0               1
  ¬                 - logical not                   1               0
    D               - to decimal list               [1,0,1,0]       [1,0]
   ȧ                - logical and                   [1,0,1,0]       0
      0             - literal zero                  0               0
     i              - first index - call this I     2               1  (0 treated as [0] by i)
       ɓ            - new dyadic chain with swapped arguments - i.e. f(N, I)
        ⁶           - literal space character       ' '             ' '
          ⁹         - chain's right argument        2               1
         ẋ          - repeat                        [' ',' ']       [' ']
           Ḋ        - dequeue                       [' ']           []
                  ʋ - last four links as a dyad - i.e. f(N, I)
             +ȷ     -   add 1000                    2010            1010
               D    -   to decimal list             [2,0,1,0]       [1,0,1,0]
                Ḋ   -   dequeue                     [0,1,0]         [0,1,0]
                 ṫ  -   tail from index (I)         [1,0]           [0,1,0]
            ;       - concatenate                   [' ',1,0]       [0,1,0]
                    - implicit print                " 10"           "010"

Jonathan Allan

Posted 2018-09-08T08:10:34.243

Reputation: 67 804

3

Python 2, 40 bytes

lambda n:'%3s'%`10000+n`.lstrip('1')[1:]

Try it online!

Implements an idea similar to Mr. Xcoder's regex-based answer but without a regex. We remove leading 1's in 10000+n as well as the next character, then pad with spaces to length 3. The result is similar to ovs's solution using lstrip but without needing two cases.

xnor

Posted 2018-09-08T08:10:34.243

Reputation: 115 687

2

Clean, 87 bytes

Doesn't output crowns (uses c).

import StdEnv,Text
$n#i=3-n/1000-n/1100-n/1110
=("ccc"+lpad(""<+n rem(10^i))i'0')%(i,9)

Try it online!

$ n                   // function $ of `n`
 # i =                // define `i` as (the number of digits that aren't crowns)
  3 -                 // three minus
  n / 1000 -          // 1 if first digit is crown
  n / 1100 -          // 1 if second digit is crown
  n / 1110            // 1 if third digit is crown
 = (                  // the string formed by
  "ccc" +             // prefixing three crowns to
  lpad (              // the padding of
   "" <+ n rem (10^i) // non-crown digits of `n`
  ) i '0'             // with zeroes
 ) % (i, 9)           // and removing the extra crowns

Clean, 99 - 3 = 96 bytes

This one has crowns.

import StdEnv,Text
$n#i=3-n/1000-n/1100-n/1110
=(""+lpad(""<+n rem(10^i))i'0')%(i*4,99)

Try it online!

Οurous

Posted 2018-09-08T08:10:34.243

Reputation: 7 916

The second answer has a score of only 90. – pppery – 2019-07-14T00:50:18.707

1

C,  84  58 bytes

Thanks to @tsh for saving 25 bytes and thanks to @ceilingcat for saving a byte!

f(n,p){for(p=1e3;p/=10;)n-=putchar(n/p>9?46:48+n/p)%12*p;}

Try it online!

Steadybox

Posted 2018-09-08T08:10:34.243

Reputation: 15 798

1f(n,p){for(p=1000;p/=10;)n-=putchar(n/p>9?46:48+n/p)%12*p;} – tsh – 2018-09-08T16:54:44.657

1

Japt, 20 bytes

A naïve (and slightly drunk!) port of Arnauld's solution. Uses " for crown.

U+A³ s r"^21*0"_çQÃÅ

Try it

Shaggy

Posted 2018-09-08T08:10:34.243

Reputation: 24 623

1

Java 10, 84 83 bytes

n->{for(int p=100,t;p>0;n-=t%12*p,p/=10)System.out.printf("%c",t=n/p>9?46:48+n/p);}

Port of @tsh' C comment.
Uses . instead of crowns.

Try it online.

Alternative approach (84 (87-3) bytes):

n->f(n,1000)String f(int n,int p){return n<p?(n+p+"").substring(1):""+f(n-p,p/10);}

Port of @tsh' JavaScript's answer.

Try it online.

Kevin Cruijssen

Posted 2018-09-08T08:10:34.243

Reputation: 67 575

1

APL (Dyalog Unicode), 32 bytes

1e3∘{⍵<⍺:1↓⍕⍵+⍺⋄'C',(⍵-⍺)∇⍨⍺÷10}

Try it online!

Prefix direct function.

Port of @tsh's JS Answer.

How:

1e3∘{⍵<⍺:1↓⍕⍵+⍺⋄'C',(⍵-⍺)∇⍨⍺÷10} ⍝ Main function, arguments ⍵ and ⍺ (⍵ → input, ⍺ → 1000).
     ⍵<⍺:                          ⍝ If ⍵<⍺
         1↓                        ⍝ Drop (↓) the first element (1) of
           ⍕                       ⍝ Format (⍕); 'stringify'
            ⍵+⍺                    ⍝ ⍵+⍺
               ⋄                   ⍝ else
                'C',               ⍝ Concatenate (,) the literal 'C' with
                         ∇⍨        ⍝ Recursive call (∇) with swapped arguments (⍨)
                    (⍵-⍺)  ⍺÷10    ⍝ New arguments; ⍵ → ⍵-⍺; ⍺ → ⍺÷10

J. Sallé

Posted 2018-09-08T08:10:34.243

Reputation: 3 233

1

PHP, 71 bytes

for($n=$argn,$x=1e4;1<$x/=10;$n%=$n<$x?$x/10:$x)echo$n<$x?$n/$x*10|0:C;

prints C for the crown. Run as pipe with -nR or try it online.

Titus

Posted 2018-09-08T08:10:34.243

Reputation: 13 814

1

Haskell, 48 bytes

('2'#).show.(+1000)
n#(a:x)|n==a='c':'1'#x|1>0=x

Try it online!

Angs

Posted 2018-09-08T08:10:34.243

Reputation: 4 825

1

Perl 6, 38 - 9 = 29 bytes

-2 bytes thanks to Jo King

{~m/...$/}o{S/1+0//}o*+1e4

Try it online!

Inspired by Arnauld's JavaScript solution.

nwellnhof

Posted 2018-09-08T08:10:34.243

Reputation: 10 037

1

sed, 39

48 bytes, but score is 39, since each counts as 1.

/..../s/^1+0//
s/^/00/
s/.*(...)/\1/

Try it online!

Digital Trauma

Posted 2018-09-08T08:10:34.243

Reputation: 64 644

0

Clean, 96 bytes

I think Super Mario 3D Land, New Super Mao Bros.2, and Super Mario 3D World have this life counter.

import StdEnv,Text
$n#i=3-n/1000-n/1100-n/1110
=(""+lpad(""<+n rem(10^i))i'0')%(i*4,99

I agree with Clean.

Verify that I’m not cheating.

Leo

Posted 2018-09-08T08:10:34.243

Reputation: 9

3Welcome to the site. What language is this? – Post Rock Garf Hunter – 2019-07-13T14:33:38.023