aboutsummaryrefslogtreecommitdiff
path: root/challenge-077
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-077')
-rw-r--r--challenge-077/lubos-kolouch/perl/ch-1.pl2
-rw-r--r--challenge-077/lubos-kolouch/perl/ch-2.pl47
-rw-r--r--challenge-077/lubos-kolouch/python/ch-1.py6
-rw-r--r--challenge-077/lubos-kolouch/python/ch-2.py55
4 files changed, 107 insertions, 3 deletions
diff --git a/challenge-077/lubos-kolouch/perl/ch-1.pl b/challenge-077/lubos-kolouch/perl/ch-1.pl
index fdf2a9f1da..c2d506cd0c 100644
--- a/challenge-077/lubos-kolouch/perl/ch-1.pl
+++ b/challenge-077/lubos-kolouch/perl/ch-1.pl
@@ -72,3 +72,5 @@ use Test::More;
is_deeply( find_solutions(6), [[5,1],[3,2,1]]);
is_deeply( find_solutions(9), [[8,1],[5,3,1]]);
is_deeply( find_solutions(-19), [[0]]);
+
+done_testing;
diff --git a/challenge-077/lubos-kolouch/perl/ch-2.pl b/challenge-077/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..8360ac008d
--- /dev/null
+++ b/challenge-077/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/bin/env perl
+#""" Perl Weekly challenge 077 Task 2 """
+#""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ """
+#""" Solution Lubos Kolouch """
+use strict;
+use warnings;
+use feature qw/say/;
+
+sub count_lone {
+ #""" count the occurences of lone X """
+
+ my $solution_count = 0;
+ my $matrix = shift;
+
+ for my $line_nr ( 0 .. scalar @$matrix - 1 ) {
+ for my $col_nr ( 0 .. scalar @$matrix - 1 ) {
+
+ next if $matrix->[$line_nr][$col_nr] eq "O";
+
+ my $is_lone = 1;
+ for my $d_x ( -1, 0, 1 ) {
+ for my $d_y ( -1, 0, 1 ) {
+ next if $d_x == 0 and $d_y == 0;
+
+ next if ( $line_nr + $d_x < 0 ) or ( $line_nr + $d_x >= scalar @$matrix );
+ next if ( $col_nr + $d_y < 0 ) or ( $col_nr + $d_y >= scalar @$matrix );
+
+ if ( $matrix->[ $line_nr + $d_x ][ $col_nr + $d_y ] eq 'X' ) {
+ $is_lone = 0;
+ last;
+ }
+ }
+ }
+ $solution_count += 1 if $is_lone;
+ }
+ }
+
+ return $solution_count;
+}
+
+
+use Test::More;
+
+is( count_lone([['O', 'O', 'X'], ['X', 'O', 'O'], ['X', 'O', 'O']]), 1);
+is( count_lone([['O', 'O', 'X', 'O'], ['X', 'O', 'O', 'O'], ['X', 'O', 'O', 'X'], ['O', 'X', 'O', 'O']]), 2);
+
+done_testing;
diff --git a/challenge-077/lubos-kolouch/python/ch-1.py b/challenge-077/lubos-kolouch/python/ch-1.py
index d181c21487..9bb92ec317 100644
--- a/challenge-077/lubos-kolouch/python/ch-1.py
+++ b/challenge-077/lubos-kolouch/python/ch-1.py
@@ -1,7 +1,7 @@
#!/bin/env python
-""" Perl Weekly challenge 077 Task 1 """
-""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ """
-""" Solution Lubos Kolouch """
+""" Perl Weekly challenge 077 Task 1
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+ Solution Lubos Kolouch """
class FibSolver:
diff --git a/challenge-077/lubos-kolouch/python/ch-2.py b/challenge-077/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..f77995658d
--- /dev/null
+++ b/challenge-077/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,55 @@
+#!/bin/env python
+""" Perl Weekly challenge 077 Task 2
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+ Solution Lubos Kolouch """
+
+
+class LoneX:
+ """ Class for solving the challenge """
+
+ def __init__(self, matrix: list):
+ """ init the solution """
+ self.matrix = matrix
+ self.solution_count = 0
+
+ def count_lone(self):
+ """ count the occurences of lone X """
+
+ for line_nr, line in enumerate(self.matrix):
+ for col_nr, char in enumerate(line):
+ if char == "O":
+ continue
+
+ is_lone = True
+ for d_x in [-1, 0, 1]:
+ for d_y in [-1, 0, 1]:
+ if d_x == d_y == 0:
+ continue
+
+ if line_nr + d_x < 0 or line_nr + d_x >= len(self.matrix):
+ continue
+
+ if col_nr + d_y < 0 or col_nr + d_y >= len(self.matrix):
+ continue
+
+ if self.matrix[line_nr + d_x][col_nr + d_y] == 'X':
+ is_lone = False
+ break
+
+ if is_lone:
+ self.solution_count += 1
+
+ def get_count(self):
+ """ just return the solutions count """
+
+ return self.solution_count
+
+
+lonex = LoneX([['O', 'O', 'X'], ['X', 'O', 'O'], ['X', 'O', 'O']])
+lonex.count_lone()
+assert lonex.get_count() == 1
+
+lonex2 = LoneX([['O', 'O', 'X', 'O'], ['X', 'O', 'O', 'O'],
+ ['X', 'O', 'O', 'X'], ['O', 'X', 'O', 'O']])
+lonex2.count_lone()
+assert lonex2.get_count() == 2