# Advent of Code 2020-02 with R & JavaScript

2 minute(s) read

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

[Disclaimer] Obviously, this post contains a big spoiler about Advent of Code.

[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

### Step 1

• Inputs are of the form `1-3 a: abcde`, for `min`-`max` `letter`:`code`, where `letter` must be found at least `min` and no more than `max` in `code`

How to: split the input in four columns, for `min`,`max`,`letter`, and `code`, count the number of `letter` in `code`, and then make sure this count is >= min and <= max.

### Step 2

• Inputs are of the form `1-3 a: abcde`, for `position1`-`position2` `letter`:`code`, where ```code[position1] == letter | code[position2] == letter``` but not ```code[position1] == letter & code[position2] == letter```, nor ```!code[position1] == letter & !code[position2] == letter```

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

## R solution

### Part one

``````# Read
ipt <- read.delim( "2020-02-aoc.txt",  header = FALSE )

library(dplyr, warn.conflicts = FALSE)
library(tidyr)
library(purrr)

ipt %>%
# Create the four columns
separate(V1, c("min", "max", "letter", "code")) %>%
pmap_dbl(~{
# If the letter is not in the input, return 0
if (!stringr::str_detect(..4, ..3)) return(0)
# Count the n of letter in code
count <- stringr::str_count(..4, ..3)
# Is this count between the boundaries?
count >= as.numeric(..1) & count <= as.numeric(..2)
}) %>% sum()
``````
``````##  607
``````

### Part two

``````ipt %>%
# Create the four columns
separate(V1, c("min", "max", "letter", "code")) %>%
pmap_dbl(~{
#browser()
# Split the code
code <- strsplit(..4, "")[]
# code[position1] == letter
p1_match <- code[as.numeric(..1)] == ..3
# code[position1] == letter
p2_match <- code[as.numeric(..2)] == ..3
# No match
if (!p1_match & !p2_match) return(0)
# Two matches
if (p1_match && p2_match) return(0)
return(1)
}) %>% sum()
``````
``````##  321
``````

## JS solution

### Part one & Two

// Reading the file and converting it to int

``````const fs = require('fs')
var ipt = fs.readFileSync("2020-02-aoc.txt", 'utf8').split("\n").filter(x => x.length != 0);
ipt = ipt.map(x => x.split(/[^A-Za-z0-9]/));
``````

// Doing the combn

``````var res1 = ipt.map(x => {
if (!x.includes(x)) return 0
var match = x.match(new RegExp(x, "g")).length
return match >= parseInt(x) & match <= parseInt(x)
})
``````

// Solution

``````// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/reduce
const reducer = (accumulator, currentValue) => accumulator + currentValue;
res1.reduce(reducer)
``````
``````## undefined
## undefined
## 607
``````

// Part 2

``````var res2 = ipt.map(x => {
// Split the code
var code = x.split("")
// code[position1] == letter
var p1_match = code[parseInt(x) - 1] === x
// code[position1] == letter
var p2_match = code[parseInt(x) - 1] === x
// No match
if (!p1_match && !p2_match) return 0
// Two matches
if (p1_match && p2_match) return 0
return 1
})
``````

// Solution

``````res2.reduce(reducer)
``````
``````## 321
``````

Categories:

Updated: