Advent of Code 2019-02 with R & JavaScript

December 1, 2019
By

[This article was first published on Colin Fay, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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.

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)
##       0 
## 3409710

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]]
## [1] 7912

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

# R
x <- 1:3
x
## [1] 1 2 3
plop <- function(y){
  y[1] <- 2
}
plop(x)
x
## [1] 1 2 3

to

// JS
var x = [1, 2, 3];
function yeay(ipt){
  ipt[1] = 12
}
yeay(x)
x
## [ 1, 12, 3 ]
  • JavaScript copies by reference. Compare:

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

And

var x = make_array(3);
var y = x
x[1] = 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[1] = 999
x
y
## [ 0, 999, 2, 3, 4, 5, 6, 7, 8, 9 ]
## [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

To leave a comment for the author, please follow the link and comment on their blog: Colin Fay.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)