Skip to content

Instantly share code, notes, and snippets.

@lilactown
Last active December 2, 2017 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilactown/fa500e163ed372d239e2abcdf49894ff to your computer and use it in GitHub Desktop.
Save lilactown/fa500e163ed372d239e2abcdf49894ff to your computer and use it in GitHub Desktop.
Advent of Code, 2017 Day 1: A more general solution
let explode = (input) =>
Js.String.split("", input)
|> Array.map(int_of_string);
let rec solver = (digits, len, step, xPos, total) => {
let x = digits[xPos];
let yPos = (xPos + step) mod len;
let y = digits[yPos];
/* curry the next call to solver */
let next = solver(digits, len, step);
let isEndOfList = xPos == len - 1;
switch (x == y, isEndOfList) {
| (false, true) => total
| (true, true) => total + x
| (false, false) => next(xPos + 1, total)
| (true, false) => next(xPos + 1, total + x)
}
};
let neighbor = (input) => {
let step = 1;
let digits = explode(input);
solver(digits, Array.length(digits), step, 0, 0)
};
let halfway = (input) => {
let len = String.length(input);
let step = len / 2;
let digits = explode(input);
solver(digits, len, step, 0, 0)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment