Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Solving Advent of Code 2019-01 with R and JavaScript.

[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code, as it gives solutions for solving day 1.

[Disclaimer bis] I’m no JavaScript expert so this might not be the perfect solution. TBH, that’s also the case for the R solution.

The JavaScript code has been written in the same RMarkdown as the R code. It runs thanks to the {bubble} package: https://github.com/ColinFay/bubble

## R solution

### Part one

# Read
# Get the sum of each element, divided by 3, rounded down, and substracted 2

## [1] 5039071


## JS solution

### Part one & Two

var fs = require('fs');

var res = fs.readFileSync("input1.txt", 'utf8').split("\n").filter(x => x.length != 0);

// Turning to integer
res = res.map(x => parseInt(x));

// Doing the floor of division less 2
var val = res.map(x => Math.floor(x / 3) - 2);

// Suming
var add = (x, y) => x + y;

// Solution

// Creating the recursive function
function floorish(val, start = 0){
loc = Math.floor(val / 3) - 2;
if (loc > 0){
start += loc;
return floorish(loc, start);
} else {
return start;
}
}

// Doing the computation
console.log( res.map( x => floorish(x) ).reduce( add ) );

## 3361299
## 5039071