Skip to main content
Added */2 explanation
Source Link
0 '
  • 3.8k
  • 1
  • 23
  • 33

*/2

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).

This predicate takes two arguments. The first is a list of lists of character codes (this is how Prolog parses backtick quoted strings). The second is a associative map from points in the 12x16 map (represented as X^Y) to 32 plus the character code stored at that point in the list of lists of character codes. The 32 is added to each of the character codes so that for the color matrix it will turn the uppercase color characters into lowercase color characters.

The way it does this is generates a list of pairs of points and the character codes at that point using findall/3. It then uses list_to_assoc/2 to create the corresponding associative map from points to the character code at that point.

The findall/3 predicate is a builtin takes a "template" as its first argument, a goal as its second argument and a list as its third argument. The predicate fills the list with all the possible values of the template that cause the goal to succeed. Due to operator precedence, the template that is passed to findall/3 in */2 is parsed as (X^Y)-D. The - operator represents a pair of two values in Prolog so the template represents the point's location (X^Y) paired with 32 plus the point's character code (D). Note that the ^ used in representing the point is in no way connected to the ^/2 predicate.

Let us consider the goal that is passed to the findall/3 predicate.

nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D) % Note that the O (oh) is not a 0 (zero)

The goal contains three predicates each of which need to succeed for the goal to succeed. The nth0/3 predicate which is used twice is used to get the value at a particular index of the list (the 0 in its name indicates it is zero indexed). The first call to it stores the Yth row of the character matrix in O while the second call stores the Xth character in that row in C. The final predicate plus/3 succeeds if its first two arguments sum to its third argument. This is used to make the character code in the pair is 32 greater than the character code in the character matrix which as stated above will turn all uppercase letters into lowercase letters.

Finally findall/3 stores all of the X^Y-D combinations that cause its goal to succeed in the list L which the associative map is built from.

More Coming Soon...

More Coming Soon...

*/2

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).

This predicate takes two arguments. The first is a list of lists of character codes (this is how Prolog parses backtick quoted strings). The second is a associative map from points in the 12x16 map (represented as X^Y) to 32 plus the character code stored at that point in the list of lists of character codes. The 32 is added to each of the character codes so that for the color matrix it will turn the uppercase color characters into lowercase color characters.

The way it does this is generates a list of pairs of points and the character codes at that point using findall/3. It then uses list_to_assoc/2 to create the corresponding associative map from points to the character code at that point.

The findall/3 predicate is a builtin takes a "template" as its first argument, a goal as its second argument and a list as its third argument. The predicate fills the list with all the possible values of the template that cause the goal to succeed. Due to operator precedence, the template that is passed to findall/3 in */2 is parsed as (X^Y)-D. The - operator represents a pair of two values in Prolog so the template represents the point's location (X^Y) paired with 32 plus the point's character code (D). Note that the ^ used in representing the point is in no way connected to the ^/2 predicate.

Let us consider the goal that is passed to the findall/3 predicate.

nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D) % Note that the O (oh) is not a 0 (zero)

The goal contains three predicates each of which need to succeed for the goal to succeed. The nth0/3 predicate which is used twice is used to get the value at a particular index of the list (the 0 in its name indicates it is zero indexed). The first call to it stores the Yth row of the character matrix in O while the second call stores the Xth character in that row in C. The final predicate plus/3 succeeds if its first two arguments sum to its third argument. This is used to make the character code in the pair is 32 greater than the character code in the character matrix which as stated above will turn all uppercase letters into lowercase letters.

Finally findall/3 stores all of the X^Y-D combinations that cause its goal to succeed in the list L which the associative map is built from.

More Coming Soon...

Added some explanation
Source Link
0 '
  • 3.8k
  • 1
  • 23
  • 33

Prolog (SWI), 574 bytes

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H),T=N+1,((I\=95,(P=95;get_assoc(X^Y,C,P)))->(between(49,53,I),plus(48,M,I),nth1(M,F,Q),append(Q,R,S),T^C^H^S^F^X^Y^D;member(I,`RL`),E is(D-I//3)mod 4,T^C^H^R^F^X^Y^E;I=70,(D=0,U is X+1;D=1,V is Y+1;D=2,U is X-1;D=3,V is Y-1),(U=X;V=Y),T^C^H^R^F^U^V^D;I>97,put_assoc(X^Y,C,I,W),T^W^H^R^F^X^Y^D);N^C^H^R^F^X^Y^D).
A+C+F+L:-A*G,C*B,split_string(F,"|","",P),maplist(string_codes,P,[M|N]),0^B^G^M^[M|N]^L.

Try it online!

This defines a predicate that when called succeeds if the all the stars are successfully collected and fails otherwise. The predicate takes the arguments as such: a+c+f+x^y^d.. a and c must be lists of backtick quoted strings, while f must be a double quoted string.

Explanation Coming Soon...

This program contains three predicates, */2, ^/2, and +/2. The */2 predicates which is defined on the first line is responsible for part of the input processing. The ^/2 predicate recursively calculates how the robot moves step by step and succeeds if the robot legally collects all the stars and fails otherwise. The +/2 predicate is the main predicate of the program and prepares the input for the ^/2 predicate with some help from the */2 predicate. Note that each of these predicates technically takes only two arguments but using operators and pattern matching they can behave as if they had more arguments (I discuss this phenomenon more in depth here).

More Coming Soon...

Prolog (SWI), 574 bytes

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H),T=N+1,((I\=95,(P=95;get_assoc(X^Y,C,P)))->(between(49,53,I),plus(48,M,I),nth1(M,F,Q),append(Q,R,S),T^C^H^S^F^X^Y^D;member(I,`RL`),E is(D-I//3)mod 4,T^C^H^R^F^X^Y^E;I=70,(D=0,U is X+1;D=1,V is Y+1;D=2,U is X-1;D=3,V is Y-1),(U=X;V=Y),T^C^H^R^F^U^V^D;I>97,put_assoc(X^Y,C,I,W),T^W^H^R^F^X^Y^D);N^C^H^R^F^X^Y^D).
A+C+F+L:-A*G,C*B,split_string(F,"|","",P),maplist(string_codes,P,[M|N]),0^B^G^M^[M|N]^L.

Try it online!

This defines a predicate that when called succeeds if the all the stars are successfully collected and fails otherwise. The predicate takes the arguments as such: a+c+f+x^y^d.. a and c must be lists of backtick quoted strings, while f must be a double quoted string.

Explanation Coming Soon...

Prolog (SWI), 574 bytes

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H),T=N+1,((I\=95,(P=95;get_assoc(X^Y,C,P)))->(between(49,53,I),plus(48,M,I),nth1(M,F,Q),append(Q,R,S),T^C^H^S^F^X^Y^D;member(I,`RL`),E is(D-I//3)mod 4,T^C^H^R^F^X^Y^E;I=70,(D=0,U is X+1;D=1,V is Y+1;D=2,U is X-1;D=3,V is Y-1),(U=X;V=Y),T^C^H^R^F^U^V^D;I>97,put_assoc(X^Y,C,I,W),T^W^H^R^F^X^Y^D);N^C^H^R^F^X^Y^D).
A+C+F+L:-A*G,C*B,split_string(F,"|","",P),maplist(string_codes,P,[M|N]),0^B^G^M^[M|N]^L.

Try it online!

This defines a predicate that when called succeeds if the all the stars are successfully collected and fails otherwise. The predicate takes the arguments as such: a+c+f+x^y^d.. a and c must be lists of backtick quoted strings, while f must be a double quoted string.

Explanation

This program contains three predicates, */2, ^/2, and +/2. The */2 predicates which is defined on the first line is responsible for part of the input processing. The ^/2 predicate recursively calculates how the robot moves step by step and succeeds if the robot legally collects all the stars and fails otherwise. The +/2 predicate is the main predicate of the program and prepares the input for the ^/2 predicate with some help from the */2 predicate. Note that each of these predicates technically takes only two arguments but using operators and pattern matching they can behave as if they had more arguments (I discuss this phenomenon more in depth here).

More Coming Soon...

Added info on how to run the program
Source Link
0 '
  • 3.8k
  • 1
  • 23
  • 33

Prolog (SWI), 574 bytes

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H),T=N+1,((I\=95,(P=95;get_assoc(X^Y,C,P)))->(between(49,53,I),plus(48,M,I),nth1(M,F,Q),append(Q,R,S),T^C^H^S^F^X^Y^D;member(I,`RL`),E is(D-I//3)mod 4,T^C^H^R^F^X^Y^E;I=70,(D=0,U is X+1;D=1,V is Y+1;D=2,U is X-1;D=3,V is Y-1),(U=X;V=Y),T^C^H^R^F^U^V^D;I>97,put_assoc(X^Y,C,I,W),T^W^H^R^F^X^Y^D);N^C^H^R^F^X^Y^D).
A+C+F+L:-A*G,C*B,split_string(F,"|","",P),maplist(string_codes,P,[M|N]),0^B^G^M^[M|N]^L.

Try it online!

This defines a predicate that when called succeeds if the all the stars are successfully collected and fails otherwise. The predicate takes the arguments as such: a+c+f+x^y^d.. a and c must be lists of backtick quoted strings, while f must be a double quoted string.

Explanation Coming Soon...

Prolog (SWI), 574 bytes

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H),T=N+1,((I\=95,(P=95;get_assoc(X^Y,C,P)))->(between(49,53,I),plus(48,M,I),nth1(M,F,Q),append(Q,R,S),T^C^H^S^F^X^Y^D;member(I,`RL`),E is(D-I//3)mod 4,T^C^H^R^F^X^Y^E;I=70,(D=0,U is X+1;D=1,V is Y+1;D=2,U is X-1;D=3,V is Y-1),(U=X;V=Y),T^C^H^R^F^U^V^D;I>97,put_assoc(X^Y,C,I,W),T^W^H^R^F^X^Y^D);N^C^H^R^F^X^Y^D).
A+C+F+L:-A*G,C*B,split_string(F,"|","",P),maplist(string_codes,P,[M|N]),0^B^G^M^[M|N]^L.

Try it online!

Explanation Coming Soon...

Prolog (SWI), 574 bytes

Z*A:-findall(X^Y-D,(nth0(Y,Z,O),nth0(X,O,C),plus(32,C,D)),L),list_to_assoc(L,A).
N^C^G^[P,I|R]^F^X^Y^D:-map_assoc(\=(74),G);N<1e3,get_assoc(X^Y,G,J),J>67,put_assoc(X^Y,G,78,H),T=N+1,((I\=95,(P=95;get_assoc(X^Y,C,P)))->(between(49,53,I),plus(48,M,I),nth1(M,F,Q),append(Q,R,S),T^C^H^S^F^X^Y^D;member(I,`RL`),E is(D-I//3)mod 4,T^C^H^R^F^X^Y^E;I=70,(D=0,U is X+1;D=1,V is Y+1;D=2,U is X-1;D=3,V is Y-1),(U=X;V=Y),T^C^H^R^F^U^V^D;I>97,put_assoc(X^Y,C,I,W),T^W^H^R^F^X^Y^D);N^C^H^R^F^X^Y^D).
A+C+F+L:-A*G,C*B,split_string(F,"|","",P),maplist(string_codes,P,[M|N]),0^B^G^M^[M|N]^L.

Try it online!

This defines a predicate that when called succeeds if the all the stars are successfully collected and fails otherwise. The predicate takes the arguments as such: a+c+f+x^y^d.. a and c must be lists of backtick quoted strings, while f must be a double quoted string.

Explanation Coming Soon...

Source Link
0 '
  • 3.8k
  • 1
  • 23
  • 33
Loading