Advent of Code 2019-02 with R & JavaScript
Tags: rstats, adventofcode, javascript
Published: 2019-12-03 07:56:26

---
title: Advent of Code 2019-02 with R & JavaScript
published: true
date: 2019-12-02 00:00:00 UTC
tags: rstats, adventofcode, javascript
canonical_url: https://colinfay.me/aoc-2019-02/
---

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.

## About the JavaScript code

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>

## Instructions

Find the instructions at: <https://adventofcode.com/2019/day/2>

## R solution

> When in doubt, use brute force.

> Ken Thompson

### Part one

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

day_2 <- function(vec, one = 12, two = 2){
  vec[2] <- one
  vec[3] <- 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[1]
}

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

day_2(ipt)
```

    ##       0 
    ## 3409710

### Part two

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

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

    ## [1] 7912

## JS solution

### Part one & Two

> // Reading the file

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

``` javascript
function day_2(vec, one = 12, two = 2){
  var loc = vec.slice();
  loc[1] = one;
  loc[2] = 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[0]
}
```

``` javascript
day_2(res)
```

    ## 3409710

``` javascript
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][0], cross[i][1]);
  if (ans == 19690720) break
  i++
  } while (i < cross.length)
```

``` javascript
100 * cross[i][0] + cross[i][1]
```

    ## 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

<!-- end list -->

``` r
# R
x <- 1:3
x
```

    ## [1] 1 2 3

``` r
plop <- function(y){
  y[1] <- 2
}
plop(x)
x
```

    ## [1] 1 2 3

to

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

``` javascript
x
```

    ## [ 1, 12, 3 ]

  - JavaScript copies by reference. Compare:

<!-- end list -->

``` r
# R 
x <- 1:3
y <- x
x[1] <- 999
x
```

    ## [1] 999   2   3

``` r
y
```

    ## [1] 1 2 3

And

``` javascript
var x = make_array(3);
var y = x
x[1] = 999
```

``` javascript
x
y
```

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

  - This can be prevented with `obj.slice()`

<!-- end list -->

``` javascript
var x = make_array(10);
var y = x.slice();
x[1] = 999
```

``` javascript
x
y
```

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