Browse Source

functionized

master
boB Rudis 3 years ago
parent
commit
0a1abb9766
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 39
      R/05.R
  2. 48
      js/05.js

39
R/05.R

@ -63,37 +63,24 @@ library(tidyverse)
input <- readLines("../input/05-01.txt")
strsplit(input, "") %>%
map_dbl(~{
rng <- c(0, 127)
partition <- function(seq, lb, ub, trigger) {
for (part in .x[1:7]) {
amt <- floor((rng[2] - rng[1])/2) + 1
if (part == "F") {
rng[2] <- rng[2] - amt
} else {
rng[1] <- rng[1] + amt
}
for (part in seq) {
amt <- floor((ub - lb)/2) + 1
if (part == trigger) {
ub <- ub - amt
} else {
lb <- lb + amt
}
}
row <- if (.x[7] == "F") rng[1] else rng[2]
return(if (tail(seq, 1) == trigger) ub else lb)
rng <- c(0, 7)
for (part in .x[7:10]) {
amt <- floor((rng[2] - rng[1])/2) + 1
if (part == "L") {
rng[2] <- rng[2] - amt
} else {
rng[1] <- rng[1] + amt
}
}
seat <- if (.x[10] == "R") rng[1] else rng[2]
(row * 8) + seat
}
strsplit(input, "") %>%
map_dbl(~{
(partition(.x[1:7], 0, 127, "F") * 8) + partition(.x[8:10], 0, 7, "L")
}) %>%
sort() -> seats

48
js/05.js

@ -2,40 +2,30 @@ var fs = require("fs")
input = fs.readFileSync("../input/05-01.txt", "utf-8")
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
}
partition = function(seq, lb, ub, trigger) {
for (part of seq) {
amt = Math.floor((ub - lb)/2) + 1
if (part === trigger) {
ub -= amt
} else {
lb += amt
}
}
row = (line.charAt(6) === "F") ? lb : ub
return((seq.slice(-1) === trigger) ? ub : lb)
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
res = input
.split("\n")
.filter((x) => x.length > 1)
.map((line) => {
return((row * 8) + seat)
return(
(partition(line.substr(0, 7), 0, 127, "F") * 8) +
partition(line.substr(7, 3), 0, 7, "L")
)
}).sort()

Loading…
Cancel
Save