Created
April 4, 2016 02:01
-
-
Save primaryobjects/7c4cdfac282d6778735a26ad811cf107 to your computer and use it in GitHub Desktop.
[2016-03-21] Challenge #259 [Easy] Clarence the Slow Typist
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-21] Challenge #259 [Easy] Clarence the Slow Typist | |
| # https://www.reddit.com/r/dailyprogrammer/comments/4bc3el/20160321_challenge_259_easy_clarence_the_slow/ | |
| # Demo at http://www.r-fiddle.org/#/fiddle?id=JwY7VC2k&version=1 | |
| # | |
| # Kory Becker 4/3/2016 | |
| # http://primaryobjects.com | |
| # | |
| keypad <- data.frame(c(1, 4, 7, 10), | |
| c(2, 5, 8, 0), | |
| c(3, 6, 9, NA)) | |
| getRow <- function(target) { | |
| # Find the row of the digit within the keypad. | |
| unlist(sapply(seq(nrow(keypad)), function(rowIndex) { | |
| row <- keypad[rowIndex,] | |
| result <- sapply(row, function(digit) { | |
| target == digit | |
| }) | |
| if (length(which(result)) > 0) { | |
| rowIndex | |
| } | |
| })) | |
| } | |
| getCol <- function(target) { | |
| # Find the column of the digit within the keypad. | |
| unlist(sapply(seq(ncol(keypad)), function(colIndex) { | |
| col <- keypad[,colIndex] | |
| result <- sapply(col, function(digit) { | |
| target == digit | |
| }) | |
| if (length(which(result)) > 0) { | |
| colIndex | |
| } | |
| })) | |
| } | |
| cost <- function(input) { | |
| # Remember previous location. | |
| prev <- c(-1, -1) | |
| # Calculate all costs for each move along the string. | |
| parts <- sapply(unlist(strsplit(input, '')), function(digit) { | |
| # Convert the character into a digit. | |
| if (digit == '.') { | |
| digit <- 10 | |
| } | |
| else { | |
| digit <- as.numeric(digit) | |
| } | |
| # Get the (col, row). | |
| coord <- c(getCol(digit), getRow(digit)) | |
| # Calculate distance. | |
| if (prev[1] == -1) { | |
| # First time, cost is 0. | |
| result <- 0 | |
| } | |
| else if (prev[1] == coord[1] || prev[2] == coord[2]) { | |
| # Same row or column, cost is the difference in row or col. | |
| result <- abs(prev[1] - coord[1]) + abs(prev[2] - coord[2]) | |
| } | |
| else { | |
| # Diagonal, cost is calculated with pythagoras theorem, where a = row, b = col. | |
| a <- abs(prev[1] - coord[1]) | |
| b <- abs(prev[2] - coord[2]) | |
| result <- sqrt((a^2) + (b^2)) | |
| } | |
| prev <<- coord | |
| result | |
| }) | |
| sum(parts) | |
| } | |
| format <- function(result) { | |
| paste0(round(result, 2), 'cm') | |
| } | |
| run <- function(input) { | |
| format(cost(input)) | |
| } | |
| # Challenge input. | |
| run('219.45.143.143') |
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('219.45.143.143') | |
| [1] "27.38cm" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment