Created
April 12, 2012 15:44
-
-
Save niratama/2368443 to your computer and use it in GitHub Desktop.
9個中1個の重さの違う肉まんを探す(from https://twitter.com/#!/panta_rhei2004/status/190430047675486208)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!env perl | |
use 5.010; | |
use List::Util qw(sum); | |
my @m = @ARGV; | |
if (sum(@m[0..2]) == sum(@m[3..5])) { # 0〜2と3〜5はあんまん | |
if ($m[6] == $m[7]){ # 6と7もあんまん | |
say 8; | |
} else { # 6か7どちらかが肉まん | |
if ($m[6] == $m[8]) { # 6と8はあんまん | |
say 7; | |
} else { # 6は7や8と違う=肉まん | |
say 6; | |
} | |
} | |
} elsif (sum(@m[0..2]) < sum(@m[3..5])) { # 0〜2と3〜5どちらかに肉まんがある、6〜8はあんまん | |
if (sum(@m[0..2]) == sum(@m[6..8])) { # 0〜2はあんまんなので3〜5に重い肉まんがある | |
if ($m[3] == $m[4]) { # 3と4はあんまん | |
say 5; | |
} elsif ($m[3] < $m[4]) { # 4が重いので肉まん | |
say 4; | |
} else { # 3が重いので肉まん | |
say 3; | |
} | |
} else { # 0〜2は3〜5と6〜8とは違う=軽い肉まんがある | |
if ($m[0] == $m[1]) { # 0と1はあんまん | |
say 2; | |
} elsif ($m[0] < $m[1]) { # 0が軽いので肉まん | |
say 0; | |
} else { # 1が軽いので肉まん | |
say 1; | |
} | |
} | |
} else { # 0〜2と3〜5どちらかに肉まんがある、6〜8はあんまん | |
if (sum(@m[0..2]) == sum(@m[6..8])) { # 0〜2はあんまんなので3〜5に軽い肉まんがある | |
if ($m[3] == $m[4]) { # 3と4はあんまん | |
say 5; | |
} elsif ($m[3] < $m[4]) { # 3が軽いので肉まん | |
say 3; | |
} else { # 4が軽いので肉まん | |
say 4; | |
} | |
} else { # 0〜2は3〜5と6〜8とは違う=重い肉まんがある | |
if ($m[0] == $m[1]) { # 0と1はあんまん | |
say 2; | |
} elsif ($m[0] < $m[1]) { # 1が重いので肉まん | |
say 1; | |
} else { # 0が重いので肉まん | |
say 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment