GC4XMN7 RR138 – 404 Found
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
It’s been a few weeks since I found a puzzle worth programming in R, so I turned back to the RR series in the east of England to see if there was another, and there is.
The cache page shows a quite unilluminating image, mostly white with a couple of small characters. Opening the image in a new tab, I could see the image was similar but different. Ok, so this is one of those caches where PHP is used to serve up an image that changes. Sometimes these can be tricky, involving long waits, or lots of gathering information before you can even see the extent of the puzzle. But I suspected this one was more straightforward: it would be just a case of gathering a bunch of the images and overlaying them on each other to show the letters all in single frame. I shudder to think how I would go about this without R, so let’s get coding:
library(caTools) library(abind) library(magrittr) URL <- "http://infin.ity.me.uk/MyCaches/RR138/RR138.php" dr <- "C:/Users/alunh/OneDrive/Documents/Repos/geo2/Solving/GC4XMN7_rr138-404-found/" for (q in 1:80) { tid <- as.character(round(as.numeric(Sys.time())*1000, 0)) download.file(URL, destfile = paste0(dr, tid, ".gif"), mode = 'wb') } # download 80 versions of the image lapply( list.files(dr, full.names = TRUE, pattern="gif"), function(q) { read.gif(filename = q)$image } ) %>% c(list(along=3)) %>% do.call(what=abind) %>% apply(1:2, max) %>% # every time a non-black pixel is found, preserve it apply(1, rev) %>% # this is a lazy way of rotating the matrix so it image()s the right way up apply(1, rev) %>% # this is a lazy way of rotating the matrix so it image()s the right way up apply(1, rev) %>% # this is a lazy way of rotating the matrix so it image()s the right way up image(col=c("black", "white"))
The image shows the partial result of the plot (I’ve kept it partial to avoid spoilers), but the pattern is clear: a cross of letters, spelling out the north coordinates vertically and the east coordinates horizontally. Job done!
It took a little searching to remind myself how to read in GIFs — the caTools library is the answer. It’s a slight oddity that there isn’t a package called gif, analogous to the jpeg and png packages, but there’s no gap in the functionality.
Also putting in a welcome appearance is one of my favourite R tricks, rotating a matrix using the simplest of commands: apply(m, 1, rev)
. I don’t mind admitting I get a frisson of pleasure whenever I get to use that, because it’s so elegant.
Last time, I mentioned that I “discovered” a new integer sequence. After a length submission and review process, it was found that I hadn’t, in fact. I’d just found an existing one and put a slightly odd twist on it. I withdrew my submission, but my thanks go to the editors of oeis.org for their patience and diligence in dealing with my attempt to get my name into that most nerdy hall of fame. One day.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.