#!/usr/bin/perl -l use strict; use warnings; use feature qw(say); use List::Util qw(sum max); use Data::Dumper; use lib 'lib'; use Base::Nifty qw(commify); say "How many children are in the first generation?"; my $generation = <>; say "How many generations do you want to generate?"; my $generations = <>; chomp($generation,$generations); my %generations = ( 1 => { children => $generation, total_population => $generation, } ); my @distribution = (0,0,0,1,1,1,2,2,2,3,3,4,5,6); for my $gen (2..$generations) { my $children = 0; for (1..$generation) { $children += $distribution[rand(@distribution)]; } $generation = $children; last if ($children == 0); $generations{$gen}{children} = $generation; my $total_population = $generations{$gen}{children}; $total_population += $generations{$gen - 1}{children} if $generations{$gen - 1}; $total_population += $generations{$gen - 2}{children} if $generations{$gen - 2}; $generations{$gen}{total_population} = $total_population; } my @children; my @population; for (1..$generations) { push @children, $generations{$_}{children}; push @population, $generations{$_}{total_population}; } my $max_length_generation = length(commify(max(keys %generations))); my $max_length_children = length(commify(max(@children))); my $max_length_population = length(commify(max(@population))); for (sort {$a <=> $b} keys %generations) { printf "%${max_length_generation}s: %${max_length_children}s %${max_length_population}s\n", $_,commify($generations{$_}{children}),commify($generations{$_}{total_population}); }