aboutsummaryrefslogtreecommitdiff
path: root/challenge-086/alexander-pankoff/perl/ch-2.pl
blob: 1314a2c1b1f72159ce0195eacc3456f757e9b166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env perl
use v5.20;
use utf8;
use strict;
use warnings;
use feature qw(say signatures);
no warnings 'experimental::signatures';

use List::Util qw(all any);
use Storable qw(dclone);

use Getopt::Long qw(:config auto_help);
use Pod::Usage;

{

    my $INPUT = slurp(*STDIN);

    my @SUDOKU = map {
        [ map { $_ eq '_' ? undef : $_ } grep { /[0-9_]/ } split( /\s*/, $_ ) ]
    } split( /\n/, $INPUT );

    pod2usage( -message => "Invalid input", -exitval => 1 )
      unless all { @$_ == 9 } @SUDOKU;

    my $solved = solve( \@SUDOKU );

    die "no solution found\n" unless $solved;
    say render_sudoku($solved);
}

sub render_sudoku($sudoku) {
    join(
        "\n",
        map {
            join( ' ', '[', map { $_ // '_' } @$_, ']' )
        } @$sudoku
    );
}

sub solve($sudoku) {
    return $sudoku if solved($sudoku);

    my %cache;
    while ( my %next = next_free_position( $sudoku, \%cache ) ) {
        my $row = $next{row};
        my $col = $next{col};
        $cache{ $row . $col } = 1;

        for my $candidate ( @{ $next{domain} } ) {
            my $test = [@$sudoku];
            $test->[$row] = [ @{ $test->[$row] } ];
            $test->[$row][$col] = $candidate;
            my $solved = solve($test);
            return $solved if $solved;
        }
    }
}

sub next_free_position ( $sudoku, $cache ) {
    my %min;
    for my $row ( 0 .. 8 ) {
        for my $col ( 0 .. 8 ) {
            next if defined $sudoku->[$row][$col] || $cache->{ $row . $col };
            my @domain = get_candidates( $row, $col, $sudoku );
            return if !@domain;

            if ( !%min || @domain < @{ $min{domain} } ) {
                %min = (
                    row    => $row,
                    col    => $col,
                    domain => \@domain,
                );
            }
        }
    }

    return %min;
}

sub get_candidates ( $row, $col, $sudoku ) {
    intersection(
        [ col_candidates( $col, $sudoku ) ],
        [ row_candidates( $row, $sudoku ) ],
        [ box_candidates( $row, $col, $sudoku ) ]
    );
}

sub solved($sudoku) {
    all { !row_candidates( $_, $sudoku ) } 0 ... 8;
}

sub row_candidates ( $row, $sudoku ) {
    _missing( @{ $sudoku->[$row] } );
}

sub col_candidates ( $col, $sudoku ) {
    _missing( map { $_->[$col] } @{$sudoku} );
}

sub box_candidates ( $row, $col, $sudoku ) {

    my $box_start_row = int( $row / 3 ) * 3;
    my $box_start_col = int( $col / 3 ) * 3;

    my @elems =
      map { @{$_}[ $box_start_col .. $box_start_col + 2 ] }
      ( @{$sudoku}[ $box_start_row .. $box_start_row + 2 ] );
    return _missing(@elems);
}

sub _missing(@elems) {
    state $domain = [ 1 .. 9 ];
    difference( $domain, \@elems );
}

sub difference ( $a, $b ) {
    my %b_lookup = map { $_ ? ( $_ => 1 ) : () } @$b;
    grep { !$b_lookup{$_} } @$a;
}

sub intersection ( $a, $b, @more ) {
    my %a_lookup = map { $_ => 1 } @$a;
    my @res =
      grep {
        my $b_elem = $_;
        $a_lookup{$b_elem};
      } @$b;

    return @res if !@more;
    return intersection( \@res, @more );
}

sub slurp($fh) {
    local $/ = undef;
    my $out = <$fh>;
    return $out;
}

=pod

=head1 NAME

wk-086 ch-2 - Sudoku Puzzle

=head1 SYNOPSIS

ch-2.pl [options]

  This programm will complete the given Sudoku Puzzle

  Options:
    --help       print this help text

=cut