aboutsummaryrefslogtreecommitdiff
path: root/challenge-077/ash
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-09-11 01:19:26 +1000
committerMyoungjin JEON <jeongoon@gmail.com>2020-09-11 01:19:26 +1000
commitcee930fd1fd0a3f0fb9a2caf6aa578d569c37fce (patch)
treeb467bb1ef9efad41198811efaa7bd268686fe766 /challenge-077/ash
parent20fc303d563e660dcd790ccb7baa894d77de5695 (diff)
parentbac73b38eb313b198a7a03e3199926737dca7277 (diff)
downloadperlweeklychallenge-club-cee930fd1fd0a3f0fb9a2caf6aa578d569c37fce.tar.gz
perlweeklychallenge-club-cee930fd1fd0a3f0fb9a2caf6aa578d569c37fce.tar.bz2
perlweeklychallenge-club-cee930fd1fd0a3f0fb9a2caf6aa578d569c37fce.zip
Merge remote-tracking branch 'upstream/master' into ch-077
Diffstat (limited to 'challenge-077/ash')
-rw-r--r--challenge-077/ash/blog.txt1
-rw-r--r--challenge-077/ash/blog1.txt1
-rw-r--r--challenge-077/ash/cpp/ch-2.cpp60
-rw-r--r--challenge-077/ash/raku/ch-1.raku11
-rw-r--r--challenge-077/ash/raku/ch-2.raku36
-rw-r--r--challenge-077/ash/xslt/ch-2.xml25
-rw-r--r--challenge-077/ash/xslt/ch-2.xslt41
7 files changed, 175 insertions, 0 deletions
diff --git a/challenge-077/ash/blog.txt b/challenge-077/ash/blog.txt
new file mode 100644
index 0000000000..f95ba9e75f
--- /dev/null
+++ b/challenge-077/ash/blog.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2020/09/07/add-up-fibonacci-numbers-the-weekly-challenge-77-task-1/
diff --git a/challenge-077/ash/blog1.txt b/challenge-077/ash/blog1.txt
new file mode 100644
index 0000000000..a81f60f4d3
--- /dev/null
+++ b/challenge-077/ash/blog1.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
diff --git a/challenge-077/ash/cpp/ch-2.cpp b/challenge-077/ash/cpp/ch-2.cpp
new file mode 100644
index 0000000000..2c7aa19721
--- /dev/null
+++ b/challenge-077/ash/cpp/ch-2.cpp
@@ -0,0 +1,60 @@
+/*
+ Task 2 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+ Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
+
+ Compile as:
+ $ g++ -std=c++17 ch-2.cpp
+
+ Output for the given example of matrix:
+ $ ./a.out
+ 0, 3
+ 1, 1
+ 2, 3
+*/
+
+#include <iostream>
+#include <vector>
+
+using namespace std;
+
+vector<int> test_move(vector<vector<char>> matrix, vector<int> current, vector<int> shift) {
+ current[0] += shift[0];
+ current[1] += shift[1];
+
+ if (current[0] < 0 || current[0] >= matrix.size() ||
+ current[1] < 0 || current[1] >= matrix[0].size()) {
+ return vector<int>();
+ }
+ else {
+ return current;
+ }
+}
+
+int main() {
+ vector<vector<char>> matrix = {
+ {'O', 'O', 'O', 'X'},
+ {'O', 'X', 'O', 'O'},
+ {'O', 'O', 'O', 'X'}
+ };
+
+ vector<vector<int>> neighbours = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
+
+ for (auto row = 0; row != matrix.size(); row++) {
+ for (auto col = 0; col != matrix[0].size(); col++) {
+ if (matrix[row][col] == 'O') continue;
+
+ bool ok = true;
+ for (auto neighbour : neighbours) {
+ auto move = test_move(matrix, vector<int>{row, col}, neighbour);
+ if (move.empty()) continue;
+ if (matrix[move[0]][move[1]] == 'X') {
+ ok = false;
+ break;
+ }
+ }
+ if (ok) cout << row << ", " << col << endl;
+ }
+ }
+}
diff --git a/challenge-077/ash/raku/ch-1.raku b/challenge-077/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..cdab8466cc
--- /dev/null
+++ b/challenge-077/ash/raku/ch-1.raku
@@ -0,0 +1,11 @@
+#!/usr/bin/env raku
+
+# Task 1 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+# Comments: https://andrewshitov.com/2020/09/07/add-up-fibonacci-numbers-the-weekly-challenge-77-task-1/
+
+my $n = @*ARGS[0] // 42;
+my @fib = 1, 2, * + * ...^ * > $n;
+
+"$_.join(' + ') = $n".put for @fib.combinations.grep(*.sum == $n);
diff --git a/challenge-077/ash/raku/ch-2.raku b/challenge-077/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..47d8524eaa
--- /dev/null
+++ b/challenge-077/ash/raku/ch-2.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+# Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
+
+my @matrix =
+ < O O X >,
+ < X O O >,
+ < X O O >; # square matrix
+
+# my @matrix =
+# < O O X O >,
+# < X O O O >,
+# < X O O X >,
+# < O X O O >;
+
+my @neighbours = ([X] (-1, 0, 1) xx 2).grep(*.all != 0);
+
+for ^@matrix X ^@matrix -> @coord {
+ next if @matrix[@coord[0]][@coord[1]] eq 'O';
+
+ @coord.put if all((@neighbours.map(* <<+>> @coord)).grep(0 <= *.all <= @matrix.end).map({
+ @matrix[$_[0]][$_[1]] eq 'O';
+ }));
+}
+
+# Output:
+# $ raku ch-2.raku
+# 0 2
+
+# Or:
+# $ raku ch-2.raku
+# 0 2
+# 2 3
diff --git a/challenge-077/ash/xslt/ch-2.xml b/challenge-077/ash/xslt/ch-2.xml
new file mode 100644
index 0000000000..ce27ac5cdb
--- /dev/null
+++ b/challenge-077/ash/xslt/ch-2.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0"?>
+
+<!--
+ Task 2 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+ To get a solution, run:
+ $ xsltproc ch-2.xslt ch-2.xml
+
+ For the given example matrix, you get the following output (items count from 1):
+ 1, 2
+ 3, 1
+-->
+
+<matrix>
+ <row>
+ <item>O</item> <item>X</item> <item>O</item>
+ </row>
+ <row>
+ <item>O</item> <item>O</item> <item>O</item>
+ </row>
+ <row>
+ <item>X</item> <item>O</item> <item>O</item>
+ </row>
+</matrix>
diff --git a/challenge-077/ash/xslt/ch-2.xslt b/challenge-077/ash/xslt/ch-2.xslt
new file mode 100644
index 0000000000..3a1fb688da
--- /dev/null
+++ b/challenge-077/ash/xslt/ch-2.xslt
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+<xsl:output omit-xml-declaration="yes"/>
+
+<xsl:template match="/matrix">
+ <xsl:apply-templates select="row"/>
+</xsl:template>
+
+<xsl:template match="row">
+ <xsl:apply-templates select="item"/>
+</xsl:template>
+
+<!-- catch 'O's -->
+<xsl:template match="item"/>
+
+<xsl:template match="item[text() = 'X']">
+ <xsl:variable name="row" select="count(../preceding-sibling::*) + 1"/> <!-- position of parent -->
+ <xsl:variable name="col" select="position()"/>
+
+ <xsl:variable name="lonely" select="
+ (not(/matrix/row[$row ]/item[$col - 1]) or /matrix/row[$row ]/item[$col - 1] = 'O') and
+ (not(/matrix/row[$row ]/item[$col + 1]) or /matrix/row[$row ]/item[$col + 1] = 'O') and
+ (not(/matrix/row[$row + 1]/item[$col ]) or /matrix/row[$row + 1]/item[$col ] = 'O') and
+ (not(/matrix/row[$row - 1]/item[$col ]) or /matrix/row[$row - 1]/item[$col ] = 'O') and
+ (not(/matrix/row[$row + 1]/item[$col + 1]) or /matrix/row[$row + 1]/item[$col + 1] = 'O') and
+ (not(/matrix/row[$row - 1]/item[$col - 1]) or /matrix/row[$row - 1]/item[$col - 1] = 'O') and
+ (not(/matrix/row[$row + 1]/item[$col - 1]) or /matrix/row[$row + 1]/item[$col - 1] = 'O') and
+ (not(/matrix/row[$row - 1]/item[$col + 1]) or /matrix/row[$row - 1]/item[$col + 1] = 'O')
+ "/>
+
+ <xsl:if test="$lonely">
+ <xsl:value-of select="$row"/>
+ <xsl:text>, </xsl:text>
+ <xsl:value-of select="$col"/>
+ <xsl:text disable-output-escaping="yes">&#10;</xsl:text>
+ </xsl:if>
+</xsl:template>
+
+</xsl:stylesheet>