5
\$\begingroup\$

Base: part 1

This is part 2, Strongly suggest you go try out the first one if you haven't tried it.

Story

As one of the solvers, You've managed to get to the door of the base heaven. In front of the door stands the base guard @lyxal, they were angry that you all just left the encoded text on the ground, they demand that you clean (decode) the ground before joining the party, those who are irresponsible get no ticket!

Task

You will get 2 inputs

  • First being X which is a non-negative integer in base of base list.
  • Second being base which is a list of bases.
  • bases range from 1 ~ 10
  • The base will contain at least one not 1 base
  • answer should be in the base 10

Example:

X, [bases] -> answer
110 ,[2] -> 6
30 ,[2,4] -> 6
2100 ,[2,3] -> 30
0 ,[any bases] -> 0
You may want to look at part 1 for more detail

Rule

  • No loopholes
  • Input/Output in any convenient, reasonable format is accepted.

Satisfy Lyxal with the shortest code, because a long code indicates that you are lazy and not doing your best!

\$\endgroup\$
9
  • 4
    \$\begingroup\$ I approve of this message. \$\endgroup\$
    – lyxal
    May 13, 2021 at 3:58
  • \$\begingroup\$ Can I take the bases and/or the digits in reverse order? \$\endgroup\$
    – hyper-neutrino
    May 13, 2021 at 4:00
  • 1
    \$\begingroup\$ Can we take X as a list of digits? \$\endgroup\$
    – Adám
    May 13, 2021 at 5:32
  • 1
    \$\begingroup\$ You should give a test case with more than 2 bases. \$\endgroup\$
    – xigoi
    May 13, 2021 at 7:00
  • 1
    \$\begingroup\$ You should add a test case where the last digit isn't zero. \$\endgroup\$
    – hyper-neutrino
    May 13, 2021 at 13:42

12 Answers 12

8
\$\begingroup\$

APL(Dyalog Unicode), 6 bytes SBCS

⍴⍨∘≢⊥⊢

Try it on APLgolf!

An anonymous tacit infix function taking the base in reverse as left argument and X as a list of digits as right argument.

 X

 evaluated in base…

⍴⍨∘≢ base cyclically reshaped to the length of X

\$\endgroup\$
7
\$\begingroup\$

Jelly, 7 bytes

Takes the bases in reverse order as the first argument and the digits as the second argument.

ṁżṛḅ@ƒ0

Try it online!

Explanation

ṁżṛḅ@ƒ0   Main dyadic link
ṁ         Reshape the list of bases like the list of digits
 ż        Zip with
  ṛ         the list of digits
     ƒ0   Reduce with initial value 0 under:
   ḅ        Convert from base
    @         [with swapped arguments]
\$\endgroup\$
6
\$\begingroup\$

Jelly, 7 bytes

ṁḊ1;×\ḋ

Try it online!

-1 byte thanks to Jonathan Allan

ṁ        Mold bases to the shape/length of the digits, repeating if necessary
 Ḋ       Drop the first base
  1;     and prepend 1 in its place
    ×\   Cumulatively reduce the bases by multiplication
      ḋ  Dot product with the digits
\$\endgroup\$
6
  • 2
    \$\begingroup\$ Yes, you are as fast as I expected(5 min) \$\endgroup\$
    – okie
    May 13, 2021 at 4:06
  • 2
    \$\begingroup\$ This can be golfed to ṁḊ×\ḋḊ}. \$\endgroup\$ May 13, 2021 at 12:14
  • 2
    \$\begingroup\$ @JonathanAllan Wait dot product is one byte? Thanks \$\endgroup\$
    – hyper-neutrino
    May 13, 2021 at 13:41
  • 2
    \$\begingroup\$ @JonathanAllan unfortunately this doesn't work because in case the last digit isn't zero, it gets ignored \$\endgroup\$
    – hyper-neutrino
    May 13, 2021 at 13:42
  • 2
    \$\begingroup\$ Oh, I misinterpreted it - so ṁḊ1;×\ḋ then, right? \$\endgroup\$ May 13, 2021 at 15:05
5
\$\begingroup\$

Pyth, 16 bytes

JEs*L*F<t*JlQ~hZ

Test suite

Inspired quite heavily by hyper-neutrino's Jelly answer.

Takes reversed list of digits of X on line 1 and reversed list of bases on line 2 of input.

Explanation:
JEs*L*F<t*JlQ~hZ  | Full program
JEs*L*F<t*JlQ~hZQ | with implicit variables
------------------+-------------------------------------------------------------------
                  | Call the first line of input Q (implicit)
JE                | Call the second line of input J
    L           Q | Map the following over each d in Q:
     *F           |  Find the product of
       <     ~hZ  |   the first Z (starts at 0, increments with each pass) elements of
         *JlQ     |    J, repeated the length of Q times
        t         |   with the first element removed (product of an empty list is 1)
   *L             |  and multiply it with d
  s               | Sum the resulting list
                  | Print (implicit)
\$\endgroup\$
5
\$\begingroup\$

Haskell, 32 bytes

(x:y)?(h:t)=x+h*y?(t++[h])
_?_=0

Try it online!

The relevant function is (?), which takes as input X as a list of digits (least significant first) and the list of bases.

Haskell, 35 bytes

(foldr(\(i,d)n->d+i*n)0.).zip.cycle

Try it online!

Same algorithm in point-free style. Takes the list of digits first and then X.

\$\endgroup\$
5
\$\begingroup\$

Vyxal s, 8 bytes

•Ḣ1p⁽*r*

Try it Online!

•Ḣ1p⁽*r*  Full Program
•         Mold bases like digits
 Ḣ        Pop first base off
  1p      Prepend 1
      r   Cumulative reduce over
    ⁽*    Multiplication
       *  Vectorized multiplication with digits

s (flag)  Sum
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Very pleasing. Well done \$\endgroup\$
    – lyxal
    May 13, 2021 at 5:27
  • \$\begingroup\$ Wait, Vyxal was changed within 20 minutes of the challenge being posted? \$\endgroup\$
    – Adám
    May 13, 2021 at 5:38
  • \$\begingroup\$ @Adám yep. the cumulative-reduce is new \$\endgroup\$
    – hyper-neutrino
    May 13, 2021 at 5:41
  • 3
    \$\begingroup\$ Wow, cumulative reduce has been in APL for over half a century! \$\endgroup\$
    – Adám
    May 13, 2021 at 5:42
  • \$\begingroup\$ 9 bytes due to a bug fix inspired by this answer \$\endgroup\$
    – lyxal
    May 13, 2021 at 7:39
4
\$\begingroup\$

Husk, 9 8 bytes

δṁ*G*1t¢

Try it online!

Same idea as hyper-neutrino's answer.

-1 byte from Leo.

\$\endgroup\$
1
  • \$\begingroup\$ Though, starting from 1 can be a byte shorter (using the base value for G) Try it online! \$\endgroup\$
    – Leo
    May 13, 2021 at 5:57
3
\$\begingroup\$

JavaScript (V8), 60 55 52 bytes

n=>b=>(g=r=>x in n?g(n[x]+r*b[++x%b.length]):r)(x=0)

Try it online!

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

Retina, 90 bytes

$
;
+`(\d+,)(.*,)?(\d+)(\d);
$2$1$3;$4
+`(.*,)?(\d+,)(\d+);(\d)
$2$1$.($2*$3*_$4*);
.*,|;

Try it online! Link includes test cases. Takes X as the last argument. Explanation:

$
;

Append a marker to keep track of the current digit.

+`(\d+,)(.*,)?(\d+)(\d);
$2$1$3;$4

Cyclically rotate the list of bases according to the number of digits.

+`(.*,)?(\d+,)(\d+);(\d)
$2$1$.($2*$3*_$4*);

Cyclically rotate the list of bases back again as each digit is converted from the next base.

.*,|;

Remove the list of bases and the marker.

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

Charcoal, 13 bytes

IΣE⮌θΠ⊞O…ηκIι

Try it online! Link is to verbose version of code. Takes X as a string. Explanation:

    θ           First input (X)
   ⮌            Reversed
  E             Map over characters
            ι   Current character
           I    Cast to integer
      ⊞O        Push to list
         η      Second input (list of bases)
        …       Cyclically chopped to length
          κ     Current index
     Π          Take the product
 Σ              Take the sum
I               Cast to string
                Implicitly print

Although it only takes 1 byte to multiply the current character by the product of the list Charcoal's product returns None for an empty list and it would cost 2 bytes to correct this, so it's easier to push the value to the list so that the product is always defined.

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

JavaScript (V8), 45 bytes

(b,i)=>g=X=>X&&X%10+b[i=b[++i]?i:0]*g(X/10|0)

Try it online!

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

C (gcc), 70 bytes

a;i;r;f(x,b,l)int*b;{for(a=1,r=i=0;x;x/=10)r+=x%10*a,a*=b[i++%l];x=r;}

Try it online!

Inputs a non-negative integer in base 10, a pointer to the array of bases, and the array's length (since C pointers don't carry any length info).

\$\endgroup\$

Your Answer

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

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