Skip to content

Instantly share code, notes, and snippets.

@JustinChristensen
Created December 8, 2021 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinChristensen/6136e41192fa0f98455701e4d4dabea5 to your computer and use it in GitHub Desktop.
Save JustinChristensen/6136e41192fa0f98455701e4d4dabea5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
enum state {
LOCKED,
UNLOCKED
};
// 'c' for coin
// 'p' for push
enum state transition(char c, enum state current_state) {
switch (current_state) {
case LOCKED:
if (c == 'c') return UNLOCKED;
else if (c == 'p') return LOCKED;
break;
case UNLOCKED:
if (c == 'c') return UNLOCKED;
else if (c == 'p') return LOCKED;
break;
}
}
int main() {
enum state current_state = LOCKED;
char c;
while ((c = getc(stdin)) == 'c' || c == 'p') {
current_state = transition(c, current_state);
printf("%c %s\n", c, current_state == LOCKED ? "LOCKED" : "UNLOCKED");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment