Site icon R-bloggers

Advent of Code 2019-03 with R & JavaScript

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

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

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

Instructions

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

R solution

Part one

library(magrittr)
ipt <- scan( "input3.txt", what = character(), sep = "\n")
first <- strsplit(ipt[1], split = ",")[[1]]
sec <- strsplit(ipt[2], split = ",")[[1]] 

directions <- function(ipt, x, y){
  dir <- substr(ipt, 1, 1)
  how_m <- as.numeric(substr(ipt, 2, nchar(ipt)))
  if (dir == "R"){
    x <- x + how_m
  } else if (dir == "L"){
    x <- x - how_m
  } else if (dir == "U"){
    y <- y + how_m
  } else if (dir == "D"){
    y <- y - how_m
  }
  return(list(x = x, y = y))
}

get_dir <- function(vec){
  out <- data.frame(
    x = 0, 
    y = 0
  )
  for (i in seq_along(vec)){
    y_m_1 <- out$y[nrow(out)]
    x_m_1 <- out$x[nrow(out)]
    res <- directions(vec[i], x = x_m_1, y = y_m_1)
    out %<>% rbind(
      data.frame(
        x = x_m_1:res$x, 
        y = y_m_1:res$y
      )[-1, ]
    )
  }
  out$step <- 1:nrow(out) 
  out
}

out_a <- get_dir(first)
out_b <- get_dir(sec)

res <- merge(out_a, out_b, by = c("x", "y"))
res$path <- abs(res$x) + abs(res$y)
sort(unique(res$path))[2]

## [1] 386

Part two

res$tot_step <- res$step.x + res$step.y
sort(unique(res$tot_step))[2]

## [1] 6486

JS solution

Sorry today I didn’t have time to work on the JS solution… Finding the R solution was already quite a challenge 🙂

Here is the beginning of a code to solve the problem. Might get back to it later!

Part one & Two

var res = fs.readFileSync("input3.txt", 'utf8').split("\n").filter(x => x.length != 0);
var first = res[0].split(",").filter(x => x.length != 0);
var sec = res[1].split(",").filter(x => x.length != 0);

function directions(ipt, x, y){
  var dir = ipt.substring(0,1)
  var how_m = parseInt(ipt.substring(1, ipt.length))
  if (dir == "R"){
    var x = x + how_m
  } else if (dir == "L"){
    var x = x - how_m
  } else if (dir == "U"){
    var y = y + how_m
  } else if (dir == "D"){
    var y =  y - how_m
  }
  var ret = {x : x, y : y};
  return ret
}

function get_out(vec){
  var out = {
    x: [0], 
    y: [0]
  }
  for (var i= 0; i < vec.length; i++){
    var y_m_1 = out["y"][ out[["y"]].length - 1 ]
    var x_m_1 =  out["x"][ out[["x"]].length - 1 ]
    var res = directions(vec[i], x = x_m_1, y = y_m_1)
    out.x.push(res.x);
    out.y.push(res.y);
  }
  return out
}

var dir_f = get_out(first);
var dir_s = get_out(sec);

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.