commit
e6949f3ff5
File diff suppressed because one or more lines are too long
@ -0,0 +1,2 @@
|
|||||||
|
compile:
|
||||||
|
gcc initialprog.c -Werror=switch -o initialprog
|
@ -0,0 +1,5 @@
|
|||||||
|
Advent of Code 2015 Day 1
|
||||||
|
Completed 10/2/2025
|
||||||
|
It may be that Verilog state machines followed me home to do this
|
||||||
|
I need to set up some tests on it still
|
||||||
|
Reference on makefiles: https://makefiletutorial.com/
|
Binary file not shown.
@ -0,0 +1,88 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<string.h>
|
||||||
|
FILE *fptr;
|
||||||
|
char line1[10000];
|
||||||
|
int floor_number;
|
||||||
|
enum states{
|
||||||
|
START,
|
||||||
|
PROCESS,
|
||||||
|
END,
|
||||||
|
} state;
|
||||||
|
|
||||||
|
enum events{
|
||||||
|
START_LOOP,
|
||||||
|
INCREMENT,
|
||||||
|
DECREMENT,
|
||||||
|
STOP_LOOP,
|
||||||
|
ERROR_EVENT
|
||||||
|
};
|
||||||
|
|
||||||
|
void step_state(enum events event){
|
||||||
|
switch(state){
|
||||||
|
case START:
|
||||||
|
switch(event){
|
||||||
|
case START_LOOP:
|
||||||
|
state = PROCESS;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
exit(1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PROCESS:
|
||||||
|
switch(event){
|
||||||
|
case INCREMENT:
|
||||||
|
floor_number++;
|
||||||
|
break;
|
||||||
|
case DECREMENT:
|
||||||
|
floor_number--;
|
||||||
|
break;
|
||||||
|
case STOP_LOOP:
|
||||||
|
state = END;
|
||||||
|
break;
|
||||||
|
case ERROR_EVENT:
|
||||||
|
printf("ERROR!");
|
||||||
|
exit(1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
exit(1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
floor_number = 0;
|
||||||
|
fptr = fopen("2015_day_1_input.txt", "r");
|
||||||
|
if(fptr == NULL) {
|
||||||
|
printf("Not able to open the file.");
|
||||||
|
}
|
||||||
|
fgets(line1, 10000, fptr);
|
||||||
|
fclose(fptr);
|
||||||
|
step_state(START_LOOP);
|
||||||
|
size_t i = 0;
|
||||||
|
while (line1[i] != '\0'){
|
||||||
|
if (line1[i] == '('){
|
||||||
|
step_state(INCREMENT);
|
||||||
|
}
|
||||||
|
else if (line1[i] == ')'){
|
||||||
|
step_state(DECREMENT);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("\nCharacter not ( or ), ERROR!");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nThe Floor number is: %d\n\n", floor_number);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Sources:
|
||||||
|
//https://dev.to/zirkelc/how-to-iterate-over-c-string-lcj#:~:text=Pointer-,For%2DLoop,each%20iteration%20with%20*%2B%2Bp%20.
|
||||||
|
//https://yakking.branchable.com/posts/state-machines-in-c/
|
Loading…
Reference in new issue