Advent of Code 2021-02 with R & JavaScript

2 minute(s) read

Solving Advent of Code 2021-02 with R and JavaScript.

Like yesterday, trying to write JS code that looks like R code.

Disclaimer

Disclaimer Obviously, this post contains a big spoiler about Advent of Code, as it gives solutions for solving 2021-02.

[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.

bubble::set_node_engine()

Instructions

One

  • We start at a position [0, 0] (horizontal, depth), then
    • forward X increases the horizontal position by X units
    • down X increases the depth by X units
    • up X decreases the depth by X units

Result is horizontal * depth.

Two

  • We start at a position [0, 0, 0] (horizontal, depth,), then
    • down X increases your aim by X units
    • up X decreases your aim by X units
    • forward X does two things :
      • It increases your horizontal position by X units
      • It increases your depth by your aim multiplied by X

Result is horizontal * depth.

Find the complete instructions at: https://adventofcode.com/2021/day/2.

Reading inputs

# Read
ipt <- read.delim("2021-02-aoc.txt", header = FALSE)$V1
const fs = require('fs')
var ipt = fs.readFileSync("2021-02-aoc.txt", 'utf8').split("\n").filter(x => x.length != 0);

Part one

R solution

# Horizontal and depth
start_pos <- c(horizontal = 0, depth = 0)
for (moves in ipt) {
  move <- strsplit(moves, " ")[[1]]

  if (move[1] == "up") {
    start_pos[2] <- start_pos[2] - as.numeric(move[2])
  }
  if (move[1] == "down") {
    start_pos[2] <- start_pos[2] + as.numeric(move[2])
  }
  if (move[1] == "forward") {
    start_pos[1] <- start_pos[1] + as.numeric(move[2])
  }
}

start_pos[1] * start_pos[2]
## horizontal 
##    1727835

JS solution

var start_pos = [0, 0]

// Gotcha: R for(x in y) is JS for(x of y)

for (moves of ipt) {
  // In JS, object have methods, so the split function
  // is containes inside the string object
  move = moves.split(' ')

  // Small gotcha here, JS i 0 based index
  if (move[0] == "up") {
    // R as.numeric is parseInt()
    start_pos[1] = start_pos[1] - parseInt(move[1])
  }
  if (move[0] == "down") {
    start_pos[1] = start_pos[1] + parseInt(move[1])
  }
  if (move[0] == "forward") {
    start_pos[0] = start_pos[0] + parseInt(move[1])
  }
}
start_pos[0] * start_pos[1]
## 1727835

Part two

R solution

# Horizontal, depth, and aim
start_pos <- c(horizontal = 0, depth = 0, aim = 0)
for (moves in ipt) {
  move <- strsplit(moves, " ")[[1]]

  if (move[1] == "up") {
    start_pos[3] <- start_pos[3] - as.numeric(move[2])
  }
  if (move[1] == "down") {
    start_pos[3] <- start_pos[3] + as.numeric(move[2])
  }
  if (move[1] == "forward") {
    start_pos[1] <- start_pos[1] + as.numeric(move[2])
    start_pos[2] <- start_pos[2] + (start_pos[3] * as.numeric(move[2]))
  }
}

start_pos[1] * start_pos[2]
## horizontal 
## 1544000595

JS solution

var start_pos = [0, 0, 0]

for (moves of ipt) {
  move = moves.split(' ')
  if (move[0] == "up") {
    start_pos[2] = start_pos[2] - parseInt(move[1])
  }
  if (move[0] == "down") {
    start_pos[2] = start_pos[2] + parseInt(move[1])
  }
  if (move[0] == "forward") {
    start_pos[0] = start_pos[0] + parseInt(move[1])
    start_pos[1] = start_pos[1] + (start_pos[2] * parseInt(move[1]))
  }
}
start_pos[0] * start_pos[1]
## 1544000595

Categories:

Updated:

What do you think?