Last active
April 3, 2016 18:45
-
-
Save primaryobjects/905aa0f7ad414694c62ed10d4789e2e0 to your computer and use it in GitHub Desktop.
[2016-03-28] Challenge #260 [Easy] Garage Door Opener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # [2016-03-28] Challenge #260 [Easy] Garage Door Opener | |
| # https://www.reddit.com/r/dailyprogrammer/comments/4cb7eh/20160328_challenge_260_easy_garage_door_opener/ | |
| # Demo at http://www.r-fiddle.org/#/fiddle?id=83RtwcPP&version=1 | |
| # | |
| # Kory Becker 4/3/2016 | |
| # http://primaryobjects.com | |
| # | |
| # Valid states and commands. | |
| states <- c(CLOSED = 0, OPEN = 1, OPENING = 2, CLOSING = 3, STOPPED_WHILE_OPENING = 4, STOPPED_WHILE_CLOSING = 5, OPEN_BLOCKED = 6, EMERGENCY_OPENING = 7) | |
| commands <- c(button_clicked = 0, cycle_complete = 1, block_detected = 2, block_cleared = 3) | |
| input <- c( | |
| 'button_clicked', | |
| 'cycle_complete', | |
| 'button_clicked', | |
| 'button_clicked', | |
| 'button_clicked', | |
| 'button_clicked', | |
| 'button_clicked', | |
| 'cycle_complete' | |
| ) | |
| inputBonus <- c( | |
| 'button_clicked', | |
| 'cycle_complete', | |
| 'button_clicked', | |
| 'block_detected', | |
| 'button_clicked', | |
| 'cycle_complete', | |
| 'button_clicked', | |
| 'block_cleared', | |
| 'button_clicked', | |
| 'cycle_complete' | |
| ) | |
| click <- function(state) { | |
| if (state == states['CLOSED']) { | |
| state <- states['OPENING'] | |
| } | |
| else if (state == states['OPEN']) { | |
| state <- states['CLOSING'] | |
| } | |
| else if (state == states['CLOSING']) { | |
| state <- states['STOPPED_WHILE_CLOSING'] | |
| } | |
| else if (state == states['OPENING']) { | |
| state <- states['STOPPED_WHILE_OPENING'] | |
| } | |
| else if (state == states['STOPPED_WHILE_CLOSING']) { | |
| state <- states['OPENING'] | |
| } | |
| else if (state == states['STOPPED_WHILE_OPENING']) { | |
| state <- states['CLOSING'] | |
| } | |
| state | |
| } | |
| complete <- function(state) { | |
| if (state == states['CLOSING']) { | |
| state <- states['CLOSED'] | |
| } | |
| else if (state == states['OPENING']) { | |
| state <- states['OPEN'] | |
| } | |
| else if (state == states['EMERGENCY_OPENING']) { | |
| state <- states['OPEN_BLOCKED'] | |
| } | |
| state | |
| } | |
| block <- function(state) { | |
| if (state == states['CLOSING']) { | |
| state <- states['EMERGENCY_OPENING'] | |
| } | |
| state | |
| } | |
| clear <- function(state) { | |
| if (state == states['OPEN_BLOCKED']) { | |
| state <- states['OPEN'] | |
| } | |
| state | |
| } | |
| format <- function(command) { | |
| text <- NA | |
| if (command == names(commands['button_clicked'])) { | |
| text <- 'Button clicked.' | |
| } | |
| else if (command == names(commands['cycle_complete'])) { | |
| text <- 'Cycle complete.' | |
| } | |
| else if (command == names(commands['block_detected'])) { | |
| text <- 'Block detected!' | |
| } | |
| else if (command == names(commands['block_cleared'])) { | |
| text <- 'Block cleared.' | |
| } | |
| text | |
| } | |
| # Main loop. | |
| run <- function(state, input) { | |
| # Print starting state. | |
| print(paste('Door:', names(state))) | |
| n <- sapply(input, function(command) { | |
| # Display command. | |
| print(paste('>', format(command))) | |
| # Transition to next state. | |
| if (commands[command] == commands['button_clicked']) { | |
| state <<- click(state) | |
| } | |
| else if (commands[command] == commands['cycle_complete']) { | |
| state <<- complete(state) | |
| } | |
| else if (commands[command] == commands['block_detected']) { | |
| state <<- block(state) | |
| } | |
| else if (commands[command] == commands['block_cleared']) { | |
| state <<- clear(state) | |
| } | |
| # Display current state. | |
| print(paste('Door:', names(state))) | |
| }) | |
| } | |
| run(states['CLOSED'], input) | |
| run(states['CLOSED'], inputBonus) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > run(states['CLOSED'], input) | |
| [1] "Door: CLOSED" | |
| [1] "> Button clicked." | |
| [1] "Door: OPENING" | |
| [1] "> Cycle complete." | |
| [1] "Door: OPEN" | |
| [1] "> Button clicked." | |
| [1] "Door: CLOSING" | |
| [1] "> Button clicked." | |
| [1] "Door: STOPPED_WHILE_CLOSING" | |
| [1] "> Button clicked." | |
| [1] "Door: OPENING" | |
| [1] "> Button clicked." | |
| [1] "Door: STOPPED_WHILE_OPENING" | |
| [1] "> Button clicked." | |
| [1] "Door: CLOSING" | |
| [1] "> Cycle complete." | |
| [1] "Door: CLOSED" | |
| > run(states['CLOSED'], inputBonus) | |
| [1] "Door: CLOSED" | |
| [1] "> Button clicked." | |
| [1] "Door: OPENING" | |
| [1] "> Cycle complete." | |
| [1] "Door: OPEN" | |
| [1] "> Button clicked." | |
| [1] "Door: CLOSING" | |
| [1] "> Block detected!" | |
| [1] "Door: EMERGENCY_OPENING" | |
| [1] "> Button clicked." | |
| [1] "Door: EMERGENCY_OPENING" | |
| [1] "> Cycle complete." | |
| [1] "Door: OPEN_BLOCKED" | |
| [1] "> Button clicked." | |
| [1] "Door: OPEN_BLOCKED" | |
| [1] "> Block cleared." | |
| [1] "Door: OPEN" | |
| [1] "> Button clicked." | |
| [1] "Door: CLOSING" | |
| [1] "> Cycle complete." | |
| [1] "Door: CLOSED" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment