Решение появления кода 2019-01 с R и JavaScript.
[Отказ от ответственности] Очевидно, что этот пост содержит большой спойлер о коде Adventof, поскольку он дает решения для решения дня 1.
[Отказ от ответственности BIS] Я не эксперт JavaScript, так что это не может быть решением. TBH, это также относится к решению R.
О коде JavaScript
Код JavaScript был записан в том же rmarkdown, что и RCODE. Он работает благодаря {bubble}
Пакет: https://github.com/colinfay/bubble
инструкции
Найдите инструкции по адресу: https://adventofcode.com/2019/day/1
R Решение
Часть первая
# Read ipt <- read.delim( "input1.txt", header = FALSE ) # Get the sum of each element, divided by 3, rounded down, and substracted 2 sum( floor( ipt$V1 / 3) - 2 ) ## [1] 3361299
Часть вторая
Использование рекурсивной функции: https://en.wikipedia.org/wiki/recursion_(computer_science)
floorish <- function(x, start = 0){ loc <- floor( x / 3) - 2 if (loc > 0){ start <- start + loc floorish(loc, start) } else { return(start) } } sum( purrr::map_dbl(ipt$V1, floorish) ) ## [1] 5039071
М.С. Решение
Часть первая и две
var fs = require('fs'); // Reading the file var res = fs.readFileSync("input1.txt", 'utf8').split("\n").filter(x => x.length != 0); // Turning to integer res = res.map(x => parseInt(x)); // Doing the floor of division less 2 var val = res.map(x => Math.floor(x / 3) - 2); // Suming var add = (x, y) => x + y; // Solution console.log( val.reduce(add) ); // Creating the recursive function function floorish(val, start = 0){ loc = Math.floor(val / 3) - 2; if (loc > 0){ start += loc; return floorish(loc, start); } else { return start; } } // Doing the computation console.log( res.map( x => floorish(x) ).reduce( add ) ); ## 3361299 ## 5039071
Оригинал: “https://dev.to/colinfay/advent-of-code-2019-01-with-r-javascript-5742”