17
\$\begingroup\$

Introduction

A double-castle number™ is a positive integer number that has a pattern of $$\underbrace{a\cdots a}_{m\text{ }a\text{s}}b\underbrace{a\cdots a}_{m\text{ }a\text{s}}b\underbrace{a\cdots a}_{m\text{ }a\text{s}}\underbrace{a\cdots a}_{n\text{ }a\text{s}}b\underbrace{a\cdots a}_{n\text{ }a\text{s}}b\underbrace{a\cdots a}_{n\text{ }a\text{s}}$$ Where \$m>0\$, \$n>0\$ and \$a-b=1\$ are all non-negative integers, when represented in an integer base \$B\$ where \$B\ge2\$. It is so named because a bar chart representing the base-\$B\$ digits of such a number resembles two castles of the same height place- side by side.

For example, \$7029\$* is a double-castle number because when represented in base 2 it becomes \$1101101110101_2\$, which can be split into \$11011011\$ and \$10101\$.

enter image description here

This is the case when \$m=2\$, \$n=1\$, \$a=1\$, \$b=0\$ and \$B=2\$.

\$305421994212\$ is also a double-castle number because when represented in base 8 it becomes \$4343444344344_8\$, which can be split into \$43434\$ and \$44344344\$.

enter image description here

This is the case when \$m=1\$, \$n=2\$, \$a=4\$, \$b=3\$ and \$B=8\$.

For \$a>=10\$, \$a\$ should be treated as a single base-\$B\$ "digit" with the value of \$a\$ in base-10. \$206247763570426655730674346\$ is a double-castle number in base-16, whose representation in base-16 is \$\text{AA9AA9AAAAAA9AAAA9AAAA}_{16}\$. Here, \$a=10\$ but is treated as a single digit \$(10)_{16}=\text{A}_{16}\$.

enter image description here

This is the case when \$m=2\$, \$n=4\$, \$a=10\$, \$b=9\$ and \$B=16\$.

Challenge

Write a program or function that, given integers \$m>0\$, \$n>0\$, \$1\le a<B\$ and \$B\ge2\$, calculate the corresponding double-castle number™ and output it in base-10.

Test Cases

The input below are in base-10, but in the case say when \$a=11\$ and \$B=12\$ the input should be understood as \$B_{12}\$.

m, n, a,  B  => Output
1, 1, 1,  2  => 693
2, 1, 1,  2  => 7029
1, 2, 3,  4  => 62651375
1, 2, 4,  8  => 305421994212
1, 4, 7,  10 => 7676777776777767777
2, 4, 8,  16 => 164983095594247313234036872
2, 4, 10, 16 => 206247763570426655730674346

Winning Condition

This is a code-golf challenge, the shortest submission in each language wins. No standard loopholes allowed.

*7029 comes from my ID minus it written reversed.

\$\endgroup\$
  • 1
    \$\begingroup\$ How are numbers > 10 in the input to be represented. Say, for base 16, will it be "10, 11, 12, 16", or "A, B, C, 16"? Are we free to pick? \$\endgroup\$ – Abigail Jun 2 at 11:25
  • \$\begingroup\$ Could you add one or multiple test case where \$a\geq10\$? Both my answers apparently failed those cases.. (i.e. 2, 4, 10, 16 => 206247763570426655730674346). Also, maybe some test cases where n and m are larger than 10. And change the term 'digits' to 'numbers' in the sentence "It is so named because a bar chart representing the digits of such a number resembles two castles of the same height placed side by side." please, since it's incorrect and actually made me thing we only had to work with digits. \$\endgroup\$ – Kevin Cruijssen Jun 2 at 14:59
  • \$\begingroup\$ @KevinCruijssen I have clarified that the inputs are shown in base-10 but should be treated as the digit with the same value in base-B. \$\endgroup\$ – Shieru Asakoto Jun 2 at 16:53
  • \$\begingroup\$ @Abigail Oops I may have misunderstood your question, I mean you may receive like 10 as an input in base-16 but the number should be treated as \$A_{16}\$ instead of \$10_{16}\$ when calculating. \$\endgroup\$ – Shieru Asakoto Jun 2 at 16:59
  • \$\begingroup\$ @KevinCruijssen To further clarify the case I am going to add an image illustrating what I mean “the base-B digits”. \$\endgroup\$ – Shieru Asakoto Jun 2 at 17:05

11 Answers 11

6
\$\begingroup\$

Python 2, 83 81 79 72 bytes

-9 bytes thanks to @ovs!
-2 bytes thanks to @dingledooper!

m,n,a,B=input()
r=0
for s in n,m+n,m,m:r+=B**n;n-=~s
print~-B**n/~-B*a-r

Try it online!

Calculate aaaa...aaa in base B, then subtract 1 at the appropriate digits.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 79 bytes \$\endgroup\$ – dingledooper Jun 2 at 21:32
  • \$\begingroup\$ @dingledooper Thanks, I didn't know ** takes precedence over ~-. \$\endgroup\$ – Surculose Sputum Jun 2 at 21:37
  • 1
    \$\begingroup\$ 72 bytes as a full program. \$\endgroup\$ – ovs Jun 3 at 5:57
  • \$\begingroup\$ @ovs Updating n is really clever, thanks! \$\endgroup\$ – Surculose Sputum Jun 3 at 8:57
  • \$\begingroup\$ @ovs Why did you delete your answer? I think you can make it work for arbitrary a at a cost of only 2 bytes (build up the first number using '1' instead of `a` and then multiply by a). \$\endgroup\$ – Neil Jun 4 at 0:07
5
\$\begingroup\$

C (gcc) -lm, 83 \$\cdots\$ 81 80 bytes

Saved 1 2 bytes thanks to ceilingcat!!!

#define P-pow(B,n
f(m,n,a,B){n=-(1 P*3+3*m+4))/~-B*a P)P-~n)P*3+2+m)P*3+3+2*m);}

Try it online!

Calculates the wall to full height first and then crenellation is performed through subtraction.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 10 12 bytes

иεÐ)²<.ý}˜sβ

Try it online or verify all test cases.

+2 bytes as bugfix for \$a\geq10\$ (initial 10-byter answer: ×εЬ<ý}Jsö - Try it online or verify all test cases).

Explanation:

и             # Repeat the second (implicit) input the first (implicit) inputs amount of
              # times as list
              #  i.e. [1,2] and 4 → [[4],[4,4]]
 ε            # Map each to:
  Ð           #  Triplicate the value
              #   i.e. STACK: [4,4],[4,4],[4,4]
   )          #  Wrap them into a list
              #   i.e. STACK: [[4,4],[4,4],[4,4]]
    ²<        #  Push the second input - 1
              #   i.e. STACK: [[4,4],[4,4],[4,4]],3
      .ý      #  Intersperse this list with this value
              #   i.e. STACK: [[4,4],3,[4,4],3,[4,4]]
        }˜    # After the map: flatten the list
              #  i.e. [[[4],3,[4],3,[4]],[[4,4],3,[4,4],3,[4,4]]]
              #   → [4,3,4,3,4,4,4,3,4,4,3,4,4]
          s   # Swap to get the third (implicit) input
           β  # Base-convert the list we created to an integer using the input as base
              #  i.e. [4,3,4,3,4,4,4,3,4,4,3,4,4] and 8 → 305421994212
              # (after which the result is output implicitly)
| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

K (ngn/k), 26 bytes

{z/,/(2+3*x)#'(x#'y),'y-1}

Explanation

{z/,/(2+3*x)#'(x#'y),'y-1} / using x=2 1; y=1; z=2 as an example
              (x#'y)       / (m;n) copies of a;     ex: (1 1;1)
                    ,'y-1  / append b=a-1 to each   ex: (1 1 0;1 0)
     (2+3*x)               / length of each sublist ex: (8;5)
            #'             / copy to each length    ex: (1 1 0 1 1 0 1 1; 1 0 1 0 1)
   ,/                      / join                   ex: (1 1 0 1 1 0 1 1 1 0 1 0 1)
 z/                        / convert from base B    ex: 7029

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

perl -Mbigint -alp, 87 bytes

($m,$n,$a,$B)=@F;$o=$o*$B+$_ for(($a)x$m,$a-1)x2,($a)x$m,(($a)x$n,$a-1)x2,($a)x$n;$_=$o

Try it online!

We take the input, turn it into a corresponding list of digits, then convert it to base 10. Input is taken as space separated numbers on STDIN.

The TIO code does have some header code; this is just there so it works with multiple inputs -- without the header, it will only do the first line correctly. (those header bytes aren't counted).

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Jelly,  12  11 bytes

‘3×þṬạṖ€Ẏḅ⁵

A full program accepting three arguments: [m,n] a B which prints the result.

Try it online!

How?

‘3×þṬạṖ€Ẏḅ⁵ - Main Link: [m,n]; a
‘           - increment -> [m+1, n+1]
 3          - three
   þ        -   outer product ([1, 2, 3] by [m+1, n+1]) with:
  ×         -     multiplication -> indexes of low points (with an extra low on the
                                     right for each of castle)
    Ṭ       - un-truth (vectorises) -> two lists of 1s and zeros (1s at low points)
     ạ      - absolute difference (with a) -> convert zeros to a and 1s to a-1=b
      Ṗ€    - pop from each -> remove the extra low points)
        Ẏ   - tighten -> from a list of two lists to a flat list
          ⁵ - programs 5th argument = B
         ḅ  - convert dfrom base
            - implicit print
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 27 bytes

Nθ≔⭆E²N⪫E³×0ι1δI⁻×N⍘⭆δ1θ⍘δθ

Try it online! Link is to verbose version of code. Takes input in the order B, m, n, a. Explanation:

Nθ

Input B.

≔⭆E²N⪫E³×0ι1δ

Input m and n. For each value, create a string of that many 0s. Join 3 of those strings with 1s. Join the final strings together.

I⁻×N⍘⭆δ1θ⍘δθ

Replace all of the characters in the string with 1s, convert from base B, multiply by a, then subtract the string converted from base B, and output in decimal.

Note that although Charcoal's string based conversion accepts numeric bases higher than 62 this only succeeds when (as here) all the digit values are less than 62.

Slightly shorter solutions are possible that place upper limits on the supported value of B:

NθI↨E⭆E²N⪫E³×ι℅θ℅⊖θ℅ιN

Try it online! Link is to verbose version of code. Takes input in the order a, m, n, B. Works for B up to 65536. Works by creating a string of characters whose ordinal is a or b, then converting back into ordinals and decoding using base B. 22 bytes.

NθI⍘⭆E²N⪫E³×ι⍘θφ⍘⊖θφN

Try it online! Link is to verbose version of code. Takes input in the order a, m, n, B. Works for B up to 62. Works by creating a string of characters whose base B code is a or b, then decoding using base B. 21 bytes.

I⍘⭆E²N⪫E³×ιεI⊖εN

Try it online! Link is to verbose version of code. Takes input in the order a, m, n, B. Works for B up to 10. Works by creating a string of characters whose value is a or b, then decoding using base B. 16 bytes.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 16 bytes

⎕⊥⎕-~1\⍨∊5⍴¨⎕,¨0

Try it online!

A full program that takes m n, a, B on three lines of stdin.

How it works

⎕⊥⎕-~1\⍨∊5⍴¨⎕,¨0
            ⎕     ⍝ Take (m n) from stdin
             ,¨0  ⍝ Append 0 to each; (m 0)(n 0)
         5⍴¨      ⍝ Repeat each to length 5; (m 0 m 0 m)(n 0 n 0 n)
        ∊  ⍝ Flatten; (m 0 m 0 m n 0 n 0 n)
     1\⍨   ⍝ Expand 1 by above: m/n become m/n copies of 1, 0 becomes single 0
    ~      ⍝ Boolean negate the above
  ⎕-       ⍝ Subtract each from a (taken from stdin)
⎕⊥         ⍝ Convert from base B (taken from stdin) to integer
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Does J have any analog of APL's ? It seems to help a lot in challenges with many args. \$\endgroup\$ – Jonah Jun 3 at 3:19
  • \$\begingroup\$ @Jonah IIRC anything close to stdin costs at least 5 bytes, so I'd just use explicit adverb 1 :'body' / conjunction 2 :'body'. \$\endgroup\$ – Bubbler Jun 3 at 3:25
2
\$\begingroup\$

J, 35 bytes

{:@[#.((#10$5$0 1-~{.)~[:,5$"1,.&1)

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Husk, 12 11 bytes

`BṁȯJ←¹R3`R

Try it online! Takes arguments in the order a [m,n] B.

Explanation

`BṁȯJ←¹R3`R  Implicit arguments a, [m,n], B.
             Say a=3, m=1, n=2, B=4.
  ṁȯ         Map over [m,n] and concatenate:
               Use n=2 as example.
         `R    Repeat a that many times: [3,3]
       R3      Repeat three times: [[3,3],[3,3],[3,3]]
     ←¹        a decremented: 2
    J          Join by it: [3,3,2,3,3,2,3,3]
             Result: [3,2,3,2,3,3,3,2,3,3,2,3,3]
`B           Interpret in base B: 62651375
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 75 bytes

There may be rounding errors due to JavaScript's integer limit.

(m,n,a,B)=>B**(3*(m+n)+4)/~-B*a-B**(n-~n)-B**(m+3*n+2)-B**(2*m-3*~n)-B**n-1

Try it online!

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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