Skip to content

Instantly share code, notes, and snippets.

@perforb
Created August 8, 2012 13:22
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 perforb/3294987 to your computer and use it in GitHub Desktop.
Save perforb/3294987 to your computer and use it in GitHub Desktop.
Ants
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use constant LENGTH => 10;
my ($n, $min, $max) = (shift @ARGV || 8, 0, 0);
my @positions = ();
push @positions, int(rand(LENGTH + 1)) for (1 .. $n);
for my $position (@positions) {
$min = max($min, min($position, LENGTH - $position));
$max = max($max, max($position, LENGTH - $position));
}
say '[', join(', ', @positions), ']';
printf("MIN:%d MAX:%d\n", $min, $max);
sub max { $_[0] > $_[1] ? $_[0] : $_[1] }
sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
#!/usr/bin/env python
LENGTH = 10
positions = (2, 6, 8)
t_min, t_max = 0, 0
for pos in positions:
t_min = max(t_min, min(pos, LENGTH - pos))
t_max = max(t_max, max(pos, LENGTH - pos))
print "MIN:" + str(t_min)
print "MAX:" + str(t_max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment