Last active
May 3, 2019 15:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# [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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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