Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active May 3, 2019 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/5f8439abec848b0bb13e4f5aa57aa3d0 to your computer and use it in GitHub Desktop.
Save primaryobjects/5f8439abec848b0bb13e4f5aa57aa3d0 to your computer and use it in GitHub Desktop.
[2019-04-08] Challenge #377 [Easy] Axis-aligned crate packing in R. Demo https://repl.it/repls/StarchyGrippingMath
#
# [2019-04-08] Challenge #377 [Easy] Axis-aligned crate packing
# https://www.reddit.com/r/dailyprogrammer/comments/bazy5j/20190408_challenge_377_easy_axisaligned_crate/
#
# Given X, Y, x, and y, determine how many boxes can fit into a single crate if they have to be placed so that the x-axis of the boxes is aligned with the x-axis of the crate, and the y-axis of the boxes is aligned with the y-axis of the crate. That is, you can't rotate the boxes. The best you can do is to build a rectangle of boxes as large as possible in each dimension.
# For instance, if the crate is size X = 25 by Y = 18, and the boxes are size x = 6 by y = 5, then the answer is 12. You can fit 4 boxes along the x-axis (because 6*4 <= 25), and 3 boxes along the y-axis (because 5*3 <= 18), so in total you can fit 4*3 = 12 boxes in a rectangle.
#
# Demo https://repl.it/repls/StarchyGrippingMath
#
fit1 <- function(crateX, crateY, boxX, boxY) {
# Calculate the max boxes that fit in 2 dimensions.
fitx <- floor(crateX / boxX)
fity <- floor(crateY / boxY)
fitx * fity
}
fit2 <- function(crateX, crateY, boxX, boxY) {
# Allow rotating all boxes by 90 degrees (boxX x boxY or boxY x boxX).
max(fit1(crateX, crateY, boxX, boxY), fit1(crateX, crateY, boxY, boxX))
}
fit3NoRotation <- function(crateX, crateY, crateZ, boxX, boxY, boxZ) {
# Calculate the max boxes that fit in 3 dimensions.
fitx <- floor(crateX / boxX)
fity <- floor(crateY / boxY)
fitz <- floor(crateZ / boxZ)
fitx * fity * fitz
}
fit3 <- function(crateX, crateY, crateZ, boxX, boxY, boxZ) {
# Allow rotating all boxes by 90 degrees in 3 dimensions.
max(fit3NoRotation(crateX, crateY, crateZ, boxX, boxY, boxZ),
fit3NoRotation(crateX, crateY, crateZ, boxX, boxZ, boxY),
fit3NoRotation(crateX, crateY, crateZ, boxY, boxX, boxZ),
fit3NoRotation(crateX, crateY, crateZ, boxY, boxZ, boxX),
fit3NoRotation(crateX, crateY, crateZ, boxZ, boxX, boxY),
fit3NoRotation(crateX, crateY, crateZ, boxZ, boxY, boxX))
}
# Test client for fit1.
fit1TestData <- list(list(input=c(25, 18, 6, 5), output=12),
list(input=c(10, 10, 1, 1), output=100),
list(input=c(12, 34, 5, 6), output=10),
list(input=c(12345, 678910, 1112, 1314), output=5676),
list(input=c(5, 100, 6, 1), output=0)
)
result <- sapply(fit1TestData, function(testData) {
do.call(fit1, as.list(testData$input)) == testData$output
})
print(ifelse(all(result, TRUE), 'fit1 is correct!', 'fit1 failed.'))
# Test client for fit2.
fit2TestData <- list(list(input=c(25, 18, 6, 5), output=15),
list(input=c(12, 34, 5, 6), output=12),
list(input=c(12345, 678910, 1112, 1314), output=5676),
list(input=c(5, 5, 3, 2), output=2),
list(input=c(5, 100, 6, 1), output=80),
list(input=c(5, 5, 6, 1), output=0)
)
result <- sapply(fit2TestData, function(testData) {
do.call(fit2, as.list(testData$input)) == testData$output
})
print(ifelse(all(result, TRUE), 'fit2 is correct!', 'fit2 failed.'))
# Test client for fit3.
fit3TestData <- list(list(input=c(10, 10, 10, 1, 1, 1), output=1000),
list(input=c(12, 34, 56, 7, 8, 9), output=32),
list(input=c(123, 456, 789, 10, 11, 12), output=32604),
list(input=c(1234567, 89101112, 13141516, 171819, 202122, 232425), output=174648)
)
result <- sapply(fit3TestData, function(testData) {
do.call(fit3, as.list(testData$input)) == testData$output
})
print(ifelse(all(result, TRUE), 'fit3 is correct!', 'fit3 failed.'))
[1] "fit1 is correct!"
[1] "fit2 is correct!"
[1] "fit3 is correct!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment