-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.c
More file actions
119 lines (100 loc) · 4.13 KB
/
Copy pathsolution.c
File metadata and controls
119 lines (100 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
#define PERMS 0644
#define MAX_TRUCKS 250
#define TRUCK_MAX_CAP 20
#define MAX_NEW_REQUESTS 50
#define MAX_TOTAL_PACKAGES 5000
// ----------------------
// Represents a package request
// ----------------------
typedef struct PackageRequest {
int packageId; // Unique identifier for a package request
int pickup_x; // X coordinate of the cell at which the request has arrived
int pickup_y; // Y coordinate of the cell at which the request has arrived
int dropoff_x; // X coordinate of the cell at which the package has to be dropped
int dropoff_y; // Y coordinate of the cell at which the package has to be dropped
int arrival_turn; // The turn at which the request has arrived
int expiry_turn; // The turn at which the request will expire
} PackageRequest;
// ----------------------
// Shared Memory Structure
// ----------------------
typedef struct MainSharedMemory {
char authStrings[MAX_TRUCKS][TRUCK_MAX_CAP + 1]; // ith element is auth string for truck i
char truckMovementInstructions[MAX_TRUCKS]; // ith element is the movement instruction for truck i
int pickUpCommands[MAX_TRUCKS]; // package id to be picked up by ith truck, -1 otherwise
int dropOffCommands[MAX_TRUCKS]; // package id to be dropped off by ith truck, -1 otherwise
int truckPositions[MAX_TRUCKS][2]; // ith element is the current cell (x, y) of truck i
int truckPackageCount[MAX_TRUCKS]; // ith element is the count of packages carried by truck i
int truckTurnsInToll[MAX_TRUCKS]; // ith element is the number of turns truck i is in a toll cell, 0 otherwise
PackageRequest newPackageRequests[MAX_NEW_REQUESTS]; // New requests for this turn
int packageLocations[MAX_TOTAL_PACKAGES][2]; // (x, y) of the packages, (-1, -1) otherwise
} MainSharedMemory;
// ----------------------
// Helper to Student: Signals new turn state is ready in SHM
// ----------------------
typedef struct TurnChangeResponse {
long mtype; // fixed as 2
int turnNumber; // the current turn (starts as 1)
int newPackageRequestCount; // count of new packages that have arrived this turn
int errorOccured; // set as 1 if error
int finished; // set as 1 if all requests have been fulfilled
} TurnChangeResponse;
// Student to Solver
typedef struct SolverRequest {
long mtype;
int truckNumber;
char authStringGuess[TRUCK_MAX_CAP + 1];
} SolverRequest;
// Solver to Student
typedef struct SolverResponse {
long mtype; // set to 4
int guessIsCorrect; // 1 if correct, 0 if incorrect
} SolverResponse;
void generate_random_string(int number, int length, char* guess_string) {
//u - 0, l-1, d-2, r-3
//uu...u(all zeroes) to rr...r(all threes)
//convert 'number' to its base 4 representation and generate guess_string from that
guess_string[length] = '\0';
for(int i=length-1; i>=0; i--) {
int digit = number%4;
if(digit==0)guess_string[i] = 'u';
else if(digit==1)guess_string[i] = 'l';
else if(digit == 2)guess_string[i] = 'd';
else guess_string[i] = 'r';
number/=4;
}
}
int main() {
FILE* file_ptr = file_ptr = fopen("input.txt", "r");
int N;
int D;
int S;
int T;
int B;
key_t shm_key;
key_t main_mq_key;
fscanf(file_ptr, "%d", &N);
fscanf(file_ptr, "%d", &D);
fscanf(file_ptr, "%d", &S);
fscanf(file_ptr, "%d", &T);
fscanf(file_ptr, "%d", &B);
fscanf(file_ptr, "%ld", &shm_key);
fscanf(file_ptr, "%ld", &main_mq_key);
key_t *solver_mq_keys = (key_t *)malloc(S * sizeof(key_t));
for(int i=0; i < S; i++) {
fscanf(file_ptr, "%ld", &solver_mq_keys[i]);
}
fclose(file_ptr);
MainSharedMemory *mainShmPtr;
shmget(shm_key, sizeof(MainSharedMemory), PERMS);
return 0;
}