[Jprogramming] Advent of Code, Day 1 (spoilers)
Daniel Lyons
fusion at storytotell.org
Fri Dec 1 05:49:26 UTC 2017
The problem statement has to do with summing up digits in a large number if the digit matches the following digit, with wraparound.
My concept is to take the input as an array of single-digit numbers, compare this to itself rotated forward a digit. Wherever those match up, the following digit is a match. I can then copy to select the values that match and then simply sum them up. It works out like this:
NB. First I want digits, so:
a =. "."0 '<my big captcha string>'
NB. Part 1: the idea is to line up the input against itself, rotated right one
+/ a #~ a = 1 |. a
This worked, and then the twist in problem 2 turned out to be easy to compensate for: instead of hard-coding 1, calculate half the length of the array:
NB. Part 2: rotate by half the length
+/ a #~ a = (-: $ a) |. a
I did not even try to define these as functions, since I was sure I'd lose the plot trying to make them tacit and get frustrated.
--
Daniel Lyons
More information about the Programming
mailing list