Browse Source

Day 5 js

master
boB Rudis 3 years ago
parent
commit
4cd1f6cac0
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 1
      R/.Rproj.user/shared/notebooks/paths
  2. 2
      R/05.R
  3. 14
      R/05.py
  4. 2
      js/04.js
  5. 59
      js/05.js

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

@ -17,6 +17,7 @@
/Users/hrbrmstr/Development/2020-code-advent/js/02.js="BC8B36A1"
/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/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="DFC19AA4"

2
R/05.R

@ -81,7 +81,7 @@ strsplit(input, "") %>%
rng <- c(0, 7)
for (part in .x[8:10]) {
for (part in .x[7:10]) {
amt <- floor((rng[2] - rng[1])/2) + 1
if (part == "L") {
rng[2] <- rng[2] - amt

14
R/05.py

@ -4,7 +4,7 @@ import numpy as np
with open("../input/05-01.txt") as f:
input = f.read().splitlines()
def partition(seq, lb, ub, trigger, ret):
def partition(seq, lb, ub, trigger):
for idx, part in enumerate(seq):
amt = math.floor((ub - lb)/2) + 1
@ -18,8 +18,8 @@ def partition(seq, lb, ub, trigger, ret):
def get_seat(x):
return(
(partition(x[0:7], 0, 127, "F", "F") * 8) +
partition(x[7:10], 0, 7, "L", "L")
(partition(x[0:7], 0, 127, "F") * 8) +
partition(x[7:10], 0, 7, "L")
)
res = [ get_seat(bpass) for bpass in input ]
@ -33,11 +33,9 @@ max(res)
# 05-02
res.insert(0, res[0])
idx = np.where(np.diff(res) > 1)
res.pop(0)
res[idx[0][0]] - 1
idx = np.where(np.diff(res) > 1)
res.pop(0)
res[idx[0][0]] - 1

2
js/04.js

@ -17,7 +17,7 @@ Number.prototype.between = function(a, b) {
return(this >= min && this <= max)
};
req_keys = new Set([ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ])
req_keys = new Set([ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ])
input = []

59
js/05.js

@ -0,0 +1,59 @@
var fs = require("fs")
//input = fs.readFileSync("/tmp/test.txt", "utf-8")
input = fs.readFileSync("../input/05-01.txt", "utf-8")
console.log(input)
res = input
.split("\n")
.filter((x) => x.length > 1)
.map((line) => {
lb = 0
ub = 127
for (part of line.substr(0, 7)) {
amt = Math.floor((ub - lb)/2) + 1
if (part === "F") {
ub -= amt
} else {
lb += amt
}
}
row = (line.charAt(6) === "F") ? lb : ub
lb = 0
ub = 7
for (part of line.substr(7, 3)) {
amt = Math.floor((ub - lb)/2) + 1
if (part === "L") {
ub -= amt
} else {
lb += amt
}
}
seat = (line.charAt(9) === "R") ? lb : ub
return((row * 8) + seat)
}).sort()
// 05-01
console.log(Math.max(...res))
// 05-02
diff = res.map((n, i, a) => i ? n - a[i-1] : 0 - n)
console.log(res[diff.findIndex((x) => x > 1)] - 1)
Loading…
Cancel
Save