Skip to content

Instantly share code, notes, and snippets.

@arecvlohe
Last active February 7, 2018 15:41
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 arecvlohe/5cd6a74c56546ed912b8fdec75d4c5a5 to your computer and use it in GitHub Desktop.
Save arecvlohe/5cd6a74c56546ed912b8fdec75d4c5a5 to your computer and use it in GitHub Desktop.
Advent of Code - Day 1
let inverseCaptcha = (path) => {
let arr =
Node_fs.readFileSync(path, `utf8)
|> Js.String.trim
|> Js.String.split("");
Js.Array.reducei(
(acc, curr, idx) =>
switch (idx == 0) {
| true =>
let firstValue = int_of_string(curr);
let lastValue = int_of_string(arr[Js.Array.length(arr) - 1]);
firstValue == lastValue ? acc + firstValue : acc;
| _ =>
let prevValue = int_of_string(arr[idx - 1]);
let currValue = int_of_string(curr);
prevValue == currValue ? acc + currValue : acc;
},
0,
arr
);
};
let result = inverseCaptcha("./src/inverse-captcha.txt");
Js.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment