Skip to content

Instantly share code, notes, and snippets.

@amatubu
Created March 15, 2013 11: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 amatubu/5169391 to your computer and use it in GitHub Desktop.
Save amatubu/5169391 to your computer and use it in GitHub Desktop.
Find spices which is compatible with Minnlabic
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
my %spices;
my %blends;
# ブレンドリストを開く
open my $blend_file, "blendlist.txt";
die "ブレンドリストが開けません。" if !$blend_file;
# ブレンドのカウント
while ( my $blend = <$blend_file> ) {
if ( $blend =~ m/(\w+)\s(\w+)/ ) {
my ( $spice1, $spice2 ) = sort ( $1, $2 );
$spices{$spice1}++;
$spices{$spice2}++;
# マトリックス
$blends{$spice1}{$spice2} = 1;
$blends{$spice2}{$spice1} = 1;
}
}
# カウント結果を表示
my ( $min, $max );
my ( $min_spice, $max_spice );
open my $spicelist_file, ">", "spicelist.txt";
for my $spice ( sort keys %spices ) {
printf $spicelist_file "%12s %2d\n", $spice, $spices{$spice};
if ( !defined( $min ) || $min > $spices{$spice} ) {
$min = $spices{$spice};
$min_spice = $spice;
}
if ( !defined( $max ) || $max < $spices{$spice} ) {
$max = $spices{$spice};
$max_spice = $spice;
}
}
close $spicelist_file;
# 相性のよいスパイスが最も多いスパイス
printf " Max : %12s (%2d)\n", $max_spice, $max;
# 相性のよいスパイスが最も少ないスパイス
printf " Min : %12s (%2d)\n", $min_spice, $min;
print "\n";
# 相性のよいスパイスが最も多いスパイスと共通部分が多いスパイスを探す
for my $spice1 ( sort keys %spices ) {
my $count = $blends{$max_spice}{$spice1} || 0;
for my $spice2 ( sort keys %spices ) {
if ( $spice1 ne $spice2 ) {
if ( defined( $blends{$max_spice}{$spice2} ) && defined( $blends{$spice1}{$spice2} ) ) {
$count++;
}
}
}
if ( $count >= $min ) {
printf " %12s (%2d)\n", $spice1, $count;
}
}
# 相性のよいスパイスが最も多いスパイスと比較して、共通して相性のよいスパイスの
# 数が、最も少ないスパイスの数よりも多いものを探す(ここで見つかったものは同じ
# 皿に入れる)
my %group1;
find_same_plate( $max_spice );
# 結果を出力
print join " ", ( sort keys %group1 );
print "\n";
# マトリックス表を作成2
my %spice_blend_matrix2;
open my $matrix_file2, ">", "matrix2.txt";
for my $spice1 ( sort { ( $group1{$a} || 0 ) <=> ( $group1{$b} || 0 ) } keys %spices ) {
for my $spice2 ( sort { ( $group1{$a} || 0 ) <=> ( $group1{$b} || 0 ) } keys %spices ) {
if ( $spice1 ne $spice2 ) {
$spice_blend_matrix2{$spice1} .= $blends{$spice1}{$spice2} || "0";
} else {
$spice_blend_matrix2{$spice1} .= " ";
}
}
print $matrix_file2 "$spice_blend_matrix2{$spice1} $spice1 ($spices{$spice1})\n";
}
close $matrix_file2;
sub find_same_plate {
my ( $spice ) = shift;
for my $spice1 ( sort keys %spices ) {
my $count = $blends{$spice}{$spice1} || 0;
for my $spice2 ( sort keys %spices ) {
if ( $spice1 ne $spice2 ) {
if ( defined( $blends{$spice}{$spice2} ) && defined( $blends{$spice1}{$spice2} ) ) {
$count++;
}
}
}
if ( $count >= $min && !defined( $group1{$spice1} ) ) {
printf " %12s (%2d)\n", $spice1, $count;
$group1{$spice1} = $count;
}
}
}
exit 0;
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment