Advent 2017-1 12/8 '17
Advent of code is fun, if you like programming.
In December every day thru Christmas you get a little puzzle that you need to write code to solve. You get some text and some rules; a single value will be the answer.
I wrote a programming language (called SAI) a while ago and I'm using it to solve these puzzles. I may post a few solutions here, so if you want to solve them on your own, don't read further, or any other "Advent" posts.
-- spoiler --
Here's the solution to the first puzzle, December 1.
In the code below, source is the provided input to your puzzle solver and the two debug statements print out the solutions to parts 1 and 2 of the puzzle:
set elements to source.split('') thru it-0 // note! previous to elements last result to 0 each elements as elem if previous = elem set result + elem set previous to elem debug "Part 1: ${result}" set result to 0 len to elements.length count 0 to len as pos1 set pos2 to (pos1 + (len/2)) % len if elements[pos1] = elements[pos2] set result + elements[pos1] debug "Part 2: ${result}"
I designed SAI to be easy to read and with a minimum of punctuation and special symbols, so it looks more like pseudo-code than actual code, and in general resembles a typed out version of what you would verbally describe a program to be doing. Also, this program is certainly not as efficient as one could make it. But I believe that readable and maintainable are more important than efficiency, with very narrow exceptions.
Note that SAI compiles to Javascript, so is subject to some of Javascript's bullshit, in particular the strange handling of numeric values vs strings of digits. A simple and fast way of converting a string to a value (if you're sure it is convertible) is to subtract 0 from it; that's what is happening here.