IR Hex to Raw IR code conversion - infrared

How do I get this Hex IR Code
0000 006d 0022 0003 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702 00a9 00a8 0015 0015 0015 0e6e
Into a Raw IR Code like this
int[] irdata = {4600,4350,700,1550,650,1550,650,1600,650,450,650,450,650,450,650,450,700,400,700,1550,650,1550,650,1600,650,450,650,450,650,450,700,450,650,450,650,450,650,1550,700,450,650,450,650,450,650,450,650,450,700,400,650,1600,650,450,650,1550,650,1600,650,1550,650,1550,700,1550,650,1550,650};
mIR.sendIRPattern(37470, irdata);

The first four numbers there have special meaning:
1 - 0000 indicates raw IR data (you can ignore this value)
2 - frequency
3 - length of the first burst pair sequence
4 - length of the second burst pair sequence
The frequency will be particularly important. LG wants the frequency in Hz, as you might expect, but your Hex code is in terms of the Pronto internal clock. The conversion will be:
carrierfrequency = 1000000/(HexFreq * .241246)
To the rest of the code, after that four digit preamble, LG wants those in μs, where the hex code has them in terms of the frequency. You'll need to convert each of them:
pulselength = 1000000*(HexPulse/carrierfrequency)
I'm not sure whether you want to just send the whole thing, or only the first or second burst sequence. The second is a repeat sequence, used for long button presses and the like. But keep in mind, these are in terms of pairs, not individual numbers. 00a9 00a8 is one burst pair (on time, off time). In this case:
first sequence: 00a9 00a8 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 003f 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 003f 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0040 0015 0015 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 003f 0015 0702
second sequence: 00a9 00a8 0015 0015 0015 0e6e
Sidenote: The distinct pair up front, and very large value at the end are very typical. Makes it easy to eyeball without having to count.
So, to lay out the steps:
array numbers = Split hexcode on space
(ignore numbers[0])
carrierFrequency = 1000000/(numbers[1] * .241246)
codeLength = numbers[2]
repeatCodeLength = numbers[3]
for (number in numbers[4 to end]) {
convertedToMicrosec = 1000000*(number/carrierFrequency)
fullSequenceConverted.add(convertedToMicrosec)
}
sequence1EndPoint = 2 * codeLength
sequence2EndPoint = sequence1EndPoint + 2 * repeatCodeLength
firstSequence = fullSequenceConverted from index 0 to sequence1EndPoint
secondSequence = fullSequenceConverted from sequence1EndPoint to sequence2EndPoint
mIR.sendIRPattern(carrierFrequency, firstSequence)

Related

Find element from subsets with priority assigned to each subset and within subsets

S1...S5 are web pages
S1>S2>S3>S4>S5 on web pages priority.
A1..A4 are product titles
For each web page product title priority is in the order of the definition.
For S1, A1>A2>A3 on product title priority.
product title priority is only related to a particular webpage
We need top 3 web pages and corresponding product title that are unique and of highest priority.
Input:
{S1: {A1, A2, A3}, S2: {A1, A3}, S3: {A1, A2}, S4 {A4}, S5 {A1, A4}}
Expected output:
S1: A1
S2: A3
S3: A2
Input:
{S1: {A1, A2, A3}, S2: {A1, A4}, S3: {A1,A4, A5}, S4: {A6}
Expected output:
S1:A1
S2:A4
S3:A5
Input:
{S1: {A1, A2, A3}, S2: {A1, A3}, S3: {A3}}
Output:
S1:A2
S2:A1
S3:A3
Input:
{S1: {A1, A2, A3}, S2: {A3}, S3: {A3}, S4: {A4}}
Output:
S1:A1
S2:A3
S4:A4

A program that displays the number of all descendants of this person's family tree

Sample database:
descendant("son","father")
descendant("son","father")
descendant("son","father")
etc..
For "J" number of descendants = 0.
For "A" number of descendants = 8.
I made a program, but for some reason it does not count correctly, and if there are too many branches, it outputs "False".
descendant('B', 'A').
descendant('F', 'A').
descendant('D', 'B').
descendant('E', 'B').
descendant('G', 'F').
descendant('H', 'G').
descendant('I', 'G').
descendant('J', 'G').
num_of_descendant('G', 0).
num_of_descendant(N,Nb) :-
descendant(P,N),
num_of_descendant(P,Nb1),
Nb is Nb1+1.
Update:
descendant('Игорь', 'Рюрик').
descendant('Ольга', 'Игорь').
descendant('Святослав Игоревич', 'Ольга').
descendant('Ярополк', 'Святослав Игоревич').
descendant('Святополк Окаянный', 'Ярополк').
descendant('Владимир Святой', 'Святослав Игоревич').
descendant('Ярослав I Мудрый', 'Владимир Святой').
descendant('Святослав', 'Ярослав I Мудрый').
descendant('Олег', 'Святослав').
descendant('Всеволод II', 'Олег').
descendant('Изяслав I', 'Ярослав I Мудрый').
descendant('Святополк', 'Изяслав I').
descendant('Всеволод I', 'Ярослав I Мудрый').
descendant('Владимир Мономах', 'Всеволод I').
descendant('Мстислав Великий', 'Владимир Мономах').
descendant('Изяслав II', 'Мстислав Великий').
descendant('Ярополк', 'Владимир Мономах').
descendant('Юрий Долгорукий', 'Владимир Мономах').
descendant('Владимир Мономах', 'Всеволод I').
descendant('Михаил I', 'Юрий Долгорукий').
descendant('Всеволод III Большое Гнездо', 'Юрий Долгорукий').
descendant('Юрий II', 'Всеволод III Большое Гнездо').
descendant('Ярослав II', 'Всеволод III Большое Гнездо').
descendant('Андрей', 'Ярослав II').
descendant('Василий', 'Андрей').
descendant('Константин', 'Василий').
descendant('Дмитрий Суздальский', 'Константин').
descendant('Василий Костромской', 'Ярослав II').
descendant('Ярослав III Тверской', 'Ярослав II').
descendant('Михаил II Святой', 'Ярослав III Тверской').
descendant('Александр II', 'Михаил II Святой').
descendant('Александр Невский', 'Ярослав II').
descendant('Андрей Городецкий', 'Александр Невский').
descendant('Дмитрий Переяславский', 'Александр Невский').
descendant('Даниил Московский', 'Александр Невский').
descendant('Юрий III Московский', 'Даниил Московский').
descendant('Иоанн I Калита', 'Даниил Московский' ).
descendant('Симеон Гордый', 'Иоанн I Калита').
descendant('Иоанн II Кроткий', 'Иоанн I Калита').
descendant('Дмитрий Донской', 'Иоанн II Kроткий').
descendant('Василий I', 'Дмитрий Донской').
descendant('Василий II Темный', 'Василий I').
descendant('Иоанн III', 'Василий II Темный').
descendant('Василий III', 'Иоанн III').
descendant('Иоанн IV', 'Василий III').
descendant('Федор', 'Иоанн IV').
descendant('Константин', 'Всеволод III Большое Гнездо').
descendant_path(Lower, Upper) :-
descendant(Down1, Upper),
(Down1 = Lower ; descendant_path(Lower, Down1)).
descendants_count(TopLevel, Count) :-
aggregate_all(count, Desc, descendant_path(Desc, TopLevel), Count).
Picture of tree:
Click
For "Александр Невский" number of descendents = 14,but your code return count = 7. I think your code stops on "Иоанн II Кротый" and doesn't count the rest of the descendants.
Using aggregate_all/4
descendant('B', 'A').
descendant('F', 'A').
descendant('D', 'B').
descendant('E', 'B').
descendant('G', 'F').
descendant('H', 'G').
descendant('I', 'G').
descendant('J', 'G').
descendant_path(Lower, Upper) :-
descendant(Down1, Upper),
(Down1 = Lower ; descendant_path(Lower, Down1)).
descendants_count(TopLevel, Count) :-
aggregate_all(count, Desc, descendant_path(Desc, TopLevel), Count).
Result in swi-prolog:
?- time(descendants_count('A', Count)).
% 67 inferences, 0.000 CPU in 0.000 seconds (96% CPU, 627294 Lips)
Count = 8.
?- time(descendants_count('J', Count)).
% 27 inferences, 0.000 CPU in 0.000 seconds (93% CPU, 358585 Lips)
Count = 0.
?- time(descendants_count('F', Count)).
% 47 inferences, 0.000 CPU in 0.000 seconds (95% CPU, 496110 Lips)
Count = 4.
Beware of mis-spellings and character mis-matches:
?- "Иоанн II Kроткий" = "Иоанн II Кроткий".
false.
?- "K" = "К".
false.

Printing a complex matrix Fortran

The complex matrix is declared this way:
complex(8) :: matrix(:,:)
How can I print this matrix with each element as: (a, b) or a+ib, and in a nxn format? (by that I mean as a square matrix, with a row per line, so there will be n rows and n columns)
This is the way I would print a real matrix with the format I want:
do i=1,n
do j=1,n
write(*, fmt="(f0.2, tr2)", advance="no") matrix(i,j)
end do
write(*, fmt="(a)") " "
end do
But I'm not sure how to translate this to a complex matrix
How can I print this matrix with each element as: (a, b)
Supposing you already know that (a b) is the default printing fotmat for complex type, Why isn't this just enough?
do j=1,n
write(*, *) matrix(:,j)
end do
The output would be something like:
(10.000000000000000,-20.000000000000000) (10.000000000000000,-20.000000000000000) (10.000000000000000,-20.000000000000000)
(10.000000000000000, 20.000000000000000) (10.000000000000000, 20.000000000000000) (10.000000000000000, 20.000000000000000)
If you want something more customized, you could try something like this (adjusting the field width and precision):
do j=1,n
write(*, "(*('('sf6.2xspf6.2x'i)':x))") matrix(:,j)
end do
That produces something like this:
( 10.00 -20.00 i) ( 10.00 -20.00 i) ( 10.00 -20.00 i)
( 10.00 +20.00 i) ( 10.00 +20.00 i) ( 10.00 +20.00 i)
So far this is what has worked for me. Taking into account Clinton's advice:
character(19) fmt
fmt = '(F7.2,"+",F7.2,"i")'
do i=1,n
do j=1,n
fmt(8:8) = MERGE('+',' ',imag(a(i,j)).gt.0)
write(*,fmt, advance="no") a(i,j)
end do
write(*, fmt="(a)") " "
end do
And the output is:
-0.26 -0.00i -0.00 -0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i
0.00+ 0.00i -0.25 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i
0.00 0.00i 0.00 0.00i -0.05 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i
0.00 0.00i 0.00 0.00i 0.00 0.00i -0.05 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i
0.00+ 0.00i -0.46 -0.00i -0.00+ 0.00i -0.00+ 0.00i -0.50 0.00i 0.00 -0.00i 0.00 0.00i -0.00 0.00i
0.32+ 0.00i -0.00+ 0.00i 0.00 -0.00i -0.00 -0.00i 0.00+ 0.00i -0.27 0.00i 0.00 0.00i 0.00 0.00i
-0.00+ 0.00i 0.00 -0.00i -0.00 -0.00i 0.24+ 0.00i 0.00 0.00i 0.00 0.00i -0.18 0.00i 0.00 0.00i
0.00 0.00i -0.00 -0.00i 0.24+ 0.00i 0.00 -0.00i 0.00 0.00i 0.00 0.00i 0.00 0.00i -0.18 0.00i
If someone has a better suggestion I'll be glad to hear it :)
Here is something that has worked in the past, it could be fine tuned quite a bit
!! compile and link with gfortran -I/usr/include -o PrintComplex PrintComplex.f90
Program PrintComplex
use, intrinsic :: iso_c_binding
implicit none
integer, parameter :: N=16
integer :: k
real (kind=c_double) :: val(N)
complex (kind=c_double_complex) :: in(N)
character(19) fmt
fmt = '(F7.2,"+",F7.2,"i")'
val=(/(sin(3.14159d0*float(k)/3.d0),k=1,N)/)
in=cmplx(val,-val/2)
print *,"in"
do k=1,N
fmt(8:8) = MERGE('+',' ',imag(in(k)).gt.0)
write(*,fmt)in(k)
end do
End Program PrintComplex
The output is:
in
0.87 -0.43i
0.87 -0.43i
0.00 -0.00i
-0.87+ 0.43i
-0.87+ 0.43i
-0.00+ 0.00i
0.87 -0.43i
0.87 -0.43i
0.00 -0.00i
-0.87+ 0.43i
-0.87+ 0.43i
-0.00+ 0.00i
0.87 -0.43i
0.87 -0.43i
0.00 -0.00i
-0.87+ 0.43i

Syntax error during associative array expansion in Bash 4.2

all
I got two 2D-array files to read with bash
these two files contain different rows x columns such as:
file1.txt (nx6)
DIFFUSION X 0.054 0.072 Y E2
DEEP_N_WELL X 1.8 2.25 N D
PW_CORE X 0.306 0.306 Y U1
PW_IO X 0.306 0.306 Y T1
NW_CORE X 0.306 0.306 Y U1
NW_IO X 0.306 0.306 Y T1
TG X 0.306 0.306 N S1
N+_POLY_IMP X 0.162 0.162 Y T1
POLY1 X 0.036 0.09 Y F2
LV_NLDD X 0.162 0.162 Y T1
LV_PLDD X 0.162 0.162 Y T1
LPL_NLDD X 0.162 0.162 Y T1
LPL_PLDD X 0.162 0.162 Y T1
CELL_NLDD X 0.216 0.216 N U1
CELL_PLDD X 0.216 0.216 N U1
HV_NLDD X 0.162 0.162 Y T1
HV_PLDD X 0.162 0.162 Y T1
N+ X 0.162 0.162 Y T1
P+ X 0.162 0.162 Y T1
SMT X 0.162 0.162 Y T1
PESD X 0.486 0.486 N A
SAB X 0.324 0.324 N S1
CONTACT X 0.054 0.072/0.090 Y F1
METAL1 1x 0.063 0.063 Y E2
MVIA1 1x 0.063 0.063/0.081 Y E1
METAL2 1x 0.063 0.063 Y E2
MVIA2 1x 0.063 0.063/0.081 Y E1
METAL3 1x 0.063 0.063 Y E2
MVIA3 1x 0.063 0.063/0.081 Y E1
METAL4 1x 0.063 0.063 Y E2
MVIA4 1x 0.063 0.063/0.081 Y E1
METAL5 1x 0.063 0.063 Y E2
MVIA5 1x 0.063 0.063/0.081 Y E1
METAL6 1x 0.063 0.063 Y E2
MVIA6 6x 0.31 0.320/0.320 N S1
METAL7 6x 0.32 0.4 N S1
TMV_RDL X 1.8 1.8 N B
AL_RDL X 1.8 1.8 N B
PASV_RDL X 5.4 5.4 N B
N_WELL X 0.306 0.306 NULL U1
HP_CELL_NLDD X 0.216 0.216 N U1
HP_CELL_PLDD X 0.216 0.216 N U1
LVH_NLDD X 0.162 0.162 Y T1
LVH_PLDD X 0.162 0.162 Y T1
LVUL_NLDD X 0.162 0.162 Y T1
LVUL_PLDD X 0.162 0.162 Y T1
CG X 0.306 0.306 N S1
AW X 1.5 1.5 N B
file2.txt (mx3)
DIFFUSION 0.054 0.072
POLY1 0.036 0.090
SMT 0.162 0.162
VTNH 0.162 0.162
VTPH 0.162 0.162
N+_POLY_IMP 0.162 0.162
P+_POLY_IMP 0.162 0.162
LV_NLDD 0.162 0.162
LV_PLDD 0.162 0.162
HV_NLDD 0.162 0.162
HV_PLDD 0.162 0.162
LVL_NLDD 0.162 0.162
LVL_PLDD 0.162 0.162
LVH_NLDD 0.162 0.162
LVH_PLDD 0.162 0.162
LPL_NLDD 0.162 0.162
LPL_PLDD 0.162 0.162
HVL_NLDD 0.162 0.162
N+ 0.162 0.162
P+ 0.162 0.162
SG 0.162 0.162
VTP_WLDR 0.162 0.162
VTN_WLDR 0.162 0.162
HS_CELL_PLDD 0.216 0.216
HS_CELL_NLDD 0.216 0.216
NW_CORE 0.306 0.306
PW_CORE 0.306 0.306
PW_IO 0.306 0.306
NW_IO 0.306 0.306
DT 0.090 0.118
CONTACT 0.054 0.072/0.091
METAL1 0.063 0.063
MVIA1 0.063 0.063/0.081
METAL2 0.063 0.063
MVIA2 0.063 0.063/0.081
METAL3 0.063 0.063
MVIA3 0.063 0.063/0.081
METAL4 0.063 0.063
MVIA4 0.063 0.063/0.081
METAL5 0.063 0.063
MVIA5 0.063 0.063/0.081
METAL6 0.063 0.063
MVIA6 0.063 0.063/0.081
METAL7 0.063 0.063
MVIA7 0.126 0.126
METAL8 0.126 0.126
MVIA8 0.126 0.126
METAL9 0.126 0.126
what I wanna do is to extract the elements inside both files then do some comparisons as following picture:
http://imgur.com/3Zd0TKD.jpg
"DESC1==DESC2" or "DESC1!=DESC2" in green label is a obstructer for me
I really wanna do is to take one element in $DESC1 and compare with whole elements in ${DESC2[#]}, if it does/dosen't find a element in ${DESC2[#]} then feedback true/false
Here is my work:
#!/bin/bash
clear
##===================================================================##
##===================================================================##
##========== read information from file1.txt and file2.txt ==========##
##===================================================================##
##===================================================================##
idx1=0
while read -a file1array$idx1; do
let idx1++
done < file1.txt
idx2=0
while read -a file2array$idx2; do
let idx2++
done < file2.txt
##===================================================================##
##===================================================================##
##================ start to compare these two files =================##
##===================================================================##
##===================================================================##
for ((i=0; i<idx1; i++)); do
for ((j=0; j<idx2; j++)); do
DESC1="file1array$i[0]"
DM1="file1array$i[1]"
W1="file1array$i[2]"
S1="file1array$i[3]"
CRITICAL1="file1array$i[4]"
GRADE1="file1array$i[5]"
DESC2="file2array$j[0]"
W2="file2array$j[1]"
S2="file2array$j[2]"
if [[ "${!GRADE1}" == [E-GT-Z][1-9] && "${!DESC1}" == "${!DESC2}" ]]; then
W1_Judge=`expr "scale=3; ${!W1} - ${!W2}" | bc`
S1_Judge=`expr "scale=3; ${!S1} - ${!S2}" | bc`
[ $W1_Judge != 0 -o $S1_Judge != 0 ] && declare -A jgWS=( ["${!DESC1}"]="WSNG" )
elif [[ "${!GRADE1}" == [E-GT-Z][1-9] && "${!DESC1}" != "${!DESC2}" ]]; then
[ "${!CRITICAL1}" != "NULL" ] && declare -A jgLOSS=( ["${!DESC1}"]="LOSSNG" )
elif [[ "${!GRADE1}" != [E-GT-Z][1-9] && "${!DESC1}" == "${!DESC2}" ]]; then
[[ "$DM1" == [1-2]x || "$DM1" == "X" ]] && declare -A jgEXTRA=( ["${!DESC1}"]="EXTRANG" )
else
declare -A jgBYPASS=( ["${!DESC1}"]="OK" )
fi
done
if [ "${jgWS[${!DESC1}]}" == "WSNG" ]; then
echo "${!DESC1} : W or S NG"
elif [ "${jgLOSS[${!DESC1}]}" == "LOSSNG" ]; then
echo "${!DESC1} : LOSS NG"
elif [ "${jgEXTRA[${!DESC1}]}" == "EXTRANG" ]; then
echo "${!DESC1} : EXTRA NG"
else
echo "${!DESC1} : OK"
fi
done
How can I solve the part of green label or is there any easier way to achieve the goal? I feel that I'm very close but turns out I'm just stuck with for-loop and test
The error message is "N+: syntax error: operand expected (error token is "+")"
Actually I can simply define "declare -A jgEXTRA=( ["N+"]="EXTRANG" )" and "echo {jgEXTRA[N+]}"
so I don't know what's going on? Is there any way to solve it?
For the first question:
The complexity of your script is high. It would be an issue if file1 and file2 are big. You should sort them to reduce the complexity (from N^2 to log N).
Do you consider to use 'comm' command? I think this does a big part of the job and could let you simplify your script a lot
For the second question:
It seems you do not initialize jgWS in some condition. When it happens, ${jgWS[a]} return empty string, but ${jgWS[a+]} prints your error message. I am not sure to understand why, but it is what I observed.
Other remarks
You read loops, at the begining, will miss the last lines of file1 and file2 if they are not terminated by a Linux Line Feed (because read returns false when it doesn't finish on LF).

Tell prolog to stop and start search with other possibilities

I'm trying to solve the Rubik's cube with prolog, I've tried this:
cubo_resuelto(F, F, F, F, F, F, F, F, F,
A, A, A, A, A, A, A, A, A,
I, I, I, I, I, I, I, I, I,
D, D, D, D, D, D, D, D, D,
B, B, B, B, B, B, B, B, B,
T, T, T, T, T, T, T, T, T).
mov(f, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F7, F4, F1, F8, F5, F2, F9, F6, F3,
A1, A2, A3, A4, A5, A6, I9, I6, I3,
I1, I2, B1, I4, I5, B2, I7, I8, B3,
A7, D2, D3, A8, D5, D6, A9, D8, D9,
D7, D4, D1, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9)).
mov(a, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,%
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(D1, D2, D3, F4, F5, F6, F7, F8, F9,
A7, A4, A1, A8, A5, A2, A9, A6, A3,
F1, F2, F3, I4, I5, I6, I7, I8, I9,
T9, T8, T7, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, I3, I2, I1)).
mov(d, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, B3, F4, F5, B6, F7, F8, B9,
A1, A2, F3, A4, A5, F6, A7, A8, F9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D7, D4, D1, D8, D5, D2, D9, D6, D3,
B1, B2, T3, B4, B5, T6, B7, B8, T9,
T1, T2, A3, T4, T5, A6, T7, T8, A9)).
mov(i, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(A1, F2, F3, A4, F5, F6, A7, F8, F9,
T1, A2, A3, T4, A5, A6, T7, A8, A9,
I7, I4, I1, I8, I5, I2, I9, I6, I3,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
F1, B2, B3, F4, B5, B6, F7, B8, B9,
B1, T2, T3, B4, T5, T6, B7, T8, T9)).
mov(b, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, F3, F4, F5, F6, I7, I8, I9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, T3, T2, T1,
D1, D2, D3, D4, D5, D6, F7, F8, F9,
B7, B4, B1, B8, B5, B2, B9, B6, B3,
D9, D8, D7, T4, T5, T6, T7, T8, T9)).
mov(t, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
D3, D6, D9, A4, A5, A6, A7, A8, A9,
A3, I2, I3, A2, I5, I6, A1, I8, I9,
D1, D2, B9, D4, D5, B8, D7, D8, B7,
B1, B2, B3, B4, B5, B6, I1, I4, I7,
T7, T4, T1, T8, T5, T2, T9, T6, T3)).
move(+M, OldState, NewState):-
mov(M, OldState, NewState).
move(-M, OldState, NewState):-
mov(M, NewState, OldState).
move_list([], X, X).
move_list([Move|T], X, Z):-
move(Move, X, Y),
move_list(T, Y, Z).
So I can do something like this:
move_list(X, cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'G', 'G', 'G',
'G', 'G', 'W', 'G', 'G', 'W', 'G', 'G', 'W',
'B', 'P', 'P', 'B', 'P', 'P', 'B', 'P', 'P',
'P', 'P', 'P', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'),
cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y')).
And it will answer with
X = [+f, +f, +f]
But when I try this:
move_list(X, cubo('R', 'R', 'P', 'R', 'R', 'W', 'P', 'P', 'P',
'B', 'B', 'R', 'B', 'B', 'R', 'G', 'G', 'R',
'G', 'G', 'W', 'G', 'G', 'W', 'R', 'R', 'W',
'B', 'B', 'B', 'P', 'P', 'P', 'B', 'Y', 'Y',
'Y', 'Y', 'Y', 'P', 'W', 'W', 'P', 'W', 'W',
'W', 'G', 'G', 'Y', 'Y', 'B', 'Y', 'Y', 'G'),
cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y')).
Prolog try to solve just with the +f movement.
I want to tell prolog that after 4 +f start look with other moves.
I also want to tell it that just try at max N movements.
How can I do that?
Thanks.
PS: I know that is not the best way to solve the Rubik's cube, but I want to try that way.
To limit the number of moves, add counter to the predicate. Move should be valid only if the counter is within the limit. Every move should decrease the counter. Counter(s) should be part of your state.
And you are right, that seems like a very bad way to solve Rubik's cube, especially if you allow reaching the same state twice - to prevent this, look at designing backtracking algorithms in prolog.

Resources