面倒くさいの恐怖

YMDです。

今週一週間↓にいい意味でも悪い意味でもハマってました。
あなたのスキルで飯は食えるか? 史上最大のコーディングスキル判定

いけるだろ、と思ってやったら全然できず、最初Javascriptでやってたのをあきらめてperl持ち替えてからはなんとか4時間くらいでできたのではないかと。

全然プログラマーじゃ食えないということか・・・orz 今からがんばるからいいんだ・・

完成してみると最初にやろうとしていた方向で間違ってなかったのに(というかあとちょっとだった)、「あーもうこの方法めんどくさすぎる、やめやめ!」ってなってしまったのが最大の敗因でした。

面倒くさい、この言葉があなたの人生からどれだけのものを奪っているか想像したことがありますか・・・

僕は子供の頃2メートル先にある雑誌をとるのがめんどくさくてもっと遠くにいる弟に「それとって」とかいって良く喧嘩をする、そんな人生を歩んできました。

面倒くさい、という言葉によって思考停止して目の前の具体的な問題にシャッターを下ろす、そういう人生を歩んでいたらいけないと思います。

そういうことをこの問題から学ぶことができました。

というわけで、メンバーのみなさんはブログを書いてください。なお、右の欄がついにおれのポストだけになったので、永年ビール無料券が発行されました。よろしくお願いします。

↓回答 九連宝燈判定したからあってる・・と思う。input_count配列がなんかエレガントでない気がする

#!/usr/bin/perl
use strict;

my $input = $ARGV[0];

$input =~ /^[1-9]{13}$/ or die "invalid input\n";

my @input_arr = split('', $input);

# 各牌の枚数
my @input_count;

foreach my $i (@input_arr) {
    if(!$input_count[$i]) {
	$input_count[$i] = 1;
    } else {
	$input_count[$i] += 1;
    }
}

&seek($input, $output, @input_count);

sub seek {
    (my $str, my $out, my @count) = @_;

    # 残り一枚なら単騎待ち
    if (length($str) < 2)  {
	$out .= "[$str]";
	print "$out\n";
	return;
    } 
    
    # 1~9で始まるジュンツ、コーツを探す
    for (my $i=1; $i <= 9; $i++) {
	# その数字の牌がもうなければ次
	next if $count[$i] eq 0;
	
	my ($ch1, $ch1_index, $ch2, $ch2_index, $ch3, $ch3_index) = ($i, -1, -1, -1, -1, -1);

	# テンパイ判定
	if (length($str) == 4) {
	    $ch1_index = $ch2_index = -1;
	    $ch1_index = index($str, $ch1);
	    $ch2_index = index($str, $ch1, $ch1_index + 1);
	    if ($ch1_index > -1 && $ch2_index > -1) {
		my $machi;
		for (my $j=0; $j < length($str); $j++) {
		    $machi .= substr($str, $j, 1) 
			if $j ne $ch1_index && $j ne $ch2_index;
		}

		print "$out\($ch1$ch1\)\[$machi\]\n" if &isMachiValid($machi);
	    }
	}

	#カブリ防止のためより大きい数字か同じメンツ以外は追加しない。その判定用
	$out =~ /\((\d\d\d)\)$/;
	my $heaviness = $1;

	# コーツ
	$ch2 = $ch3 = $ch1;
	$ch1_index = index($str, $ch1);
	$ch2_index = index($str, $ch2, $ch1_index + 1) if $ch1_index >= 0;
	$ch3_index = index($str, $ch3, $ch2_index + 1) if $ch2_index >= 0;
	if ($ch3_index > -1
	    && $count[$ch2] > 0 && $count[$ch3] > 0
	    && $ch1 * 100 + $ch2 * 10 + $ch3 >= $heaviness) {
	    &seekNext($str, $out, \@count, 
		     $ch1, $ch2, $ch3,
		     $ch1_index, $ch2_index, $ch3_index) ;
	}

	$ch1_index = $ch2_index = $ch3_index = -1;
	# ジュンツ
	if ($ch1 < 8 ) {
	    $ch2 = $ch1 + 1;
	    $ch3 = $ch1 + 2;
	    $ch1_index = index($str, $ch1);
	    $ch2_index = index($str, $ch1 + 1);
	    $ch3_index = index($str, $ch1 + 2);
	    
	    if ($ch2_index > -1 && $ch3_index > -1
		&& $count[$ch2] > 0 && $count[$ch3] > 0
		&& $ch1 * 100 + $ch2 * 10 + $ch3 >= $heaviness) {
		&seekNext($str, $out, \@count, 
			  $ch1, $ch2, $ch3,
			  $ch1_index, $ch2_index, $ch3_index);
	    }
	}
    }
}

sub isMachiValid {
    my $machi = @_[0];
    return 1 if (length($machi) < 2);
    return 0 if (length($machi) > 2);

    my $ch1 = substr($machi, 0, 1);
    my $ch2 = substr($machi, 1, 1);
    ($ch1, $ch2) = ($ch2, $ch1) if $ch1 > $ch2;

    return 1 if ($ch1 + 1 eq $ch2|| $ch1 + 2 eq $ch2 || $ch1 eq $ch2);
    return 0;
}

sub seekNext {
    (my $str, my $out, my $count_ref,
     my $ch1, my $ch2, my $ch3, 
     my $ch1_index, my $ch2_index, my $ch3_index) = @_;
    my @count = @$count_ref;

    my $nextstr = "";
    for (my $i=0; $i < length($str); $i++) {
	$nextstr .= substr($str, $i, 1) if $i ne $ch1_index 
	    && $i ne $ch2_index 
	    && $i ne $ch3_index;
    }

    my $nextout = "$out\($ch1$ch2$ch3\)";
    
    my @nextcount = @count;
    $nextcount[$ch1] -= 1; $nextcount[$ch2] -= 1; $nextcount[$ch3] -= 1;

    &seek($nextstr, $nextout, @nextcount);
}

Comments are closed.