Permalink
Browse files

initial Sudoku code

  • Loading branch information...
0 parents commit eacbfbda9e8155553f5266f894edc11675b2eb01 @moritz committed Sep 11, 2010
Showing with 55 additions and 0 deletions.
  1. +55 −0 lib/Sudoku.pm
@@ -0,0 +1,55 @@
+use v6;
+
+class Sudoku::Constraint {
+ has @.x;
+ has @.y; # ;;
+ has %.remaining-symbols handles delete-symbol => 'delete';
+}
+
+class Sudoku {
+ has $.block-size = 3;
+ has $.size = $.block-size ** 2;
+ has @.rows;
+
+ has @!maybe;
+ has @!constraints;
+
+ method from-string($s) {
+ my $o = self.new(rows => $s.comb(/.**9/).map: { [ .comb.map: +* ] });
+ $o.init();
+ $o;
+ }
+
+ method Str {
+ @!rows.map({ .map({ $_ == 0 ?? '.' !! $_ }).join ~ "\n" }).join;
+ }
+
+ method init() {
+ for ^$!size X ^$!size -> $x, $y {
+ $constraints[$y][$x] = [ True xx $!size ];
+ }
+ for ^$!size {
+ # rows
+ @!constraints.push: Sudoku::Constraint.new(
+ x => ^$.size,
+ y => $_ xx $!size,
+ remaining-symbols => hash( 1..$!size Z=> True xx * ),
+ );
+ # columns
+ @!constraints.push: Sudoku::Constraint.new(
+ x => $_ xx $.size,
+ y => ^$.size,
+ remaining-symbols => hash( 1..$!size Z=> True xx * ),
+ );
+ }
+ for ^$!block-size X ^$!block-size -> $x, $y {
+ @!constraints.push: Sudoku::Constraint.new(
+ x => (^$!block-size X+ ($x * $!block-size)) xx $!block-size,
+ y => ((^$!block-size Xxx $!block-size )X+ ($y * $!block-size)),
+ remaining-symbols => hash( 1..$!size Z=> True xx * ),
+ );
+ }
+ }
+}
+
+# vim: ft=perl6

0 comments on commit eacbfbd

Please sign in to comment.