Skip to content

Instantly share code, notes, and snippets.

@hiratara
Last active August 29, 2015 14:07
Show Gist options
  • Save hiratara/22e2aa5b2df81790c561 to your computer and use it in GitHub Desktop.
Save hiratara/22e2aa5b2df81790c561 to your computer and use it in GitHub Desktop.
package Solver;
use strict;
use warnings;
no warnings 'experimental';
use feature qw(signatures postderef);
use Exporter qw(import);
use List::MoreUtils qw(all);
our @EXPORT_OK = qw(solve);
my $board = <<__BOARD__;
a
bcd
efghi
jklmnop
qrstuvwxy
__BOARD__
my @board = map { [split //, $_] } split /\n/, $board;
my %position;
for my $y (0 .. $#board) {
for my $x (0 .. $board[$y]->$#*) {
if (my $label = $board[$y][$x]) {
$position{$label} = [$x, $y];
}
}
}
sub get_position :prototype($) ($label) {
$position{$label};
}
sub is_up :prototype($) ($label) {
my $position = get_position($label);
($position->[0] + $position->[1]) % 2 == 0;
}
sub lines :prototype($) ($label) {
my $pos = get_position $label;
is_up $label ? (
[$pos->[0] + 1, $pos->[1]],
[$pos->[0] , $pos->[1]],
[$pos->[0] , $pos->[1] + 1]
)
: (
[$pos->[0] , $pos->[1]],
[$pos->[0] + 1, $pos->[1]],
[$pos->[0] , $pos->[1]]
);
}
sub toggle :prototype($$) ($counts, $label) {
my @lines = lines $label;
for my $i (0 .. $#lines) {
my $key = join '-', $lines[$i]->@*;
$counts->[$i]{$key} = ! $counts->[$i]{$key};
}
}
sub solve :prototype($) ($input) {
my $counts = [{}, {}, {}];
toggle $counts, $_ for split //, $input;
join ',', map {
my $set = $_;
scalar grep { $set->{$_} } keys %$set;
} @$counts;
}
1;
package SolverOld;
use strict;
use warnings;
no warnings 'experimental';
use feature qw(signatures postderef);
use Exporter qw(import);
use List::MoreUtils qw(all);
our @EXPORT_OK = qw(solve);
my $board = <<__BOARD__;
a
bcd
efghi
jklmnop
qrstuvwxy
__BOARD__
my @board = map { [split //, $_] } split /\n/, $board;
my %position;
for my $y (0 .. $#board) {
for my $x (0 .. $board[$y]->$#*) {
if (my $label = $board[$y][$x]) {
$position{$label} = [$x, $y];
}
}
}
sub get_position :prototype($) ($label) {
$position{$label};
}
sub get_label :prototype($) ($pos) {
all { $_ >= 0 } $pos->@* or return ' ';
$board[$pos->[1]][$pos->[0]] // ' ';
}
sub is_up :prototype($) ($label) {
my $position = get_position($label);
($position->[0] + $position->[1]) % 2 == 0;
}
sub solve :prototype($) ($input) {
my %labels = map { $_ => 1 } split //, $input;
my @answer = (0, 0, 0);
for my $label (keys %labels) {
my $pos = get_position $label;
my $is_up = is_up $label;
$labels{get_label([$pos->[0] + 1, $pos->[1]])} or $answer[$is_up ? 0 : 1]++;
$labels{get_label([$pos->[0] - 1, $pos->[1]])} or $answer[$is_up ? 1 : 0]++;
$labels{get_label([$pos->[0], $pos->[1] + ($is_up ? 1 : -1)])} or $answer[2]++;
}
return join ',', @answer;
}
1;
use strict;
use warnings;
use v5.20;
use Solver qw(solve);
use Test::More;
while (<DATA>) {
tr/\r\n//d;
my ($no, $input, $expected) = split /\t/, $_;
is solve($input), $expected, "TEST $no";
}
done_testing;
__END__
0 bdelmnouy 5,7,9
1 a 1,1,1
2 q 1,1,1
3 t 1,1,1
4 i 1,1,1
5 fg 2,0,2
6 gh 0,2,2
7 gm 2,2,0
8 fgh 1,1,3
9 fghm 2,2,2
10 fhm 3,3,3
11 bdfhjprx 8,8,0
12 abcdfghm 4,4,0
13 jklmqrst 0,4,4
14 klmntuvw 4,0,4
15 abcdefghijklmnopqrstuvwxy 5,5,5
16 abcdefghijklmnoqrtvwxy 6,8,4
17 abdefhijklnoprstvwxy 10,8,4
18 acegikmoqsuwy 13,13,5
19 bdfhjlnprtvxy 13,11,1
20 abdegijlnpqsuwy 15,15,15
21 aefghiqrstuvwxy 3,3,15
22 cfhkmoqrstuvwxy 7,7,15
23 cfhkmortvx 10,10,10
24 no 0,2,2
25 pwy 3,3,3
26 iqwy 4,4,4
27 lopuv 3,3,5
28 abdjtw 6,6,6
29 fgpstux 5,3,5
30 dijlnotv 6,8,2
31 bdefkmpwx 5,9,3
32 bfghjlmuwx 4,8,6
33 befghlopqrw 5,7,9
34 bfgjklmnqsux 8,6,8
35 fijklnpqstvwy 9,9,9
36 abcdfgilmnrsuv 8,6,6
37 abcdegijklnpruw 11,11,9
38 efgijkmnopqrtvwx 6,8,4
39 abcdefghilopqrtwy 9,9,7
40 abfghklmopqrsuvwxy 8,6,12
41 abcdeghklmoprstuwxy 9,7,7
42 abcdehijklmnopqrtwxy 8,8,6
43 acdefghimnopqrstuvwxy 7,3,9
44 abcfghijklmnopqrtuvwxy 6,6,6
45 abcdefghijklmnoqrstuwxy 5,7,7
46 abcdeghijklmnopqrstuvwxy 6,6,6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment