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-02 with R and JavaScript.

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

[Disclaimer bis] I’m no JavaScript expert so this might not be the perfect solution. And 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

When in doubt, use brute force.

Ken Thompson

### Part one

extract <- function(vec, idx) vec[as.character(idx)]

day_2 <- function(vec, one = 12, two = 2){
vec <- one
vec <- two
names(vec) <- 0:(length(vec) - 1)
start <- 0
repeat {
req <- extract(vec, start)

if (req == 99) break
if (req == 1) fun <- +
if (req == 2) fun <- *

vec[as.character(
extract(vec, start + 3)
)] <- fun(
extract(vec, extract(vec, start + 1)),
extract(vec, extract(vec, start + 2))
)
start <- start + 4
}

vec
}

ipt <- scan( "input2.txt", what = numeric(), sep = ",")

day_2(ipt)

##       0
## 3409710


### Part two

x <- purrr::cross2(0:99, 0:99)
i <- 1

repeat{
res <- day_2(ipt, x[[i]][], x[[i]][])
if (res == 19690720) break
i <- i + 1
}
100 * x[[i]][] +  x[[i]][]

##  7912


## JS solution

### Part one & Two

var res = fs.readFileSync("input2.txt", 'utf8').split(",").filter(x => x.length != 0);
var res = res.map(x => parseInt(x));

function day_2(vec, one = 12, two = 2){
var loc = vec.slice();
loc = one;
loc = two;
start = 0;
do {
var req = loc[start];
if (req === 99){
break;
}
pos1 = loc[start + 1];
pos2 = loc[start + 2];
pos3 = loc[start + 3];
if (req === 1){
loc[pos3] = loc[pos1] + loc[pos2];
} else if (req === 2){
loc[pos3] = loc[pos1] * loc[pos2];
}
start = start + 4;
} while (start < vec.length)
return loc
}

day_2(res)

## 3409710

function make_array(l){
return Array.from({length: l}, (el, index) => index);
}
var x = make_array(100);
var y = make_array(100);

var cross = [];

for (var i = 0; i < x.length; i++){
for (var j = 0; j < y.length; j++){
cross.push(
[x[i], y[j]]
)
}
}

i = 0

do {
ans = day_2(res, cross[i], cross[i]);
if (ans == 19690720) break
i++
} while (i < cross.length)

100 * cross[i] + cross[i]

## 7912


## Day 2 takeaway

• Array.from({length: n}, (el, index) => index); is more or less the equivalent of R 1:n

• When doing [] = in JS, we’re modifying the original objet. Compare

# R
x <- 1:3
x

##  1 2 3

plop <- function(y){
y <- 2
}
plop(x)
x

##  1 2 3


to

// JS
var x = [1, 2, 3];
function yeay(ipt){
ipt = 12
}
yeay(x)

x

## [ 1, 12, 3 ]

• JavaScript copies by reference. Compare:
# R
x <- 1:3
y <- x
x <- 999
x

##  999   2   3

y

##  1 2 3


And

var x = make_array(3);
var y = x
x = 999

x
y

## [ 0, 999, 2 ]
## [ 0, 999, 2 ]

• This can be prevented with obj.slice()
var x = make_array(10);
var y = x.slice();
x = 999

x
y

## [ 0, 999, 2, 3, 4, 5, 6, 7, 8, 9 ]
## [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]