diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-06 11:48:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-06 11:48:39 +0100 |
| commit | 524e83f8bb3eafc206db0fa73023aea2359ed2b2 (patch) | |
| tree | 1aaa1e7a0d62763bbf5b1210677020df4cade6c6 | |
| parent | c0cdfa81baba26f31aef37e8414bdd6e37f46623 (diff) | |
| parent | dec8a21b242df0149e9f76798544e44b17e78621 (diff) | |
| download | perlweeklychallenge-club-524e83f8bb3eafc206db0fa73023aea2359ed2b2.tar.gz perlweeklychallenge-club-524e83f8bb3eafc206db0fa73023aea2359ed2b2.tar.bz2 perlweeklychallenge-club-524e83f8bb3eafc206db0fa73023aea2359ed2b2.zip | |
Merge pull request #10558 from Firedrake/rogerbw-challenge-281
RogerBW solutions for challenge no. 281
23 files changed, 1050 insertions, 0 deletions
diff --git a/challenge-281/roger-bell-west/crystal/ch-1.cr b/challenge-281/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..e8f0745b2a --- /dev/null +++ b/challenge-281/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,25 @@ +#! /usr/bin/crystal +require "spec" +describe "checkcolor" do + it "test_ex1" do + checkcolor("d3").should eq true + end + it "test_ex2" do + checkcolor("g5").should eq false + end + it "test_ex3" do + checkcolor("e6").should eq true + end +end + +def cs2xy(a) + c = a.chars + x = c[0].ord - 'a'.ord + y = c[1].ord - '1'.ord + [x, y] +end + +def checkcolor(a) + xy = cs2xy(a) + (xy[0] + xy[1]) % 2 == 1 +end diff --git a/challenge-281/roger-bell-west/crystal/ch-2.cr b/challenge-281/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..077af477a4 --- /dev/null +++ b/challenge-281/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,52 @@ +#! /usr/bin/crystal +require "spec" +describe "knightsmove" do + it "test_ex1" do + knightsmove("g2", "a8").should eq 4 + end + it "test_ex2" do + knightsmove("g2", "h2").should eq 3 + end +end + +def cs2xy(a) + c = a.chars + x = c[0].ord - 'a'.ord + y = c[1].ord - '1'.ord + [x, y] +end + +def knightsmove(from, to) + fc = cs2xy(from) + tc = cs2xy(to) + queue = Deque(Array(Int32)).new + queue.push([fc[0], fc[1], 0]) + seen = Set(Array(Int32)).new + while queue.size > 0 + cc = queue.shift + if cc[0] == tc[0] && cc[1] == tc[1] + return cc[2] + else + [ + [2, 1], + [1, 2], + [2, -1], + [1, -2], + [-2, 1], + [-1, 2], + [-2, -1], + [-1, -2], + ].each do |offset| + x = cc[0] + offset[0] + y = cc[1] + offset[1] + if x >= 0 && x <= 7 && y >= 0 && y <= 7 + if !seen.includes?([x, y]) + queue.push([x, y, cc[2] + 1]) + seen.add([x, y]) + end + end + end + end + end + -1 +end diff --git a/challenge-281/roger-bell-west/javascript/ch-1.js b/challenge-281/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..d07544d416 --- /dev/null +++ b/challenge-281/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,33 @@ +#! /usr/bin/node + +"use strict" + +function cs2xy(a) { + const x = a.charCodeAt(0) - 'a'.charCodeAt(0); + const y = a.charCodeAt(1) - '1'.charCodeAt(0); + return [x, y]; +} + +function checkcolor(a) { + const xy = cs2xy(a); + return (xy[0] + xy[1]) % 2 == 1; +} + +if (checkcolor('d3')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!checkcolor('g5')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (checkcolor('e6')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-281/roger-bell-west/javascript/ch-2.js b/challenge-281/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..d14b70c0b6 --- /dev/null +++ b/challenge-281/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,58 @@ +#! /usr/bin/node + +"use strict" + +function cs2xy(a) { + const x = a.charCodeAt(0) - 'a'.charCodeAt(0); + const y = a.charCodeAt(1) - '1'.charCodeAt(0); + return [x, y]; +} + +function knightsmove(from, to) { + const fc = cs2xy(from); + const tc = cs2xy(to); + let queue = []; + queue.push([fc[0], fc[1], 0]); + let seen = new Set; + while (queue.length > 0) { + const cc = queue.shift(); + if (cc[0] == tc[0] && cc[1] == tc[1]) { + return cc[2]; + } else { + for (let offset of [ + [2, 1], + [1, 2], + [2, -1], + [1, -2], + [-2, 1], + [-1, 2], + [-2, -1], + [-1, -2] + ]) { + const x = cc[0] + offset[0]; + const y = cc[1] + offset[1]; + if (x >= 0 && x <= 7 && y >= 0 && y <= 7) { + const cv = x * 8 + y; + if (!seen.has(cv)) { + queue.push([x, y, cc[2] + 1]); + seen.add(cv); + } + } + } + } + } + return -1; +} + +if (knightsmove('g2', 'a8') == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (knightsmove('g2', 'h2') == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-281/roger-bell-west/kotlin/ch-1.kt b/challenge-281/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..2f71eaf7be --- /dev/null +++ b/challenge-281/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,34 @@ +fun cs2xy(a: String): Pair<Int, Int> { + val c = a.toList() + val x = c[0].code - 'a'.code + val y = c[1].code - '1'.code + return Pair(x, y) +} + +fun checkcolor(a: String): Boolean { + val xy = cs2xy(a) + return (xy.first + xy.second) % 2 == 1 +} + +fun main() { + + if (checkcolor("d3")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!checkcolor("g5")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (checkcolor("e6")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-281/roger-bell-west/kotlin/ch-2.kt b/challenge-281/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..203a2dd4e5 --- /dev/null +++ b/challenge-281/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,59 @@ +fun cs2xy(a: String): Pair<Int, Int> { + val c = a.toList() + val x = c[0].code - 'a'.code + val y = c[1].code - '1'.code + return Pair(x, y) +} + +fun knightsmove(from: String, to: String): Int { + val fc = cs2xy(from) + val tc = cs2xy(to) + var queue = ArrayDeque<List<Int>>() + queue.add(listOf(fc.first, fc.second, 0)) + var seen = mutableSetOf<Int>() + while (queue.size > 0) { + val cc = queue.removeFirst() + if (cc[0] == tc.first && cc[1] == tc.second) { + return cc[2] + } else { + for (offset in listOf ( + listOf(2, 1), + listOf(1, 2), + listOf(2, -1), + listOf(1, -2), + listOf(-2, 1), + listOf(-1, 2), + listOf(-2, -1), + listOf(-1, -2) + )) { + val x = cc[0] + offset[0] + val y = cc[1] + offset[1] + if (x >= 0 && x <= 7 && y >= 0 && y <= 7) { + val cv = x * 8 + y + if (!seen.contains(cv)) { + queue.add(listOf(x, y, cc[2] + 1)) + seen.add(cv) + } + } + } + } + } + return -1 +} + +fun main() { + + if (knightsmove("g2", "a8") == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (knightsmove("g2", "h2") == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-281/roger-bell-west/lua/ch-1.lua b/challenge-281/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..afb6fa324b --- /dev/null +++ b/challenge-281/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,34 @@ +#! /usr/bin/lua + +function cs2xy(a) + local x = string.byte(a, 1) - string.byte("a") + local y = string.byte(a, 2) - string.byte("1") + return {x, y} +end + +function checkcolor(a) + local xy = cs2xy(a) + return (xy[1] + xy[2]) % 2 == 1 +end + +if checkcolor("d3") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not checkcolor("g5") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if checkcolor("e6") then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-281/roger-bell-west/lua/ch-2.lua b/challenge-281/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..3979fb2830 --- /dev/null +++ b/challenge-281/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,58 @@ +#! /usr/bin/lua + +function cs2xy(a) + local x = string.byte(a, 1) - string.byte("a") + local y = string.byte(a, 2) - string.byte("1") + return {x, y} +end + +function knightsmove(from, to) + local fc = cs2xy(from) + local tc = cs2xy(to) + local queue = {} + table.insert(queue, {fc[1], fc[2], 0}) + local seen = {} + while #queue > 0 do + local cc = table.remove(queue, 1) + if (cc[1] == tc[1] and cc[2] == tc[2]) then + return cc[3] + else + for _, offset in ipairs({ + {2, 1}, + {1, 2}, + {2, -1}, + {1, -2}, + {-2, 1}, + {-1, 2}, + {-2, -1}, + {-1, -2} + }) do + local x = cc[1] + offset[1] + local y = cc[2] + offset[2] + if (x >= 0 and x <= 7 and y >= 0 and y <= 7) then + local cv = x * 8 + y + if seen[cv] == nil then + table.insert(queue, {x, y, cc[3] + 1}) + seen[cv] = true + end + end + end + end + end + return -1 +end + +if knightsmove("g2", "a8") == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if knightsmove("g2", "h2") == 3 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-281/roger-bell-west/perl/ch-1.pl b/challenge-281/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..f9acac4da2 --- /dev/null +++ b/challenge-281/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(checkcolor('d3'), 1, 'example 1'); +is(checkcolor('g5'), 0, 'example 2'); +is(checkcolor('e6'), 1, 'example 3'); + +sub cs2xy($a) { + my @c = split('', $a); + my $x = ord($c[0]) - ord('a'); + my $y = ord($c[1]) - ord('1'); + return [$x, $y]; +} + +sub checkcolor($a) { + my $xy = cs2xy($a); + return (($xy->[0] + $xy->[1]) % 2 == 1)?1:0; +} diff --git a/challenge-281/roger-bell-west/perl/ch-2.pl b/challenge-281/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..01b2fb0cbc --- /dev/null +++ b/challenge-281/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,53 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(knightsmove('g2', 'a8'), 4, 'example 1'); +is(knightsmove('g2', 'h2'), 3, 'example 2'); + +sub cs2xy($a) { + my @c = split('', $a); + my $x = ord($c[0]) - ord('a'); + my $y = ord($c[1]) - ord('1'); + return [$x, $y]; +} + +sub knightsmove($from, $to) { + my $fc = cs2xy($from); + my $tc = cs2xy($to); + my @queue; + push @queue, [$fc->[0], $fc->[1], 0]; + my %seen; + while (@queue) { + my $cc = shift @queue; + if ($cc->[0] == $tc->[0] && $cc->[1] == $tc->[1]) { + return $cc->[2]; + } else { + foreach my $offset ( + [2, 1], + [1, 2], + [2, -1], + [1, -2], + [-2, 1], + [-1, 2], + [-2, -1], + [-1, -2] + ) { + my $x = $cc->[0] + $offset->[0]; + my $y = $cc->[1] + $offset->[1]; + if ($x >= 0 && $x <= 7 && $y >= 0 && $y <= 7) { + my $cv = $x * 8 + $y; + if (!exists $seen{$cv}) { + push @queue,[$x, $y, $cc->[2] + 1]; + $seen{$cv} = 1; + } + } + } + } + } + return -1; +} diff --git a/challenge-281/roger-bell-west/postscript/ch-1.ps b/challenge-281/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..f1b160ebc7 --- /dev/null +++ b/challenge-281/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,62 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/s2a { + [ exch { } forall ] +} bind def + + +% end included library code + +/cs2xy { + [ exch + s2a aload pop + (1) s2a aload pop sub + exch + (a) s2a aload pop sub + exch + ] +} bind def + +/checkcolor { + cs2xy aload pop + add 2 mod 1 eq +} bind def + +(checkcolor) test.start +(d3) checkcolor test +(g5) checkcolor not test +(e6) checkcolor test +test.end diff --git a/challenge-281/roger-bell-west/postscript/ch-2.ps b/challenge-281/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..6f78ee73d3 --- /dev/null +++ b/challenge-281/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,109 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/s2a { + [ exch { } forall ] +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/apop.left { % [a b c] -> [b c] a + dup 0 get exch + [ exch aload length -1 roll pop ] exch +} bind def + + +% end included library code + +/cs2xy { + [ exch + s2a aload pop + (1) s2a aload pop sub + exch + (a) s2a aload pop sub + exch + ] +} bind def + +/knightsmove { + 0 dict begin + cs2xy /tc exch def + cs2xy /fc exch def + /queue [ [ fc 0 get fc 1 get 0 ] ] def + /seen 0 dict def + -1 + { + queue length 1 ge { + queue apop.left /cc exch def /queue exch def + cc 0 get tc 0 get eq cc 1 get tc 1 get eq and { + pop cc 2 get + exit + } { + [ + [2 1] + [1 2] + [2 -1] + [1 -2] + [-2 1] + [-1 2] + [-2 -1] + [-1 -2] + ] { + /offset exch def + /x cc 0 get offset 0 get add def + /y cc 1 get offset 1 get add def + x 0 ge x 7 le and y 0 ge and y 7 le and { + /cv x 8 mul y add def + seen cv known not { + /queue queue [ x y cc 2 get 1 add ] apush.right def + seen cv true put + } if + } if + } forall + } ifelse + } { + exit + } ifelse + } loop + end +} bind def + +(knightsmove) test.start +(g2) (a8) knightsmove 4 eq test +(g2) (h2) knightsmove 3 eq test +test.end diff --git a/challenge-281/roger-bell-west/python/ch-1.py b/challenge-281/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..fb683db3ea --- /dev/null +++ b/challenge-281/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def cs2xy(a): + x = ord(a[0]) - ord('a') + y = ord(a[1]) - ord('1') + return [x, y] + +def checkcolor(a): + xy = cs2xy(a) + return (xy[0] + xy[1]) % 2 == 1 + +import unittest + +class TestCheckcolor(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(checkcolor("d3"), True, 'example 1') + + def test_ex2(self): + self.assertEqual(checkcolor("g5"), False, 'example 2') + + def test_ex3(self): + self.assertEqual(checkcolor("e6"), True, 'example 3') + +unittest.main() diff --git a/challenge-281/roger-bell-west/python/ch-2.py b/challenge-281/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..e6bbe85da8 --- /dev/null +++ b/challenge-281/roger-bell-west/python/ch-2.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 + +from collections import deque + +def cs2xy(a): + x = ord(a[0]) - ord('a') + y = ord(a[1]) - ord('1') + return [x, y] + +def knightsmove(ffrom, to): + fc = cs2xy(ffrom) + tc = cs2xy(to) + queue = deque() + queue.append((fc[0], fc[1], 0)) + seen = set() + while len(queue) > 0: + cc = queue.popleft() + if cc[0] == tc[0] and cc[1] == tc[1]: + return cc[2] + else: + for offset in ( (2, 1), (1, 2), (2, -1), (1, -2), (-2, 1), (-1, 2), (-2, -1), (-1, -2) ): + x = cc[0] + offset[0] + y = cc[1] + offset[1] + if x >= 0 and x <= 7 and y >= 0 and y <= 7: + cv = x * 8 + y + if cv not in seen: + queue.append((x, y, cc[2] + 1)) + seen.add(cv) + return -1 + +import unittest + +class TestKnightsmove(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(knightsmove("g2", "a8"), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(knightsmove("g2", "h2"), 3, 'example 2') + +unittest.main() diff --git a/challenge-281/roger-bell-west/raku/ch-1.p6 b/challenge-281/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..bca1ffd049 --- /dev/null +++ b/challenge-281/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(checkcolor('d3'), True, 'example 1'); +is(checkcolor('g5'), False, 'example 2'); +is(checkcolor('e6'), True, 'example 3'); + +sub cs2xy($a) { + my @c = $a.comb; + my $x = ord(@c[0]) - ord('a'); + my $y = ord(@c[1]) - ord('1'); + return [$x, $y]; +} + +sub checkcolor($a) { + my @xy = cs2xy($a); + return (@xy[0] + @xy[1]) % 2 == 1; +} diff --git a/challenge-281/roger-bell-west/raku/ch-2.p6 b/challenge-281/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..6cf2e65ae6 --- /dev/null +++ b/challenge-281/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,51 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(knightsmove('g2', 'a8'), 4, 'example 1'); +is(knightsmove('g2', 'h2'), 3, 'example 2'); + +sub cs2xy($a) { + my @c = $a.comb; + my $x = ord(@c[0]) - ord('a'); + my $y = ord(@c[1]) - ord('1'); + return [$x, $y]; +} + +sub knightsmove($from, $to) { + my @fc = cs2xy($from); + my @tc = cs2xy($to); + my @queue; + @queue.push([@fc[0], @fc[1], 0]); + my %seen = SetHash.new; + while (@queue.elems > 0) { + my @cc = @queue.shift.flat; + if (@cc[0] == @tc[0] && @cc[1] == @tc[1]) { + return @cc[2]; + } else { + for ( + [2, 1], + [1, 2], + [2, -1], + [1, -2], + [-2, 1], + [-1, 2], + [-2, -1], + [-1, -2] + ) -> @offset { + my $x = @cc[0] + @offset[0]; + my $y = @cc[1] + @offset[1]; + if ($x >= 0 && $x <= 7 && $y >= 0 && $y <= 7) { + my $cv = $x * 8 + $y; + if (%seen{$cv}:!exists) { + @queue.push([$x, $y, @cc[2] + 1]); + %seen{$cv} = True; + } + } + } + } + } + return -1; +} diff --git a/challenge-281/roger-bell-west/ruby/ch-1.rb b/challenge-281/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..745f286ba4 --- /dev/null +++ b/challenge-281/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def cs2xy(a) + c = a.chars + x = c[0].ord - 'a'.ord + y = c[1].ord - '1'.ord + [x, y] +end + +def checkcolor(a) + xy = cs2xy(a) + (xy[0] + xy[1]) % 2 == 1 +end + +require 'test/unit' + +class TestCheckcolor < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, checkcolor('d3')) + end + + def test_ex2 + assert_equal(false, checkcolor('g5')) + end + + def test_ex3 + assert_equal(true, checkcolor('e6')) + end + +end diff --git a/challenge-281/roger-bell-west/ruby/ch-2.rb b/challenge-281/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..fa48d3d65a --- /dev/null +++ b/challenge-281/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,59 @@ +#! /usr/bin/ruby + +require 'set' + +def cs2xy(a) + c = a.chars + x = c[0].ord - 'a'.ord + y = c[1].ord - '1'.ord + [x, y] +end + +def knightsmove(from, to) + fc = cs2xy(from) + tc = cs2xy(to) + queue = Array.new + queue.push([fc[0], fc[1], 0]) + seen = Set.new + while queue.size > 0 + cc = queue.shift + if cc[0] == tc[0] && cc[1] == tc[1] + return cc[2] + else + [ + [2, 1], + [1, 2], + [2, -1], + [1, -2], + [-2, 1], + [-1, 2], + [-2, -1], + [-1, -2], + ].each do |offset| + x = cc[0] + offset[0] + y = cc[1] + offset[1] + if x >= 0 && x <= 7 && y >= 0 && y <= 7 + if !seen.include?([x, y]) + queue.push([x, y, cc[2] + 1]) + seen.add([x, y]) + end + end + end + end + end + -1 +end + +require 'test/unit' + +class TestKnightsmove < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, knightsmove('g2', 'a8')) + end + + def test_ex2 + assert_equal(3, knightsmove('g2', 'h2')) + end + +end diff --git a/challenge-281/roger-bell-west/rust/ch-1.rs b/challenge-281/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..17e7af026c --- /dev/null +++ b/challenge-281/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(checkcolor("d3"), true); +} + +#[test] +fn test_ex2() { + assert_eq!(checkcolor("g5"), false); +} + +#[test] +fn test_ex3() { + assert_eq!(checkcolor("e6"), true); +} + +fn cs2xy(a: &str) -> (u8, u8) { + let c = a.chars().collect::<Vec<char>>(); + let x = c[0] as u8 - 'a' as u8; + let y = c[1] as u8 - '1' as u8; + (x, y) +} + +fn checkcolor(a: &str) -> bool { + let xy = cs2xy(a); + (xy.0 + xy.1) % 2 == 1 +} diff --git a/challenge-281/roger-bell-west/rust/ch-2.rs b/challenge-281/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..14f213dc54 --- /dev/null +++ b/challenge-281/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,55 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::{HashSet, VecDeque}; + +#[test] +fn test_ex1() { + assert_eq!(knightsmove("g2", "a8"), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(knightsmove("g2", "h2"), 3); +} + +fn cs2xy(a: &str) -> (i8, i8) { + let c = a.chars().collect::<Vec<char>>(); + let x = c[0] as i8 - 'a' as i8; + let y = c[1] as i8 - '1' as i8; + (x, y) +} + +fn knightsmove(from: &str, to: &str) -> isize { + let fc = cs2xy(from); + let tc = cs2xy(to); + let mut queue: VecDeque<(i8, i8, isize)> = VecDeque::new(); |
