Skip to content

Instantly share code, notes, and snippets.

@dwhdai
Created October 4, 2019 00:46
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 dwhdai/d98dc1955f774c1601ca8324872c00e0 to your computer and use it in GitHub Desktop.
Save dwhdai/d98dc1955f774c1601ca8324872c00e0 to your computer and use it in GitHub Desktop.
advent of code 1
captcha <- function(x){
#input 1122
#return 3
#input 1111
#return 4
first_digit <- substr(x,1,1)
x <- paste0(x, first_digit)
t <- vector(mode = "character")
for (i in 1:(nchar(x)-1)){
current_digit <- substr(x,i,i)
next_digit <- substr(x, i+1, i+1)
if (current_digit == next_digit){
t <- append(t, next_digit)
}
}
nums_to_add <- as.numeric(t)
return(sum(nums_to_add))
}
@dwhdai
Copy link
Author

dwhdai commented Oct 4, 2019

x is the input stored as a character, since R has a maximum value for numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment