What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
The Rubik's Cube is a pleasant and challenging pastime. In this exercise however, we don't want to solve the cube. We want to (mindlessly) execute the same sequence over and over again. We would like to know how long it will take us to go back to the original starting position.
Write a program which, given a series of moves, outputs the number of times that sequence must be executed to reach the original state again.
Input Description
A space separated series of movies in the official WCA Notation will be given.
There are 6 faces. U (up, the top face). D (down, the bottom face). L (left). R (right). F (front). B (back). * Each face is turned like you were looking at it from the front. * A notation such as X means you turn the X face clockwise 90'. So R L means turn the right face clockwise 90' (from its perspective), then the left face clockwise 90' (from its perspective). * A notation such as X' (pronounced prime) means you turn the X face anticlockwise 90'. So R U' means turn the right face clockwise 90', then the top face anticlockwise 90'. * notation such as X2 means you turn the X face 180'.
Example (each line is a separate challenge):
R F2 L' U D B2
Output Description
The output should be the number of times you have to execute the input sequence to arrive at the original state.
Challenge Inputs
R
R F2 L' U D B2
R' F2 B F B F2 L' U F2 D R2 L R' B L B2 R U
Challenge Outputs
4
18
36
Solution
in C
#include <stdio.h>
#include <string.h>
static const char faces[] = "FRBLUD";
static const char init[9 * 6] =
"%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char rotations[][9 * 6] = {
"+(%,)&-*'O/0P23Q56789:;<=>?@ARCDSFGTIJKLMNHEB41.UVWXYZ",
"%&T()W+,Z41.52/630Q89N;<K>?@ABCDEFGHIJ'LM*OP-RS=UV:XY7",
"%&'()*+,-./Z12Y45X=:7>;8?<9KABJDEIGH036LMNOPQRSTUVW@CF",
"I&'L)*O,-./012345678X:;U=>RFC@GDAHEB?JK<MN9PQ%ST(VW+YZ",
"./0()*+,-789123456@AB:;<=>?%&'CDEFGHOLIPMJQNKRSTUVWXYZ",
"%&'()*FGH./0123+,-789:;<456@ABCDE=>?IJKLMNOPQXURYVSZWT"
};
static void
execute(char *dst, const char *src, int perm)
{
const char *index = rotations[perm];
for (int i = 0; i < 9 * 6; i++)
dst[i] = src[index[i] - '%'];
}
static int
parse(char *commands, int *seq)
{
int c = 0;
char *tok = strtok(commands, " \r\n");
do {
int face = strchr(faces, tok[0]) - faces;
switch (tok[1]) {
case '\'':
seq[c++] = face;
case '2':
seq[c++] = face;
case 0:
seq[c++] = face;
}
} while ((tok = strtok(0, " \r\n")));
return c;
}
int
main(void)
{
char input[256];
while (fgets(input, sizeof(input), stdin)) {
int seq[128];
int n = parse(input, seq);
char cube[2][9 * 6];
memcpy(cube[0], init, sizeof(init));
int c = 0;
do {
for (int i = 0; i < n; i++) {
char *dst = cube[(c * n + i + 1) % 2];
char *src = cube[(c * n + i + 0) % 2];
execute(dst, src, seq[i]);
}
c++;
} while (memcmp(cube[(c * n) % 2], init, sizeof(init)));
printf("%d\n", c);
}
}
The Rubik's Cube is a pleasant and challenging pastime. In this exercise however, we don't want to solve the cube. We want to (mindlessly) execute the same sequence over and over again. We would like to know how long it will take us to go back to the original starting position.
Write a program which, given a series of moves, outputs the number of times that sequence must be executed to reach the original state again.
Input Description
A space separated series of movies in the official WCA Notation will be given.
There are 6 faces. U (up, the top face). D (down, the bottom face). L (left). R (right). F (front). B (back). * Each face is turned like you were looking at it from the front. * A notation such as X means you turn the X face clockwise 90'. So R L means turn the right face clockwise 90' (from its perspective), then the left face clockwise 90' (from its perspective). * A notation such as X' (pronounced prime) means you turn the X face anticlockwise 90'. So R U' means turn the right face clockwise 90', then the top face anticlockwise 90'. * notation such as X2 means you turn the X face 180'.
Example (each line is a separate challenge):
R F2 L' U D B2
Output Description
The output should be the number of times you have to execute the input sequence to arrive at the original state.
Challenge Inputs
R
R F2 L' U D B2
R' F2 B F B F2 L' U F2 D R2 L R' B L B2 R U
Challenge Outputs
4
18
36
Solution
in C
#include <stdio.h>
#include <string.h>
static const char faces[] = "FRBLUD";
static const char init[9 * 6] =
"%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char rotations[][9 * 6] = {
"+(%,)&-*'O/0P23Q56789:;<=>?@ARCDSFGTIJKLMNHEB41.UVWXYZ",
"%&T()W+,Z41.52/630Q89N;<K>?@ABCDEFGHIJ'LM*OP-RS=UV:XY7",
"%&'()*+,-./Z12Y45X=:7>;8?<9KABJDEIGH036LMNOPQRSTUVW@CF",
"I&'L)*O,-./012345678X:;U=>RFC@GDAHEB?JK<MN9PQ%ST(VW+YZ",
"./0()*+,-789123456@AB:;<=>?%&'CDEFGHOLIPMJQNKRSTUVWXYZ",
"%&'()*FGH./0123+,-789:;<456@ABCDE=>?IJKLMNOPQXURYVSZWT"
};
static void
execute(char *dst, const char *src, int perm)
{
const char *index = rotations[perm];
for (int i = 0; i < 9 * 6; i++)
dst[i] = src[index[i] - '%'];
}
static int
parse(char *commands, int *seq)
{
int c = 0;
char *tok = strtok(commands, " \r\n");
do {
int face = strchr(faces, tok[0]) - faces;
switch (tok[1]) {
case '\'':
seq[c++] = face;
case '2':
seq[c++] = face;
case 0:
seq[c++] = face;
}
} while ((tok = strtok(0, " \r\n")));
return c;
}
int
main(void)
{
char input[256];
while (fgets(input, sizeof(input), stdin)) {
int seq[128];
int n = parse(input, seq);
char cube[2][9 * 6];
memcpy(cube[0], init, sizeof(init));
int c = 0;
do {
for (int i = 0; i < n; i++) {
char *dst = cube[(c * n + i + 1) % 2];
char *src = cube[(c * n + i + 0) % 2];
execute(dst, src, seq[i]);
}
c++;
} while (memcmp(cube[(c * n) % 2], init, sizeof(init)));
printf("%d\n", c);
}
}
Comments
Post a Comment