Skip to content

Instantly share code, notes, and snippets.

@martinhj
Last active December 7, 2019 17:23
Show Gist options
  • Save martinhj/132c575f576c6581f5ebcbd65f11072a to your computer and use it in GitHub Desktop.
Save martinhj/132c575f576c6581f5ebcbd65f11072a to your computer and use it in GitHub Desktop.
AdventOfCode 2019 reasonml
[%raw "require('isomorphic-fetch')"]
let fetchUrl = "https://adventofcode.com/2019/day/1/input"
let calculateFuel = mass =>
mass / 3 - 2
let add = (i, j) => i + j;
let parseResponse = text =>
Js.String.split( "\n", text)
|> Js.Array.map(String.trim)
|> Js.Array.filter(text => String.length(text) > 0)
|> Js.Array.map(
number =>
int_of_string(number)
|> calculateFuel
)
|> Js.Array.reduce((acc, currentFuel) => add(acc, currentFuel), 0)
|> Js.log
let fetchData = Js.Promise.(
Fetch.fetchWithInit(
fetchUrl,
Fetch.RequestInit.make(
~headers=Fetch.HeadersInit.make({
"Cookie": "mysecret",
}),
()
)
)
|> then_(Fetch.Response.text)
|> then_(text =>
parseResponse(text)
|> resolve
)
);
let subtractBy = (~subtrahend, minuend) => minuend - subtrahend;
let devideBy = (~divisor, dividend) => dividend / divisor;
let subtractByTwo = subtractBy(~subtrahend=2);
let devideByThree = devideBy(~divisor=3);
type summarizeFuel = int => int;
let summarizeFuel = (themass: int) => {
let rec aux = (~sum=0, mass: int) =>
switch (calculateFuel(mass)) {
| x when x > 0 => aux(~sum=sum + x, x)
| _ => sum
};
aux(themass);
};
let mapSumFuel = (aList: list(int)) => {
let rec aux = (~newList=[], l) =>
switch (l) {
| [] => newList
| [head, ...tail] =>
aux(~newList=[summarizeFuel(head), ...newList], tail)
};
aux(aList);
};
let summerizeList = (listToBeSumarized: list(int)) => {
let rec aux = (~sum=0, l: list(int)) =>
switch (l) {
| [] => sum
| [head, ...tail] => aux(~sum=sum + head, tail)
};
aux(listToBeSumarized);
};
let dataset = [ 128398, 118177, 139790, 84818, 75859, 139920, 90212, 74975, 120844, 85533, 77851, 127044, 128094, 77724, 81951, 115804, 60506, 65055, 52549, 108749, 92367, 53974, 52896, 66403, 93539, 118392, 78768, 128172, 85643, 109508, 104742, 71305, 84558, 68640, 58328, 58404, 70131, 73745, 149553, 57511, 119045, 90210, 129537, 114869, 113353, 114181, 130737, 134877, 90983, 84361, 62750, 114532, 139233, 139804, 130391, 144731, 84309, 137050, 79866, 121266, 93502, 132060, 109190, 61326, 58826, 129305, 141059, 143017, 56552, 102142, 110604, 136052, 93872, 71951, 72954, 70701, 137381, 76580, 62535, 62666, 126366, 66361, 109076, 126230, 73367, 94459, 126314, 133327, 143771, 50752, 75607, 117606, 142366, 59068, 75574, 149836, 57058, 77622, 83276, 82734 ];
dataset->mapSumFuel->summerizeList->print_int;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment