Skip to content

Commit a0428f9

Browse files
Update program.c
1 parent ff26b87 commit a0428f9

1 file changed

Lines changed: 155 additions & 18 deletions

File tree

  • programs/2016/C/01-No_Time_for_a_Taxicab

programs/2016/C/01-No_Time_for_a_Taxicab/program.c

Lines changed: 155 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,97 @@
33
#include <stdlib.h>
44
#include <string.h>
55
#include <stdbool.h>
6+
#include <time.h>
7+
#include <math.h>
8+
9+
10+
// STRUCTURES:
11+
// todo: describe here
12+
typedef struct Instruction {
13+
char direction ; // "L" or "R"
14+
unsigned int distance ;
15+
16+
} Instruction ;
17+
18+
19+
// todo: describe here
20+
typedef struct Position {
21+
int x ;
22+
int y ;
23+
char facingDirection ; // 'N', 'E', 'S', 'W'
24+
25+
} Position ;
26+
27+
28+
/* FUNC: setDefaultPos
29+
// TODO : complete the description here
30+
A kind of constructor, in a sense. :)
31+
*/
32+
Position setDefaultPos( ) {
33+
34+
// Initialization:
35+
Position pos ;
36+
37+
// Values:
38+
pos.x = 0 ;
39+
pos.y = 0 ;
40+
pos.facingDirection = 'N' ;
41+
42+
return pos ;
43+
}
44+
45+
46+
47+
/* FUNC: tokenCounter
48+
// TODO : complete the description here
49+
50+
*/
51+
int tokenCounter(const char* stringInput) {
52+
53+
char scrutinizedLetter = stringInput[0];
54+
int tokenCounter = 1; // We suppose the input has at least one element
55+
int i = 0;
56+
57+
while(scrutinizedLetter != '\0') {
58+
59+
scrutinizedLetter = stringInput[i];
60+
61+
if(scrutinizedLetter == ',') {
62+
tokenCounter++;
63+
}
64+
65+
// End of loop
66+
i++;
67+
}
68+
69+
return tokenCounter;
70+
}
71+
672

773

874
int main() {
975

1076
// --- MAIN VARIABLES:
11-
static bool debug_mode = false;
77+
static bool debug_mode = true;
1278
int i;
79+
80+
int tokenAmount = 0 ;
81+
unsigned int inputTokenLength ;
82+
unsigned int tokenDistanceCalculation ;
83+
84+
85+
// RNG
86+
srand(time(NULL));
87+
int randomValue ;
88+
1389

14-
// --- TEXT CONVERSION:
15-
// I convert the input file from raw text to usable list input.
16-
// This is mandatory for now, as I'm using an online IDE
17-
// (which doesn't allow to upload files in a workspace)...
90+
/* --- TEXT CONVERSION:
91+
I convert the input file from raw text to usable list input.
92+
This is mandatory for now, as I'm using an online IDE
93+
(which doesn't allow to upload files in a workspace)...
94+
LINK:
95+
https://www.online-cpp.com/
96+
*/
1897

1998
char* rawInput = "L4, L3, R1, L4, R2, R2, L1, L2, R1, R1, L3, R5, L2, R5, L4, L3, R2, R2, L5, L1, R4, L1, R3, L3, R5, R2, L5, R2, R1, R1, L5, R1, L3, L2, L5, R4, R4, L2, L1, L1, R1, R1, L185, R4, L1, L1, R5, R1, L1, L3, L2, L1, R2, R2, R2, L1, L1, R4, R5, R53, L1, R1, R78, R3, R4, L1, R5, L1, L4, R3, R3, L3, L3, R191, R4, R1, L4, L1, R3, L1, L2, R3, R2, R4, R5, R5, L3, L5, R2, R3, L1, L1, L3, R1, R4, R1, R3, R4, R4, R4, R5, R2, L5, R1, R2, R5, L3, L4, R1, L5, R1, L4, L3, R5, R5, L3, L4, L4, R2, R2, L5, R3, R1, R2, R5, L5, L3, R4, L5, R5, L3, R1, L1, R4, R4, L3, R2, R5, R1, R2, L1, R4, R1, L3, L3, L5, R2, R5, L1, L4, R3, R3, L3, R2, L5, R1, R3, L3, R2, L1, R4, R3, L4, R5, L2, L2, R5, R1, R2, L4, L4, L5, R3, L4";
2099
size_t inputLength = strlen(rawInput);
@@ -25,27 +104,85 @@ int main() {
25104
}
26105

27106
// Transforming the raw input into proper data
28-
char* rawInputBuffer = malloc( sizeof(char) * (inputLength+1) );
29-
strcpy(rawInputBuffer, rawInput) ;
107+
char* rawInputBuffer = malloc(sizeof(char) * (inputLength+1));
108+
strcpy(rawInputBuffer, rawInput);
30109

31-
char** input = malloc( sizeof(char*) * inputLength ) ;
32-
static int loopCounter = 0 ;
110+
// Getting the amount of space we need in the input array:
111+
tokenAmount = tokenCounter( rawInputBuffer ) ;
112+
if( debug_mode ) { printf("\ntokenAmount = %d\n", tokenAmount ) ; }
33113

34114

115+
// Then, setting up the space for it:
116+
Instruction* input = malloc(sizeof(Instruction*) * tokenAmount);
117+
118+
119+
static int loopCounter = 0;
120+
121+
35122
// Tokenizing the input here:
36-
char *inputToken = strtok(rawInput, ", ");
123+
char *inputToken = strtok(rawInputBuffer, ", ");
124+
125+
while(inputToken != NULL) {
126+
127+
// 0. Analyzing the inputToken:
128+
inputTokenLength = strlen(inputToken);
129+
130+
// 1. Direction
131+
input[loopCounter].direction = inputToken[0] ;
132+
133+
// 2. Distance
134+
tokenDistanceCalculation = 0;
135+
136+
for(i=1; i<inputTokenLength; i++){
137+
tokenDistanceCalculation = tokenDistanceCalculation*10 + (inputToken[i] - '0') ;
138+
}
139+
input[loopCounter].distance = tokenDistanceCalculation;
140+
141+
// 3. Getting ready for next loop
142+
inputToken = strtok(NULL, ", "); // We keep tokenizing the same STRING, with the "NULL" variable.
143+
loopCounter++;
144+
}
145+
146+
// Displaying the result of a random variable:
147+
if(debug_mode){
148+
randomValue = rand() % (loopCounter+1) ;
149+
150+
printf( "\n\n*- %dst input:\n", randomValue+1);
151+
printf( " direction = %c\n", input[randomValue].direction ) ;
152+
printf( " distance = %u\n\n", input[randomValue].distance ) ;
153+
}
37154

38-
while( inputToken != NULL ) {
39-
input[loopCounter] = inputToken ;
40-
inputToken = strtok(NULL, ", ") ; // We keep tokenizing the same STRING, with the "NULL" variable.
155+
156+
157+
/* --- DATA INTERPRETATION:
158+
We set our default position to North (N), and read the input step by step
159+
while turning Left (L) or Right (R) and going forward to a set distance.
160+
161+
Then, it's only a matter of calculing the taxicab distance while being by
162+
default on (0,0)... thus adding the two x+y coordinates.
163+
*/
164+
165+
Position gridPosition = setDefaultPos() ;
166+
const int orderLength = loopCounter ;
167+
168+
// Direction:
169+
int directionMapping[256] = {O} ; // We set a table large enough that contains all ASCII usual letters.
170+
directionMapping['N'] = 1 ;
171+
directionMapping['E'] = 2 ;
172+
directionMapping['S'] = 3 ;
173+
directionMapping['W'] = 4 ;
174+
175+
176+
177+
for(i=0; i<orderLength; i++){
178+
// 1. Pole facing update
179+
41180

42-
loopCounter++ ;
181+
// 2. Distance calculation
43182
}
44183

45-
46-
47-
48-
184+
// 3. Overall distance calculation (from the default position)
185+
49186

50187
return 0;
51188

0 commit comments

Comments
 (0)