aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-11 10:47:33 +0100
committerGitHub <noreply@github.com>2024-08-11 10:47:33 +0100
commit70fff8294eb39896d29ce5e4543af547e5de724c (patch)
tree49c64e98cb2cd2fa1017bf63d9fab91c1290f776
parent763e9d078439adfe10b90a2731c34c761eb8a4e8 (diff)
parent3f28cf46d7602c7dbb9f5236607e8c90967617d4 (diff)
downloadperlweeklychallenge-club-70fff8294eb39896d29ce5e4543af547e5de724c.tar.gz
perlweeklychallenge-club-70fff8294eb39896d29ce5e4543af547e5de724c.tar.bz2
perlweeklychallenge-club-70fff8294eb39896d29ce5e4543af547e5de724c.zip
Merge pull request #10577 from deadmarshal/TWC281
TWC281
-rw-r--r--challenge-281/deadmarshal/blog.txt1
-rw-r--r--challenge-281/deadmarshal/java/Ch1.java13
-rw-r--r--challenge-281/deadmarshal/java/Ch2.java44
-rw-r--r--challenge-281/deadmarshal/modula-3/ch1/src/Ch1.m317
-rw-r--r--challenge-281/deadmarshal/modula-3/ch1/src/m3makefile5
-rw-r--r--challenge-281/deadmarshal/modula-3/ch2/src/Cell.i312
-rw-r--r--challenge-281/deadmarshal/modula-3/ch2/src/Ch2.m357
-rw-r--r--challenge-281/deadmarshal/modula-3/ch2/src/m3makefile7
-rw-r--r--challenge-281/deadmarshal/perl/ch-1.pl12
-rw-r--r--challenge-281/deadmarshal/perl/ch-2.pl42
10 files changed, 210 insertions, 0 deletions
diff --git a/challenge-281/deadmarshal/blog.txt b/challenge-281/deadmarshal/blog.txt
new file mode 100644
index 0000000000..f81b9a1f81
--- /dev/null
+++ b/challenge-281/deadmarshal/blog.txt
@@ -0,0 +1 @@
+https://deadmarshal.blogspot.com/2024/08/twc281.html
diff --git a/challenge-281/deadmarshal/java/Ch1.java b/challenge-281/deadmarshal/java/Ch1.java
new file mode 100644
index 0000000000..775eabbd2d
--- /dev/null
+++ b/challenge-281/deadmarshal/java/Ch1.java
@@ -0,0 +1,13 @@
+public class Ch1 {
+ public static void main(String[] args) {
+ System.out.println(check_color("d3"));
+ System.out.println(check_color("g5"));
+ System.out.println(check_color("e6"));
+ }
+
+ private static boolean check_color(String str) {
+ assert str.length() == 2;
+ return (str.charAt(0) + str.charAt(1)) % 2 == 1;
+ }
+}
+
diff --git a/challenge-281/deadmarshal/java/Ch2.java b/challenge-281/deadmarshal/java/Ch2.java
new file mode 100644
index 0000000000..4a2f96db37
--- /dev/null
+++ b/challenge-281/deadmarshal/java/Ch2.java
@@ -0,0 +1,44 @@
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class Ch2 {
+ public static void main(String[] args) {
+ System.out.println(knights_move("g2", "a8"));
+ System.out.println(knights_move("g2", "h2"));
+ }
+
+ private static int knights_move(String start, String end) {
+ assert start.length() == 2 && end.length() == 2;
+ int[] s = new int[]{start.charAt(0) - 'a', start.charAt(1) - '0'};
+ int[] e = new int[]{end.charAt(0) - 'a', end.charAt(1) - '0'};
+ return min_steps(s, e, 8);
+ }
+
+ record Cell(int x, int y, int distance) {
+ }
+
+ private static boolean is_inside(int x, int y, int n) {
+ return x >= 0 && x <= n && y >= 0 && y <= n;
+ }
+
+ private static int min_steps(int[] knight_pos, int[] target_pos, int n) {
+ int[][] dirs =
+ {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
+ Queue<Cell> q = new LinkedList<>();
+ q.add(new Cell(knight_pos[0], knight_pos[1], 0));
+ boolean[][] visited = new boolean[n + 1][n + 1];
+ while (!q.isEmpty()) {
+ Cell t = q.poll();
+ if (t.x == target_pos[0] && t.y == target_pos[1]) return t.distance;
+ for (int i = 0; i < n; ++i) {
+ int x = t.x + dirs[i][0];
+ int y = t.y + dirs[i][1];
+ if (is_inside(x, y, n) && !visited[x][y]) {
+ visited[x][y] = true;
+ q.add(new Cell(x, y, t.distance + 1));
+ }
+ }
+ }
+ return Integer.MAX_VALUE;
+ }
+}
diff --git a/challenge-281/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-281/deadmarshal/modula-3/ch1/src/Ch1.m3
new file mode 100644
index 0000000000..45fe2ed220
--- /dev/null
+++ b/challenge-281/deadmarshal/modula-3/ch1/src/Ch1.m3
@@ -0,0 +1,17 @@
+MODULE Ch1 EXPORTS Main;
+
+IMPORT SIO,Text;
+
+PROCEDURE CheckColor(READONLY Str:TEXT):BOOLEAN =
+ BEGIN
+ <* ASSERT Text.Length(Str) = 2 *>
+ RETURN (ORD(Text.GetChar(Str,0)) +
+ ORD(Text.GetChar(Str,1))) MOD 2 = 1
+ END CheckColor;
+
+BEGIN
+ SIO.PutBool(CheckColor("d3")); SIO.Nl();
+ SIO.PutBool(CheckColor("g5")); SIO.Nl();
+ SIO.PutBool(CheckColor("e6")); SIO.Nl()
+END Ch1.
+
diff --git a/challenge-281/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-281/deadmarshal/modula-3/ch1/src/m3makefile
new file mode 100644
index 0000000000..9f66e4a51f
--- /dev/null
+++ b/challenge-281/deadmarshal/modula-3/ch1/src/m3makefile
@@ -0,0 +1,5 @@
+import("libm3")
+import("libsio")
+implementation("Ch1")
+program("ch1")
+
diff --git a/challenge-281/deadmarshal/modula-3/ch2/src/Cell.i3 b/challenge-281/deadmarshal/modula-3/ch2/src/Cell.i3
new file mode 100644
index 0000000000..c497ecb45f
--- /dev/null
+++ b/challenge-281/deadmarshal/modula-3/ch2/src/Cell.i3
@@ -0,0 +1,12 @@
+INTERFACE Cell;
+
+CONST
+ Brand = "Cell";
+
+TYPE
+ T = RECORD
+ X,Y,D:INTEGER := 0;
+ END;
+
+END Cell.
+
diff --git a/challenge-281/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-281/deadmarshal/modula-3/ch2/src/Ch2.m3
new file mode 100644
index 0000000000..a011c979ee
--- /dev/null
+++ b/challenge-281/deadmarshal/modula-3/ch2/src/Ch2.m3
@@ -0,0 +1,57 @@
+MODULE Ch2 EXPORTS Main;
+
+IMPORT SIO,Text,ASCII,Cell,CellSeq;
+
+PROCEDURE IsInside(READONLY X,Y,N:INTEGER):BOOLEAN =
+ BEGIN
+ RETURN X >= 0 AND X <= N AND Y >= 0 AND Y <= N
+ END IsInside;
+
+PROCEDURE MinSteps(READONLY K1,K2,T1,T2,N:INTEGER):CARDINAL =
+ VAR
+ Dirs := ARRAY[0..7] OF Cell.T{
+ Cell.T{-2,1},Cell.T{-1,2},
+ Cell.T{1,2},Cell.T{2,1},
+ Cell.T{2,-1},Cell.T{1,-2},
+ Cell.T{-1,-2},Cell.T{-2,-1}};
+ Q:CellSeq.T := NEW(CellSeq.T).init(NUMBER(Dirs)+1);
+ Visited: REF ARRAY OF ARRAY OF BOOLEAN :=
+ NEW(REF ARRAY OF ARRAY OF BOOLEAN,N+1,N+1);
+ Temp:Cell.T;
+ X,Y:INTEGER;
+ BEGIN
+ Q.addhi(Cell.T{K1,K2,0});
+ WHILE Q.size() # 0 DO
+ Temp := Q.remlo();
+ IF Temp.X = T1 AND Temp.Y = T2 THEN RETURN Temp.D END;
+ FOR I := FIRST(Dirs) TO LAST(Dirs) DO
+ X := Temp.X + Dirs[I].X;
+ Y := Temp.Y + Dirs[I].Y;
+ IF IsInside(X,Y,N) AND NOT Visited[X,Y] THEN
+ Visited[X][Y] := TRUE;
+ Q.addhi(Cell.T{X,Y,Temp.D+1})
+ END
+ END
+ END;
+ RETURN LAST(INTEGER);
+ END MinSteps;
+
+PROCEDURE KnightsMoves(READONLY Start,End:TEXT):CARDINAL =
+ BEGIN
+ <* ASSERT Text.Length(Start) = 2 AND Text.Length(End) = 2
+ AND Text.GetChar(Start,0) IN ASCII.Letters AND
+ Text.GetChar(Start,1) IN ASCII.Digits AND
+ Text.GetChar(End,0) IN ASCII.Letters AND
+ Text.GetChar(End,1) IN ASCII.Digits *>
+ RETURN MinSteps(ORD(Text.GetChar(Start,0)) - ORD('a'),
+ ORD(Text.GetChar(Start,1)) - ORD('0'),
+ ORD(Text.GetChar(End,0)) - ORD('a'),
+ ORD(Text.GetChar(End,1)) - ORD('0'),
+ 8)
+ END KnightsMoves;
+
+BEGIN
+ SIO.PutInt(KnightsMoves("g2","a8")); SIO.Nl();
+ SIO.PutInt(KnightsMoves("g2","h2")); SIO.Nl()
+END Ch2.
+
diff --git a/challenge-281/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-281/deadmarshal/modula-3/ch2/src/m3makefile
new file mode 100644
index 0000000000..8b2702e54d
--- /dev/null
+++ b/challenge-281/deadmarshal/modula-3/ch2/src/m3makefile
@@ -0,0 +1,7 @@
+import("libm3")
+import("libsio")
+interface("Cell")
+sequence("Cell","Cell")
+implementation("Ch2")
+program("ch2")
+
diff --git a/challenge-281/deadmarshal/perl/ch-1.pl b/challenge-281/deadmarshal/perl/ch-1.pl
new file mode 100644
index 0000000000..1e52edbcb4
--- /dev/null
+++ b/challenge-281/deadmarshal/perl/ch-1.pl
@@ -0,0 +1,12 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+sub check_color{
+ (ord(substr $_[0],0,1) + substr $_[0],1,1) % 2
+}
+
+printf "%d\n",check_color('d3');
+printf "%d\n",check_color('g5');
+printf "%d\n",check_color('e6');
+
diff --git a/challenge-281/deadmarshal/perl/ch-2.pl b/challenge-281/deadmarshal/perl/ch-2.pl
new file mode 100644
index 0000000000..a25f06d9fc
--- /dev/null
+++ b/challenge-281/deadmarshal/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+sub is_inside{
+ $_[0] >= 0 && $_[0] <= $_[2]
+ && $_[1] >= 0 && $_[1] <= $_[2]
+}
+
+sub min_steps{
+ my ($k1,$k2,$t1,$t2,$n) = @_;
+ my @dirs = ([-2,1],[-1,2],[1,2],[2,1],
+ [2,-1],[1,-2],[-1,-2],[-2,-1]);
+ my (@queue,@visited);
+ push @queue,[$k1,$k2,0];
+ while(@queue){
+ my $t = shift @queue;
+ return $t->[2] if $t->[0] == $t1 && $t->[1] == $t2;
+ foreach my $i(0..$#dirs){
+ my $x = $t->[0] + $dirs[$i][0];
+ my $y = $t->[1] + $dirs[$i][1];
+ if(is_inside($x,$y,$n)){
+ $visited[$x][$y] = 1;
+ push @queue,[$x,$y,$t->[2]+1]
+ }
+ }
+ }
+ -1
+}
+
+sub knights_move{
+ my ($s,$e) = @_;
+ my ($k1,$k2,$t1,$t2) = (ord(substr($s,0,1)) - ord('a'),
+ substr($s,1,1) - '0',
+ ord(substr($e,0,1)) - ord('a'),
+ substr($e,1,1) - '0');
+ min_steps($k1,$k2,$t1,$t2,8)
+}
+
+printf "%d\n",knights_move('g2','a8');
+printf "%d\n",knights_move('g2','h2');
+