096 Su Doku -- v2.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 12 February 2024
  4. # https://github.com/trizen
  5. # https://projecteuler.net/problem=96
  6. # Runtime: 0.555s
  7. use 5.036;
  8. sub is_valid ($board, $row, $col, $num) {
  9. # Check if the number is not present in the current row and column
  10. foreach my $i (0 .. 8) {
  11. if (($board->[$row][$i] == $num) || ($board->[$i][$col] == $num)) {
  12. return 0;
  13. }
  14. }
  15. # Check if the number is not present in the current 3x3 subgrid
  16. my ($start_row, $start_col) = (3 * int($row / 3), 3 * int($col / 3));
  17. foreach my $i (0 .. 2) {
  18. foreach my $j (0 .. 2) {
  19. if ($board->[$start_row + $i][$start_col + $j] == $num) {
  20. return 0;
  21. }
  22. }
  23. }
  24. return 1;
  25. }
  26. sub find_empty_locations ($board) {
  27. my @locations;
  28. # Find all empty positions (cells with 0)
  29. foreach my $i (0 .. 8) {
  30. foreach my $j (0 .. 8) {
  31. if ($board->[$i][$j] == 0) {
  32. push @locations, [$i, $j];
  33. }
  34. }
  35. }
  36. return @locations;
  37. }
  38. sub find_empty_location ($board) {
  39. # Find an empty position (cell with 0)
  40. foreach my $i (0 .. 8) {
  41. foreach my $j (0 .. 8) {
  42. if ($board->[$i][$j] == 0) {
  43. return ($i, $j);
  44. }
  45. }
  46. }
  47. return (undef, undef); # If the board is filled
  48. }
  49. sub solve_sudoku_fallback ($board) {
  50. my ($row, $col) = find_empty_location($board);
  51. if (!defined($row) && !defined($col)) {
  52. return 1; # Puzzle is solved
  53. }
  54. foreach my $num (1 .. 9) {
  55. if (is_valid($board, $row, $col, $num)) {
  56. # Try placing the number
  57. $board->[$row][$col] = $num;
  58. # Recursively try to solve the rest of the puzzle
  59. if (__SUB__->($board)) {
  60. return 1;
  61. }
  62. # If placing the current number doesn't lead to a solution, backtrack
  63. $board->[$row][$col] = 0;
  64. }
  65. }
  66. return 0; # No solution found
  67. }
  68. sub solve_sudoku ($board) {
  69. while (1) {
  70. # Return early when the first 3 values are solved
  71. if ($board->[0][0] != 0 and $board->[0][1] != 0 and $board->[0][2] != 0) {
  72. return $board;
  73. }
  74. my @empty_locations = find_empty_locations($board);
  75. if (not @empty_locations) {
  76. last; # solved
  77. }
  78. my $found = 0;
  79. # Solve easy cases
  80. foreach my $ij (@empty_locations) {
  81. my ($i, $j) = @$ij;
  82. my ($count, $value) = (0, 0);
  83. foreach my $n (1 .. 9) {
  84. is_valid($board, $i, $j, $n) || next;
  85. last if (++$count > 1);
  86. $value = $n;
  87. }
  88. if ($count == 1) {
  89. $board->[$i][$j] = $value;
  90. $found ||= 1;
  91. }
  92. }
  93. next if $found;
  94. # Solve more complex cases
  95. my @stats;
  96. foreach my $ij (@empty_locations) {
  97. my ($i, $j) = @$ij;
  98. $stats[$i][$j] = [grep { is_valid($board, $i, $j, $_) } 1 .. 9];
  99. }
  100. my (@rows, @cols, @subgrid);
  101. foreach my $ij (@empty_locations) {
  102. my ($i, $j) = @$ij;
  103. foreach my $v (@{$stats[$i][$j]}) {
  104. ++$cols[$j][$v];
  105. ++$rows[$i][$v];
  106. ++$subgrid[3 * int($i / 3)][3 * int($j / 3)][$v];
  107. }
  108. }
  109. foreach my $ij (@empty_locations) {
  110. my ($i, $j) = @$ij;
  111. foreach my $v (@{$stats[$i][$j]}) {
  112. if ( $cols[$j][$v] == 1
  113. or $rows[$i][$v] == 1
  114. or $subgrid[3 * int($i / 3)][3 * int($j / 3)][$v] == 1) {
  115. $board->[$i][$j] = $v;
  116. $found ||= 1;
  117. }
  118. }
  119. }
  120. next if $found;
  121. say "Fallback: ", scalar(@empty_locations);
  122. solve_sudoku_fallback($board);
  123. return $board;
  124. }
  125. return $board;
  126. }
  127. open my $fh, '<:raw', ($ARGV[0] // 'p096_sudoku.txt')
  128. or die "Can't open file `p096_sudoku.txt`: $!";
  129. chomp(my @grids = grep { /^[0-9]+$/ } <$fh>);
  130. close $fh;
  131. my $sum = 0;
  132. while (@grids) {
  133. my @grid = map { [split(//, $_)] } splice(@grids, 0, 9);
  134. my $solution = solve_sudoku(\@grid);
  135. $sum += "$solution->[0][0]$solution->[0][1]$solution->[0][2]";
  136. }
  137. say $sum;