Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Last active February 19, 2016 04:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AlexDaniel/b952b73284a083973802 to your computer and use it in GitHub Desktop.
reversi.p6
#!/usr/bin/env perl6
my $mine = get;
my $his = $mine eq X ?? O !! X;
my @map = lines».comb».Array;
my $solutions = 0;
for |@map, |(@map[*;$_] for ^@map),
# ↑ straight lines
|((@map[ .[0];.[1]] for $_ Z .reverse) for |(0 X.. ^@map), |((0 X.. ([R,] 0..^(@map-1))) Z+ 1..^@map ) ),
# ↑ / diagonals
|((@map[@map - 1 - .[0];.[1]] for $_ Z .reverse) for |(0 X.. ^@map), |((0 X.. ([R,] 0..^(@map-1))) Z+ 1..^@map ) )
# ↑ \ diagonals
-> $row {
my $state = 0;
for |$row, |$row.reverse <-> $cell {
given $cell {
when $mine { $state = 1 }
when $his { $state = 2 if $state == 1 }
when - { if $state == 2 { $cell = *; $solutions++ }; $state = 0 }
default { $state = 0 }
}
}
}
say $solutions legal moves for $mine;
say [~] @$_ for @map;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment