# Base-ically Guard

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!

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!

• I approve of this message. May 13 '21 at 3:58
• Can I take the bases and/or the digits in reverse order? May 13 '21 at 4:00
• Can we take X as a list of digits?
May 13 '21 at 5:32
• You should give a test case with more than 2 bases. May 13 '21 at 7:00
• You should add a test case where the last digit isn't zero. May 13 '21 at 13:42

# 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

# 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]


# 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

• Yes, you are as fast as I expected(5 min)
– okie
May 13 '21 at 4:06
• This can be golfed to ṁḊ×\ḋḊ}. May 13 '21 at 12:14
• @JonathanAllan Wait dot product is one byte? Thanks May 13 '21 at 13:41
• @JonathanAllan unfortunately this doesn't work because in case the last digit isn't zero, it gets ignored May 13 '21 at 13:42
• Oh, I misinterpreted it - so ṁḊ1;×\ḋ then, right? May 13 '21 at 15:05

# 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)


(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.

(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.

# Vyxals, 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

• Very pleasing. Well done May 13 '21 at 5:27
• Wait, Vyxal was changed within 20 minutes of the challenge being posted?
May 13 '21 at 5:38
• @Adám yep. the cumulative-reduce is new May 13 '21 at 5:41
• Wow, cumulative reduce has been in APL for over half a century!
May 13 '21 at 5:42
• 9 bytes due to a bug fix inspired by this answer May 13 '21 at 7:39

# Husk, 9 8 bytes

δṁ*G*1t¢


Try it online!

-1 byte from Leo.

• Though, starting from 1 can be a byte shorter (using the base value for G) Try it online!
– Leo
May 13 '21 at 5:57

# JavaScript (V8), 6055 52 bytes

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


Try it online!

# 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.

# Charcoal, 13 bytes

ＩΣＥ⮌θΠ⊞Ｏ…ηκＩι


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

    θ           First input (X)
⮌            Reversed
Ｅ             Map over characters
ι   Current character
Ｉ    Cast to integer
⊞Ｏ        Push to list
η      Second input (list of bases)
…       Cyclically chopped to length
κ     Current index
Π          Take the product
Σ              Take the sum
Ｉ               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.

# JavaScript (V8), 45 bytes

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


Try it online!

# 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).