Last active
August 29, 2015 13:57
-
-
Save hiratara/9409866 to your computer and use it in GitHub Desktop.
An answer for http://nabetani.sakura.ne.jp/hena/ord19nebasec/
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
package Hd; | |
use strict; | |
use warnings; | |
use Exporter qw(import); | |
use List::MoreUtils qw(any); | |
our @EXPORT_OK = ('solve'); | |
my $D = 192; | |
sub width ($) { | |
my $r = shift; | |
$D / ($r * 8); | |
} | |
sub range ($$) { | |
my ($r, $n) = @_; | |
my $w = width $r; | |
my $off = $w / 2; | |
if ($n == 0) { | |
[0, $off - 1], [$D - $off, $D - 1]; | |
} else { | |
my $x1 = $w * $n; | |
my $x2 = $w * ($n + 1) - 1; | |
[$x1 - $off, $x2 - $off]; | |
} | |
} | |
sub collision ($$) { | |
my ($range1, $range2) = @_; | |
$range2->[0] <= $range1->[0] && $range1->[0] <= $range2->[1] or | |
$range2->[0] <= $range1->[1] && $range1->[1] <= $range2->[1] or | |
$range1->[0] <= $range2->[0] && $range2->[0] <= $range1->[1] or | |
$range1->[0] <= $range2->[1] && $range2->[1] <= $range1->[1]; | |
} | |
sub parse ($) { [/^(\d)(\d\d)$/] } | |
sub deparse ($) { sprintf '%01d%02d', @{$_[0]}} | |
sub collisions { | |
my ($r, $ranges) = @_; | |
my @result; | |
for my $n (0 .. $r * 8 - 1) { | |
my @ranges = range $r, $n; | |
if (any { | |
my $range = $_; | |
any { collision $range, $_ } @$ranges; | |
} @ranges) { | |
push @result, deparse [$r, $n]; | |
} | |
} | |
return @result; | |
} | |
sub solve { | |
my $input = shift; | |
my @ngs = split /,/, $input; | |
my %mark; | |
for (@ngs) { | |
my ($r, $n) = @{parse $_}; | |
my $n1 = ($n + 1) % ($r * 8); | |
my $n2 = ($n - 1 + $r * 8) % ($r * 8); | |
$mark{deparse [$r, $_]}++ for $n1, $n2; | |
my @ranges = range $r, $n; | |
if ($r > 1) { | |
$mark{$_}++ for collisions $r - 1, \@ranges; | |
} | |
if ($r < 4) { | |
$mark{$_}++ for collisions $r + 1, \@ranges; | |
} | |
} | |
my @marks = grep { $mark{$_} >= 2 } sort keys %mark; | |
@marks = grep { my $m = $_; ! any {$_ == $m} @ngs } @marks; | |
@marks ? join ',', @marks : 'none'; | |
} | |
1; |
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
use strict; | |
use warnings; | |
use Test::More; | |
use Hd qw(solve); | |
while (<DATA>) { | |
tr/\r\n//d; | |
my ($n, $in, $out) = split /\t/, $_, 3; | |
is solve($in), $out, "Q. $n"; | |
} | |
done_testing; | |
__END__ | |
0 400,401,302 300,301,402 | |
1 105,100,306,414 none | |
2 100 none | |
3 211 none | |
4 317 none | |
5 414 none | |
6 100,106 107 | |
7 205,203 102,204 | |
8 303,305 304 | |
9 407,409 306,408 | |
10 104,103 207 | |
11 204,203 102,305 | |
12 313,314 209,418 | |
13 419,418 314 | |
14 100,102,101 201,203 | |
15 103,206,309 205,207,308,310 | |
16 414,310,309 206,311,413 | |
17 104,102,206,307,102,202 101,103,203,204,205,207,308 | |
18 104,206,308,409,407 103,205,207,306,307,309,408,410 | |
19 313,406,213,301,409,422,412,102,428 none | |
20 101,300,210,308,423,321,403,408,415 none | |
21 304,316,307,207,427,402,107,431,412,418,424 none | |
22 205,408,210,215,425,302,311,400,428,412 none | |
23 200,311,306,412,403,318,427,105,420 none | |
24 105,305,407,408,309,208,427 104,209,306,406 | |
25 311,304,322,404,429,305,316 203,303,321,405,406,430 | |
26 210,401,316,425,101 211,315 | |
27 414,403,404,416,428,421 303,415 | |
28 207,300,103,211,428 104,206 | |
29 322,314,310 none | |
30 427,200,215 100,323 | |
31 311,402,424,307,318,430,323,305,201 200,204,301,302,306,322,423,425,431 | |
32 425,430,408 none | |
33 202,320,209,426 319,427 | |
34 430,209,302,310,304,431,320 202,303,323 | |
35 208,206,406,424,213,312 207,311,313 | |
36 420,302,313,413,317,402 301,403 | |
37 319,306,309,418,204,411 305,307,308,412 | |
38 400,308,105,430,203,428,209 104,210,429,431 | |
39 200,305,214 215 | |
40 214,408,410,407,317,422 306,316,409,423 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment