Skip to content

Instantly share code, notes, and snippets.

@davorg
Last active April 25, 2020 13:56
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 davorg/aa81954372c454984f5674b8099a8103 to your computer and use it in GitHub Desktop.
Save davorg/aa81954372c454984f5674b8099a8103 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my $target = 92;
my @prices = (35, 46, 19, 7, 41, 27);
# If we have command-line arguments
if (@ARGV) {
# Get the first argument as the target
$target = shift;
}
# If we still have command-line arguments
if (@ARGV) {
# Use them as the list of numbers
@prices = @ARGV;
}
for my $x (@prices) {
for my $y (@prices) {
for my $z (@prices) {
next if $x == $y or $x == $z or $y == $z;
if ($x + $y + $z == $target) {
say "The answer is $x + $y + $z = ", $x + $y + $z;
exit;
}
}
}
}
say "There is no solution.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment