?

Log in

No account? Create an account
R Advent: Days 6-10 - The Mad Schemes of Dr. Tectonic [entries|archive|friends|userinfo]
Beemer

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

R Advent: Days 6-10 [Feb. 26th, 2016|08:25 am]
Beemer
Days 6-10 start to expose some interesting language features.

Day 6 [LOC: 15/11]
Puzzle: http://adventofcode.com/day/6
Solution: https://github.com/sethmcg/advent/blob/master/day6.R

R lets you subset arrays using a vector of indices. This very handy, and it interacts with the ":" (sequence) operator in a really intuitive way. Which is all great if you want to do things to subsets that form big contiguous blocks (as is the case in this puzzle). If you want to operate on random values scattered throughout the array, it gives you plaid, which is probably not what you want; instead, you have to use cbind() to combine your vectors of N indices for each of M dimensions into an MxN array and use that. I always have a hard time remembering cbind(); Maybe explaining it here will help.

This is the first one I had to do a substantial amount of cleanup to the input; that's what all the gsub commands at the beginning are for. I feel like that doesn't really belong in this code, and that the proper solution is for whatever system is generating the inputs to do it in a form that better matches the end use.


Day 7 [LOC: 27/1]
Puzzle: http://adventofcode.com/day/7
Solution: https://github.com/sethmcg/advent/blob/master/day7.R

Oh, man, this craziness. R has a function named "do.call" that lets you dynamically construct a function call from the name of the function and a list of arguments. Most of the time, you don't wanna do that. Most of the time, computing on the language like this just gives you a very elaborate and indirect way of shooting yourself in the foot with a bazooka. But when it is actually what you want to do, it's beautiful. I mean, this puzzle is literally a bunch of instructions to be evaluated. So just, y'know. Do exactly that. Boom.


Day 8 [LOC: 21/4]
Puzzle: http://adventofcode.com/day/8
Solution: https://github.com/sethmcg/advent/blob/master/day8.R

Ugh, string encoding. I feel like if you ever had to deal with a gross mishmash of string escaping and encoding like this in the real world, the right answer would be to move upstream until you could make the problem go away. I'm not sure whether there's a more elegant solution using mind-twisting regexps or if that's the trap to avoid, but I found it easiest just to completely ignore what the data is supposed to mean and split it into a sequence of individual characters to scan for for the patterns of interest.


Day 9 [LOC: 10/1]
Puzzle: http://adventofcode.com/day/9
Solution: https://github.com/sethmcg/advent/blob/master/day9.R

There are packages for doing the Traveling Salesman Problem, but if you need the optimal solution, the only way to know you have the right answer is to exhaustively test all possible routes. Which grows factorially, but fortunately there are only 8 stops, so it's still tractable. I feel like there's probably a more concise way of turning the input into a distance matrix, but I guess six lines of code isn't bad.


Day 10 [LOC: 9/4]
Puzzle: http://adventofcode.com/day/10
Solution: https://github.com/sethmcg/advent/blob/master/day10.R

Always check to see if the function you need already exists. Run-length encoding is commonly-enough used in statistics that there's a function in the R core ("rle") that does it. If it weren't for the loop, I bet you could get this down to a one-liner, though whether it would be intelligible is another matter...
LinkReply

Comments:
[User Picture]From: geometrician
2016-02-26 04:03 pm (UTC)
cbind() always gives me trouble, even though I probably use it more consistently than any other function. I am having trouble getting over the hump of thinking like R. I think I need to spend time walking through learning tools like anyonecancode.com, but finding the time when I'm thrashing about trying to do regression in R is a problem.
(Reply) (Thread)
[User Picture]From: dr_tectonic
2016-02-26 04:35 pm (UTC)
I think very spatially, so actually using cbind() is pretty easy for me. It's remembering to think of it as one of the possibilities to consider that gives me trouble...

anyonecancode.com looks defunct, unfortunately, but I've always found it easier to learn by working on a project I care about than on an artificial task for a class. So, honestly, thrashing about trying to do regression isn't necessarily a bad way to go about it. What I find often helps with learning new tools is starting out with synthetic data that I cooked up so I know what the right answer is; once I've figured out how to get it to tell me what I already know, then I can use it on something new.
(Reply) (Parent) (Thread)
[User Picture]From: geometrician
2016-02-26 04:46 pm (UTC)
Och, this is the one I meant: https://code.org. I try not to underestimate the value of learning like an elementary student. I like what this site appears to be--I haven't actually dived into it yet--because it looks like if I am not getting it at the grown-up level I can step back and try coloring in some balloons.
(Reply) (Parent) (Thread)
[User Picture]From: dr_tectonic
2016-02-26 06:56 pm (UTC)
Giving credit where it's due, I did learn the fundamentals of programming by playing with Logo when I was in 4th grade, and that has served me pretty well, so you may well be on to something there.
(Reply) (Parent) (Thread)