This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathE_kevin_k_biju.c
126 lines (116 loc) · 4.28 KB
/
E_kevin_k_biju.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
// An implementation of stack in C, using realloc() tied to a struct.
struct stack
{
int64_t* arrayptr;
int64_t element_count;
};
struct stack stack_create()
{
struct stack temp = { .arrayptr = NULL, .element_count = 0 };
return temp;
}
void stack_push(struct stack* stack, int64_t element)
{
stack->element_count = stack->element_count + 1;
stack->arrayptr = realloc(stack->arrayptr, sizeof(int64_t) * stack->element_count);
for(int64_t i = 0; i < stack->element_count - 1; i++)
{
(stack->arrayptr)[i + 1] = (stack->arrayptr)[i];
}
(stack->arrayptr)[0] = element;
}
void stack_pop(struct stack* stack)
{
for(int64_t i = 1; i < stack->element_count; i++)
{
(stack->arrayptr)[i - 1] = (stack->arrayptr)[i];
}
stack->element_count = stack->element_count - 1;
stack->arrayptr = realloc(stack->arrayptr, sizeof(int64_t) * stack->element_count);
}
void stack_free(struct stack* stack)
{
if(stack->arrayptr != NULL)
{
free(stack->arrayptr);
stack->arrayptr = NULL;
}
}
// The main issue the parser faces is to account for nested loops, since an outer loop depends on the
// complete processing of an inner loop to evaluate correctly. The main elements to note of is:
//
// loop_count_stack -> stores the number of times each loop has to be repeated.
// loop_store_stack -> stores the "offset" of the turtle for each iteration of the loop.
// After a loop finishes evaluating, the offset is multiplied with the loop count and added
// to either the next level of loop or directly to the turtle's position.
//
// When the parser finds an "END" instruction, one level of loop has ended. So the corresponding values
// are popped from the stack. If the stack only has one element, it is the outermost level of loop and we
// can now operate directly on the turtle's current position. The in_loop boolean tracks this.
int main(void)
{
// int64_t is guaranteed to be a 64-bit signed integer, is an alias for long on most 64-bit x86 machines.
int64_t line_count;
scanf("%ld\n", &line_count);
char instruction[33];
bool in_loop = false;
int64_t current_position = 0;
struct stack loop_count_stack = stack_create();
struct stack loop_store_stack = stack_create();
int64_t withdrawn_loop_count;
int64_t withdrawn_loop_store;
for(int64_t i = 0; i < line_count; i++)
{
// we use fgets() instead of the traditional scanf() here because scanf() stops reading
// when it encounters a space.
// We use the first letter of the instruction to distinguish if it is a "FD", "LOOP" or "END"
// instruction. strtoll() is used for passing the integer part of the integer.
fgets(instruction, 33, stdin);
if(instruction[0] == 'F')
{
if(in_loop == true)
{
(loop_store_stack.arrayptr)[0] = (loop_store_stack.arrayptr)[0]
+ strtoll(instruction + 3, NULL, 10);
}
else if(in_loop == false)
{
current_position = current_position + strtoll(instruction + 3, NULL, 10);
}
}
else if(instruction[0] == 'L')
{
stack_push(&loop_count_stack, strtoll(instruction + 5, NULL, 10));
stack_push(&loop_store_stack, 0);
in_loop = true;
}
else if(instruction[0] == 'E')
{
withdrawn_loop_count = (loop_count_stack.arrayptr)[0];
withdrawn_loop_store = (loop_store_stack.arrayptr)[0];
stack_pop(&loop_count_stack);
stack_pop(&loop_store_stack);
if(loop_count_stack.element_count == 0)
{
current_position = current_position + (withdrawn_loop_count * withdrawn_loop_store);
in_loop = false;
}
else if(loop_count_stack.element_count != 0)
{
(loop_store_stack.arrayptr)[0] = (loop_store_stack.arrayptr)[0]
+ (withdrawn_loop_count * withdrawn_loop_store);
}
}
}
printf("%ld", current_position);
fflush(stdout);
stack_free(&loop_count_stack);
stack_free(&loop_store_stack);
return 0;
}