Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Created October 12, 2020 19:54
Show Gist options
  • Save Gerst20051/e458580ad748669577476adef9737f65 to your computer and use it in GitHub Desktop.
Save Gerst20051/e458580ad748669577476adef9737f65 to your computer and use it in GitHub Desktop.
Grow Gardens
// You are a botanist growing gardens of flowers for a picky customer. Your customer wants only red flowers but the seeds for your red flowers and your blue flowers have become mixed up. You are going to use all of your available gardens to grow flowers and then sell all of your red flowers to your customer.
// A few important points
// - Your soil quality is poor and successfully grown flowers will crowd out other seeds from growing easily. Thus the first seed you plant has a 75% chance of growing a flower but every subsequently planted seed only has a 25% chance of growing into a plant. Once a seed has failed to grow in a particular garden, you are going to abandon planting any additional seeds in it as it is an indication that the soil quailty is not conducive to growing any additional plants.
// - Red flowers are particularly competitive to neighboring plants. After a red flower grows in a garden, the garden will no longer be able to grow any additional plants and thus you will wholly abandon planting additional seeds in it.
// - You had an equal number of red seeds and blue seeds. I.e., a grown plant will have a 50% chance of growing a red flower and 50% chance of growing a blue flower.
// Here are some examples of potential gardens you will grow:
// - Garden 1: Blue, Blue
// - Garden 2: Blue
// - Garden 3: Red
// - Garden 4: Blue, Red
// - Garden 5:
// - Garden 6: Blue, Blue, Red
// These are examples of gardens that are not possible:
// - Garden 1: Red, Blue
// - Garden 2: Red, Red
// - Garden 3: Blue, Red, Blue
// =============================================================================================================================
// Part 1
// Your task is to write a program to use all "n" of your gardens to grow flowers. Start by implmenting a method that takes in the number of gardens to be grown and returns a data structure holding each garden's flowers.
// def grow_gardens(number_of_gardens)
// end
// Part 2
// Using the output of your function that grows gardens, calculate the following:
// 1. Ratio of red to blue flowers
// 2. Avg flowers per garden
// 3. Avg red per garden
// 4. Avg blue per garden
function grow_gardens(numberOfGardens) {
const gardens = [];
for (let i = 0; i < numberOfGardens; i++) {
const flowers = [];
while (flowers[flowers.length - 1] !== 'Red') {
const flower = get_seed(flowers.length === 0 ? .75 : .25);
if (!flower) break;
flowers.push(flower);
}
gardens.push(flowers);
}
return gardens;
}
function get_seed(probability) {
const doesBloom = Math.random() < probability;
if (!doesBloom) return;
const seedOptions = ['Red', 'Blue'];
return seedOptions[Math.random() < .5 ? 0 : 1];
}
function analyze_gardens(gardens) {
return {
ratio: calculate_ratio(gardens),
avgCount: calculate_average_count(gardens),
avgRed: calculate_average_red(gardens),
avgBlue: calculate_average_blue(gardens),
};
}
function calculate_ratio(gardens) {
return calculate_average_red(gardens) / calculate_average_blue(gardens);
}
function calculate_average_count(gardens) {
return average(gardens.map(garden => garden.length));
}
function calculate_average_red(gardens) {
return average(gardens.map(garden => garden.filter(flower => flower === 'Red').length));
}
function calculate_average_blue(gardens) {
return average(gardens.map(garden => garden.filter(flower => flower === 'Blue').length));
}
const average = (array) => array.reduce((a, b) => a + b) / array.length;
const gardens = grow_gardens(4);
const stats = analyze_gardens(gardens);
console.log(gardens, stats);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment