One interesting bit is that, as often, most of the code on those simple challenges is spent on input parsing. In this case, in day 2 challenge, the input had a form as follows:
1-2 a: ahgtnkjz
3-12 j: asjdladajsdsjdaslj
Obviously, given the inconsistent column separator, the easiest here was to use a tailored regex. So I whipped up that ol' function from the help file of ?regexpr
that I remembered from an old StackOverflow answer of mine:
input <- readLines("input02.txt")
parse.one <- function(res, result) {
m <- do.call(rbind, lapply(seq_along(res), function(i) {
if(result[i] == -1) return("")
st <- attr(result, "capture.start")[i, ]
substring(res[i], st, st + attr(result, "capture.length")[i, ] - 1)
}))
colnames(m) <- attr(result, "capture.names")
m
}
parsed <- regexpr("^(?<lb>[0-9]+)-(?<ub>[0-9]+) (?<let>[a-z]+): (?<p>[a-z]+)$", input, perl=TRUE)
tab <- parse.one(input,parsed)
tab <- as.data.frame(tab)
What parse.one
does is just a modification of regmatches
that allows the capture of "named" group, PERL-style; i. e. it allows me to use that weird regex trick of (?<name>group)
. Once done and transformed into a data.frame, the columns are already named correctly and are instantly usable.
In other news, I've been cooking buckwheat pancakes for breakfast/lunch recently, and I am quite happy with the result. To 100g of buckwheat flour, I added an egg, 20cL of milk, 10cL of water, salt, pepper and a bit of dill. Once whipped up to a flowing but thick mixture, a small laddleful of it is placed at the center of a very hot pan. When bubbles appeared on the surface, flip and let cook another 30s/1min.