-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
160 lines (149 loc) · 4.6 KB
/
main.c
File metadata and controls
160 lines (149 loc) · 4.6 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Create our "stack" datatype
struct stack {
int height;
int max_height;
char* crate;
};
// Create our pop function
char pop(struct stack* curr) {
char output = 0;
if (curr->height) {
output = curr->crate[curr->height-1];
curr->crate[curr->height-1] = 0;
curr->height = curr->height - 1;
}
return output;
}
// Create our push function
int push(struct stack* curr, char new) {
if (curr->height == curr->max_height) {
return EXIT_FAILURE;
}
curr->crate[curr->height] = new;
curr->height = curr->height + 1;
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
// Read in our input file
FILE *in_file = fopen("input.txt", "r");
if (in_file == NULL) {
printf("Could not open input file!");
exit(-1);
}
// Declare our stack vars
struct stack* crates_1;
struct stack* crates_2;
char buffer[100];
char marker[100];
// For part two we need a buffer to keep our chars in
char* crate_2_buffer;
int line_length = 0;
int init_crates = 0;
int num_crate_in_line = 1;
int max_num_crate = 0;
int max_height_stack = 0;
int num_to_move = 0;
int move_from = 0;
int move_to = 0;
// Iterate over the file a line at a time
while(fgets(buffer, sizeof(buffer), in_file) != NULL && num_crate_in_line) {
num_crate_in_line = 0;
// Get the number of crates per line
for (int i=0; i<strlen(buffer); i++) {
if (buffer[i] == '[') {
num_crate_in_line = num_crate_in_line + 1;
}
}
// Increment our variables
if (num_crate_in_line) {
max_num_crate = num_crate_in_line;
max_height_stack = max_height_stack + 1;
}
else {
// If we're done, copy the last line into marker
// since this contains our width definitions
strcpy(marker, buffer);
}
}
// Debug output
printf("Max num crate: %d Max height stack: %d\n", max_num_crate, max_height_stack*max_num_crate);
// Initialize our crates
crates_1 = (struct stack*)malloc(max_num_crate*sizeof(struct stack));
crates_2 = (struct stack*)malloc(max_num_crate*sizeof(struct stack));
for (int i=0; i<max_num_crate; i=i+1) {
crates_1[i].max_height = max_height_stack*max_num_crate;
crates_1[i].height = 0;
crates_1[i].crate = (char*)malloc(crates_1[i].max_height*sizeof(char));
crates_2[i].max_height = max_height_stack*max_num_crate;
crates_2[i].height = 0;
crates_2[i].crate = (char*)malloc(crates_2[i].max_height*sizeof(char));
}
crate_2_buffer = (char*)malloc(crates_2[0].max_height*sizeof(char));
// Start moving back up the file through the crates
line_length = strlen(marker);
fseek(in_file, -1, SEEK_CUR);
fseek(in_file, -2*line_length, SEEK_CUR);
// Actually initialize our crates
for (int i=0; i<max_height_stack; i=i+1) {
// Go one line at a time
if(fgets(buffer, sizeof(buffer), in_file) != NULL) {
// For each line iterate a char at a time
for (int j=0; j<line_length-1; j=j+1) {
// Check if the marker exists at this spot
// and also check if a crate exists
if (marker[j] != 0x20 && buffer[j] != 0x20) {
// If it does, the crate is one less than the marker
// and the value is simply at the current index
push(&crates_1[marker[j] - '0' - 1], buffer[j]);
push(&crates_2[marker[j] - '0' - 1], buffer[j]);
}
}
// Move up two lines
fseek(in_file, -2*line_length, SEEK_CUR);
}
}
// Seek back to the end of the crates
fseek(in_file, (line_length*max_height_stack)+1, SEEK_CUR);
// Grab each instructioon
while(fscanf(in_file, "move %d from %d to %d\n", &num_to_move, &move_from, &move_to) == 3) {
// Iterate over the number of moves
for (int i=0; i<num_to_move; i=i+1) {
// For part one this is simple, for every move, simply pop from
// one crate and push to the other
push(&crates_1[move_to-1], pop(&crates_1[move_from-1]));
// For part two, its a little more complex, we need to pop
// and store these in a buffer for the time being
crate_2_buffer[i] = pop(&crates_2[move_from-1]);
}
// For part two we need to iterate back over our moves
for (int i=0; i<num_to_move; i=i+1) {
// And for each push the crate values but backwards
push(&crates_2[move_to-1], crate_2_buffer[num_to_move-i-1]);
}
}
// Print out the tops of each stack
printf("Top of each stack one: ");
for (int i=0; i<max_num_crate; i=i+1) {
printf("%c", pop(&crates_1[i]));
}
printf("\n");
printf("Top of each stack two: ");
for (int i=0; i<max_num_crate; i=i+1) {
printf("%c", pop(&crates_2[i]));
}
printf("\n");
// Free our memory
free(crate_2_buffer);
for (int i=0; i<max_num_crate; i=i+1) {
free(crates_1[i].crate);
free(crates_2[i].crate);
}
free(crates_1);
free(crates_2);
fclose(in_file);
return EXIT_SUCCESS;
}