DEV Community

Colin Fay
Colin Fay

Posted on • Originally published at colinfay.me on

Advent of Code 2019-02 with R & JavaScript

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

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)
Enter fullscreen mode Exit fullscreen mode
##       0 
## 3409710
Enter fullscreen mode Exit fullscreen mode

Part two

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]]
Enter fullscreen mode Exit fullscreen mode
## [1] 7912
Enter fullscreen mode Exit fullscreen mode

JS solution

Part one & Two

// Reading the file

var res = fs.readFileSync("input2.txt", 'utf8').split(",").filter(x => x.length != 0);
var res = res.map(x => parseInt(x));
Enter fullscreen mode Exit fullscreen mode
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]
}
Enter fullscreen mode Exit fullscreen mode
day_2(res)
Enter fullscreen mode Exit fullscreen mode
## 3409710
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
100 * cross[i][0] + cross[i][1]
Enter fullscreen mode Exit fullscreen mode
## 7912
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
## [1] 1 2 3
Enter fullscreen mode Exit fullscreen mode
plop <- function(y){
  y[1] <- 2
}
plop(x)
x
Enter fullscreen mode Exit fullscreen mode
## [1] 1 2 3
Enter fullscreen mode Exit fullscreen mode

to

// JS
var x = [1, 2, 3];
function yeay(ipt){
  ipt[1] = 12
}
yeay(x)
Enter fullscreen mode Exit fullscreen mode
x
Enter fullscreen mode Exit fullscreen mode
## [ 1, 12, 3 ]
Enter fullscreen mode Exit fullscreen mode
  • JavaScript copies by reference. Compare:
# R 
x <- 1:3
y <- x
x[1] <- 999
x
Enter fullscreen mode Exit fullscreen mode
## [1] 999   2   3
Enter fullscreen mode Exit fullscreen mode
y
Enter fullscreen mode Exit fullscreen mode
## [1] 1 2 3
Enter fullscreen mode Exit fullscreen mode

And

var x = make_array(3);
var y = x
x[1] = 999
Enter fullscreen mode Exit fullscreen mode
x
y
Enter fullscreen mode Exit fullscreen mode
## [ 0, 999, 2 ]
## [ 0, 999, 2 ]
Enter fullscreen mode Exit fullscreen mode
  • This can be prevented with obj.slice()
var x = make_array(10);
var y = x.slice();
x[1] = 999
Enter fullscreen mode Exit fullscreen mode
x
y
Enter fullscreen mode Exit fullscreen mode
## [ 0, 999, 2, 3, 4, 5, 6, 7, 8, 9 ]
## [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)