Skip to content

Instantly share code, notes, and snippets.

@lilactown
Last active December 1, 2017 21:42
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/f678e021d15145a1d6821d2fe6ca6d2b to your computer and use it in GitHub Desktop.
Save lilactown/f678e021d15145a1d6821d2fe6ca6d2b to your computer and use it in GitHub Desktop.
Advent of Code, 2017 Day 1 (1/2)
let explode = (input) =>
Js.String.split("", input)
|> Array.map(int_of_string)
|> Array.to_list;
let rec solver = (current, next, rest, first, total) =>
switch (next == first, current == next, rest) {
/* when we're on the last item in the list */
| (true, true, []) => total + current + next
| (false, true, []) => total + current
| (true, false, []) => total + next
| (false, false, []) => total
/* when we have more to go */
| (_, true, [next', ...rest']) => solver(next, next', rest', first, total + current)
| (_, false, [next', ...rest']) => solver(next, next', rest', first, total)
};
let solve = (input) =>
switch (explode(input)) {
| [current, next, ...rest] => solver(current, next, rest, current, 0)
| _ => 0
};
/*
// solution translated to JS; will not work on biiiig numbers D:
const explode =
(input) => input.split("").map((s) => parseInt(s, 10));
const solver = (current, next, rest, first, total) {
if (rest.length < 1) {
if (current === next && next === first) {
return total + current + next;
}
else if (current === next) {
return total + current;
}
else if (next === first) {
return total + next;
}
else {
return total;
}
} else {
const [next1, ...rest1] = rest;
if (current === next) {
return solver(next, next1, rest1, first, total + current);
} else {
return solver(next, next1, rest1, first, total);
}
}
};
const solve = (input) => {
if (input.length < 2) {
return 0;
} else {
const [current, next, ...rest] = explode(input);
return solver(current, next, rest, current, 0);
}
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment