DEV Community

Colin Fay
Colin Fay

Posted on • Originally published at colinfay.me on

2 1

Advent of Code 2019-03 with R & JavaScript

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
Enter fullscreen mode Exit fullscreen mode

Part two

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

## [1] 6486
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay