Last active
December 6, 2015 02:11
-
-
Save primaryobjects/dfffcf80f355a4fe273a to your computer and use it in GitHub Desktop.
[2015-12-02] Challenge #243 [Intermediate] Jenny's Fruit Basket
This file contains hidden or 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
# https://www.reddit.com/r/dailyprogrammer/comments/3v4zsf/20151202_challenge_243_intermediate_jennys_fruit/ | |
require(plyr) | |
data <- read.csv('input2.txt', sep = ' ', header = FALSE, col.names = c('name', 'price')) | |
money <- 500 | |
orders <- data.frame() | |
for (i in seq(from=1, to=6)) { | |
print(paste('Level', i)) | |
params <- data.frame(data$price) | |
if (i >= 2) { | |
# Concat the list of fruit prices as many times as combination lengths to be calculated. | |
for (j in seq(from=2, to=i)) { | |
params <- cbind(params, data.frame(data$price)) | |
} | |
} | |
# Get combinations. | |
combinations <- expand.grid(params) | |
print(paste(nrow(combinations), 'combinations')) | |
if (i >= 2 && i <= 6) { | |
# For speed boost, process in memory and remove duplicates. Otherwise, we'll process row by row, but will need to remove dups in output. | |
combinations = t(apply(combinations, 1, sort)) | |
combinations <- combinations[!duplicated(combinations),] | |
} | |
# Name columns. | |
names(combinations) <- seq(ncol(combinations)) | |
count <- nrow(combinations) | |
print(paste(count, 'unique')) | |
# Find the combinations that add up to total money. | |
sapply(seq(count), function(index) { | |
combination <- combinations[index,] | |
# Check this combination. | |
if (sum(combination) == money) { | |
# Found a result that totals to money. Convert the price list to the fruit names and add to result. | |
order <- as.data.frame(sapply(combination, function(price) { list(data$name[which(data$price == price)])})) | |
orders <<- rbind.fill(orders, order) | |
print(paste(nrow(orders), ' results found')) | |
} | |
if (index %% 1000 == 0) { | |
print(paste((index / count) * 100, '%', sep = '')) | |
} | |
}) | |
} | |
# Output valid orders. | |
output <- sapply(seq(nrow(orders)), function(i) { | |
summary <- table(t(orders[i,])) | |
result <- '' | |
# Format: 6 kiwis | |
items <- sapply(seq(summary), function(j) { | |
count <- as.numeric(summary[j]) | |
name <- names(summary)[j] | |
# Append 's' for plural form. | |
if (count > 1) { | |
name <- paste(name, 's', sep = '') | |
} | |
# Concat the count and the fruit name. | |
item <- paste(count, name) | |
# Concat the fruits into a single string. | |
result <<- paste(result, item, sep = ', ') | |
}) | |
# Remove leading , and return string. | |
substr(result, 3, nchar(result)) | |
}) | |
# Pretty-print unique output. | |
x <- sapply(output[!duplicated(output)], print) |
This file contains hidden or 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
apple 59 | |
banana 32 | |
coconut 155 | |
grapefruit 128 | |
jackfruit 1100 | |
kiwi 41 | |
lemon 70 | |
mango 97 | |
orange 73 | |
papaya 254 | |
pear 37 | |
pineapple 399 | |
watermelon 500 |
This file contains hidden or 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] "Level 1" | |
[1] "13 combinations" | |
[1] "13 unique" | |
[1] "1 results found" | |
[1] "Level 2" | |
[1] "169 combinations" | |
[1] "91 unique" | |
[1] "Level 3" | |
[1] "2197 combinations" | |
[1] "455 unique" | |
[1] "Level 4" | |
[1] "28561 combinations" | |
[1] "1820 unique" | |
[1] "2 results found" | |
[1] "3 results found" | |
[1] "4 results found" | |
[1] "54.9450549450549%" | |
[1] "Level 5" | |
[1] "371293 combinations" | |
[1] "6188 unique" | |
[1] "16.1603102779573%" | |
[1] "4 results found" | |
[1] "32.3206205559147%" | |
[1] "48.480930833872%" | |
[1] "64.6412411118293%" | |
[1] "80.8015513897867%" | |
[1] "96.961861667744%" | |
[1] "Level 6" | |
[1] "4826809 combinations" | |
[1] "18564 unique" | |
[1] "5 results found" | |
[1] "5.38677009265245%" | |
[1] "10.7735401853049%" | |
[1] "6 results found" | |
[1] "16.1603102779573%" | |
[1] "7 results found" | |
[1] "21.5470803706098%" | |
[1] "8 results found" | |
[1] "26.9338504632622%" | |
[1] "32.3206205559147%" | |
[1] "37.7073906485671%" | |
[1] "43.0941607412196%" | |
[1] "9 results found" | |
[1] "48.480930833872%" | |
[1] "10 results found" | |
[1] "53.8677009265245%" | |
[1] "11 results found" | |
[1] "59.2544710191769%" | |
[1] "64.6412411118293%" | |
[1] "12 results found" | |
[1] "13 results found" | |
[1] "70.0280112044818%" | |
[1] "75.4147812971342%" | |
[1] "14 results found" | |
[1] "80.8015513897867%" | |
[1] "86.1883214824391%" | |
[1] "91.5750915750916%" | |
[1] "96.961861667744%" | |
[1] "1 watermelon" | |
[1] "2 apples, 1 grapefruit, 1 papaya" | |
[1] "1 apple, 1 banana, 1 coconut, 1 papaya" | |
[1] "2 bananas, 1 pear, 1 pineapple" | |
[1] "1 apple, 1 kiwi, 2 oranges, 1 papaya" | |
[1] "3 apples, 1 banana, 1 papaya, 1 pear" | |
[1] "1 apple, 1 banana, 2 kiwis, 1 orange, 1 papaya" | |
[1] "1 apple, 1 coconut, 2 lemons, 2 oranges" | |
[1] "1 apple, 1 grapefruit, 1 lemon, 1 mango, 2 oranges" | |
[1] "1 banana, 1 coconut, 1 lemon, 1 mango, 2 oranges" | |
[1] "1 banana, 1 grapefruit, 2 mangos, 2 oranges" | |
[1] "1 banana, 2 lemons, 1 papaya, 2 pears" | |
[1] "1 coconut, 1 grapefruit, 1 lemon, 1 orange, 2 pears" | |
[1] "1 coconut, 1 kiwi, 2 mangos, 1 orange, 1 pear" | |
[1] "2 grapefruits, 1 mango, 1 orange, 2 pears" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment