Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/perl
- # Author: Longree (2015-07-03)
- # Computes answer to riddle: https://brilliant.org/problems/the-boxes/
- # ( if my logic below is correct )
- use warnings;
- use strict;
- # Box state matrix
- # [box1, box2, box3] (1 if object in box)
- my @boxstates = (
- [1, 0, 0],
- [0, 1, 0],
- [0, 0, 1]
- );
- # Test each box state
- my $viablecount = 0;
- for my $boxstate ( @boxstates )
- {
- my $result = &runtest(@{$boxstate});
- printf("[%s] - %s\n", join(":", @{$boxstate}),
- ($result eq 1) ? "Viable" : "Not viable");
- $viablecount++ if ($result == 1);
- }
- # Output Result
- if($viablecount < 1)
- {
- print "None of the above :(\n";
- }
- elsif($viablecount == 1)
- {
- print "One case exists! See above :)\n";
- }
- elsif(1 < $viablecount)
- {
- print "Not enough information :(";
- }
- exit 0;
- # Run test for given set of box states
- sub runtest()
- {
- my @boxstates = @_;
- my ($box1state, $box2state, $box3state) = @boxstates;
- my ($falsecount, $truecount) = (0, 0);
- &isinbox1($box1state) ? $truecount++ : $falsecount++;
- ¬inbox2($box2state) ? $truecount++ : $falsecount++;
- ¬inbox1($box1state) ? $truecount++ : $falsecount++;
- return (($falsecount == 2) && ($truecount == 1));
- }
- # statements 1
- sub isinbox1()
- {
- my $box1state = shift;
- return ($box1state == 1);
- }
- # statement 2
- sub notinbox2()
- {
- my $box2state = shift;
- return ($box2state == 0);
- }
- # statement 3
- sub notinbox1()
- {
- my $box1state = shift;
- return ($box1state == 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement