aboutsummaryrefslogtreecommitdiff
path: root/challenge-077/ash/xslt
diff options
context:
space:
mode:
authorAndrew Shitov <andy@shitov.ru>2020-09-09 21:22:11 +0200
committerAndrew Shitov <andy@shitov.ru>2020-09-09 21:22:11 +0200
commitdfef53750030612523a4edc1d63ce4f179745f7d (patch)
tree2591d1796a625ca6ecb68e3ddcb220ac0ab05ac6 /challenge-077/ash/xslt
parentdaec1a0fc3d537f1510f05a9799ac78691af0428 (diff)
downloadperlweeklychallenge-club-dfef53750030612523a4edc1d63ce4f179745f7d.tar.gz
perlweeklychallenge-club-dfef53750030612523a4edc1d63ce4f179745f7d.tar.bz2
perlweeklychallenge-club-dfef53750030612523a4edc1d63ce4f179745f7d.zip
77-2: a functional solution in XSLT
Diffstat (limited to 'challenge-077/ash/xslt')
-rw-r--r--challenge-077/ash/xslt/ch-2.xml25
-rw-r--r--challenge-077/ash/xslt/ch-2.xslt41
2 files changed, 66 insertions, 0 deletions
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>