% https://open.kattis.com/problems/aliennumbers % 1s % 2.1 Easy % I do it in Picat first since it has a built in to_radix_string() import util. import cp. main :- S = [T.split() : T in read_file_lines().tail], print(S), s(1,S). s(_,[]). s(I,[[A,L,T]|Ss]) :- nl, BaseL = L.len, BaseT = T.len, println([a=A,l=L,t=T,baseL=BaseL,baseT=BaseT]), % Identify the positions of elements in A in L (alien language) As = [(J-1) : V in A, nth(J,L,V)], writeln(as=As), % Convert this to a number, base L to_num(As,BaseL,AsN), writeln(asN=AsN), % Convert this number to base T (target langauge) % How to do this without to_radix_string/1? % Rs = AsN.to_radix_string(BaseT), % writeln(rs=Rs), % Pick the J+1'th digit inT % Res = [T[J.to_int+1] : J in Rs.to_string], % This works: Rs = dec_to_base(AsN,BaseT), writeln(rs=Rs), % Pick the J+1'th digit in T Res = [T[J.to_int+1] : J in Rs], println(res=Res), s(I+1,Ss). to_num(List, Base, Num) => Len = length(List), Num = sum([List[I]*Base**(Len-I) : I in 1..Len]). dec_to_base(N, Base) = reverse(Res) => Res = [], while (N > 0) R := N mod Base, N := N div Base, Res := Res ++ [R] end.