Advent of Code 2021-02 with R & JavaScript

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.

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

Result is horizontal * depth.

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

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, " ")[]

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

start_pos * start_pos
## 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 == "up") {
// R as.numeric is parseInt()
start_pos = start_pos - parseInt(move)
}
if (move == "down") {
start_pos = start_pos + parseInt(move)
}
if (move == "forward") {
start_pos = start_pos + parseInt(move)
}
}
start_pos * start_pos
## 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, " ")[]

if (move == "up") {
start_pos <- start_pos - as.numeric(move)
}
if (move == "down") {
start_pos <- start_pos + as.numeric(move)
}
if (move == "forward") {
start_pos <- start_pos + as.numeric(move)
start_pos <- start_pos + (start_pos * as.numeric(move))
}
}

start_pos * start_pos
## horizontal
## 1544000595

JS solution

var start_pos = [0, 0, 0]

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

Categories:

Updated: