Browse Source

Day 9 JS

master
boB Rudis 3 years ago
parent
commit
d5f4f0601d
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1014
      R/.Rhistory
  2. 1
      R/.Rproj.user/shared/notebooks/paths
  3. 10
      R/09.R
  4. 67
      js/09.js

1014
R/.Rhistory

File diff suppressed because it is too large

1
R/.Rproj.user/shared/notebooks/paths

@ -26,6 +26,7 @@
/Users/hrbrmstr/Development/2020-code-advent/js/03.js="E24752E6"
/Users/hrbrmstr/Development/2020-code-advent/js/04.js="CB04A9CB"
/Users/hrbrmstr/Development/2020-code-advent/js/05.js="AFC9E61D"
/Users/hrbrmstr/Development/2020-code-advent/js/08.js="E730C21E"
/Users/hrbrmstr/Development/2020-code-advent/scala/01/01/src/main/scala/Main.scala="8F03A1D5"
/Users/hrbrmstr/Development/2020-code-advent/scala/02/02/src/main/scala/Main.scala="418A1A5F"
/private/tmp/test.txt="1CB0AD22"

10
R/09.R

@ -75,9 +75,9 @@ input <- as.numeric(read_lines("../input/09-01.txt"))
preamble_length <- 25
window <- 1:preamble_length
input_start <- preamble_length + 1
no_sum_found <- TRUE
condition <- TRUE
while(no_sum_found && (length(input)>=(preamble_length + 1))) {
while(condition && (length(input)>=(preamble_length + 1))) {
as.data.frame(t(combn(window, 2))) %>%
mutate(
@ -86,9 +86,9 @@ while(no_sum_found && (length(input)>=(preamble_length + 1))) {
sum = input[V1] + input[V2]
) -> pair_sums
no_sum_found <- any(pair_sums$sum == input[input_start])
condition <- any(pair_sums$sum == input[input_start])
if (no_sum_found) input <- tail(input, -1)
if (condition) input <- tail(input, -1)
}
@ -133,7 +133,7 @@ while(no_sum_found && (length(input)>=(preamble_length + 1))) {
# producing 62.
#
# What is the encryption weakness in your XMAS-encrypted list of numbers?
# s
#
# 09-02 -------------------------------------------------------------------
# input <- test_input

67
js/09.js

@ -0,0 +1,67 @@
var fs = require("fs")
var combn = require('generatorics') // npm install generatorics
Array.prototype.accumulate = function(ƒ) {
res = [this[0]]
for (var idx=1; idx<this.length; idx++) res.push(ƒ(res[idx - 1], this[idx]))
return(res)
}
// 09-01
input = fs.readFileSync("../input/09-01.txt", "utf-8")
.split("\n")
.map((x) => parseInt(x))
//input = [ 35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576 ]
preamble_length = 25
input_start = preamble_length
condition = true
while(condition && (input.length > (preamble_length))) {
for (var pair of combn.combination([...Array(preamble_length).keys()], 2)) {
condition = (input[pair[0]] + input[pair[1]]) == input[input_start]
if (condition) break
}
if (condition) input.shift()
}
invalid = input[input_start]
console.log(invalid)
// 09-02
input = fs.readFileSync("../input/09-01.txt", "utf-8")
.split("\n")
.map((x) => parseInt(x))
//input = [ 35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299, 277, 309, 576 ]
const cumsum = (sum => value => sum += value)(0)
for (var idx=0; idx<input.length; idx++) {
csum = input.slice(idx, input.length).accumulate((a, b) => a + b)
if (csum.includes(invalid)) break // 'invalid' comes from part 1
}
rng = input.slice(idx, idx+csum.indexOf(invalid))
console.log(Math.min(...rng) + Math.max(...rng))
Loading…
Cancel
Save