DEV Community

Colin Fay
Colin Fay

Posted on • Originally published at colinfay.me on

Advent of Code 2019-04 with R & JavaScript

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

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

[Disclaimer bis] I’m no JavaScript expert so this might not be theperfect solution. 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 Rcode. It runs thanks to the {bubble} package:https://github.com/ColinFay/bubble

Instructions

Find the instructions at: https://adventofcode.com/2019/day/4

R solution

Part one

test <- 156218:652527
library(magrittr)
cond <- function(vec){
  splt <- strsplit(
    as.character(vec), 
    "")[[1]] %>% as.numeric()
  adj_eq <- (splt == dplyr::lag(splt))[-1]
  incre <- (splt >= dplyr::lag(splt))[-1]
  any(adj_eq) & all(incre)
}
vapply(test, cond, logical(1)) %>% sum()


## [1] 1694

Enter fullscreen mode Exit fullscreen mode

Part two

cond2 <- function(vec){
  splt <- strsplit(
    as.character(vec), 
    "")[[1]] %>% as.numeric()
  adj_eq <- (splt == dplyr::lag(splt))[-1]
  if (any(adj_eq)){
    adj_eq <- any(table(splt) == 2)
  }
  incre <- (splt >= dplyr::lag(splt))[-1]
  any(adj_eq) & all(incre)
}
vapply(test, cond2, logical(1)) %>% sum()


## [1] 1148

Enter fullscreen mode Exit fullscreen mode

JS solution

Part one & Two

// Generating the array (fromhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)

const range = (start, stop, step = 1) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
var test = range(156218, 652527)


function lag(vec){
  var res = [];
  res.push(null);
  for (var i = 0; i < vec.length - 1; i ++){
    res.push(vec[i])
  }
  return res
}

function cond(vec){
  var vec = vec.toString().split("").map(x => parseInt(x));
  var laged = lag(vec);
  var adj_eq = vec.map( (x, i) => x == laged[i] )
  adj_eq.shift()
  var adj_eq = adj_eq.some(z => z);
  var incre = vec.map( (x, i) => x >= laged[i] )
  incre.shift()
  var incre = incre.every(z => z);
  var all = [incre, adj_eq].every(z => z);
  return all
}


test.map(cond).filter(v => v).length


## 1694


function table(vec){
  var tbl = {}
  vec.map(function(x){
    if (tbl[x]){
      tbl[x] = tbl[x] + 1;
    } else {
      tbl[x] = 1;
    }
  })
  return tbl
}
function cond2(vec){
  var vec = vec.toString().split("").map(x => parseInt(x));
  var laged = lag(vec);
  var adj_eq = vec.map( (x, i) => x == laged[i] )
  adj_eq.shift()
  var adj_eq = adj_eq.some(z => z);
  if (adj_eq){
    var tb = table(vec);
    var res = [];
    for (i in tb){
      res.push(tb[i] == 2);
    }
    var adj_eq = res.some(x => x);
  }

  var incre = vec.map( (x, i) => x >= laged[i] );
  incre.shift();
  var incre = incre.every(z => z);
  var all = [incre, adj_eq].every(z => z);
  return all;
}


test.map(cond2).filter(v => v).length;


## 1148

Enter fullscreen mode Exit fullscreen mode

Top comments (0)