diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-09 11:39:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-09 11:39:44 +0100 |
| commit | 682f1a50c12b4ec24b7e3bdd34a166bb289d7cda (patch) | |
| tree | d50410058d13b0577a7bc64107541020d467f7d7 | |
| parent | 47c4cea4080b4051d024685bedd5290e205cca74 (diff) | |
| parent | a5c411b10c1d16b7a52b795bbc68b133659294a8 (diff) | |
| download | perlweeklychallenge-club-682f1a50c12b4ec24b7e3bdd34a166bb289d7cda.tar.gz perlweeklychallenge-club-682f1a50c12b4ec24b7e3bdd34a166bb289d7cda.tar.bz2 perlweeklychallenge-club-682f1a50c12b4ec24b7e3bdd34a166bb289d7cda.zip | |
Merge pull request #10568 from garciaautomation/challenge-281-garciaautomation
Solutions for challenge 281 in perl
| -rw-r--r-- | challenge-281/garciaautomation/README | 1 | ||||
| -rwxr-xr-x | challenge-281/garciaautomation/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-281/garciaautomation/perl/ch-2.pl | 71 |
3 files changed, 96 insertions, 0 deletions
diff --git a/challenge-281/garciaautomation/README b/challenge-281/garciaautomation/README new file mode 100644 index 0000000000..51a796f300 --- /dev/null +++ b/challenge-281/garciaautomation/README @@ -0,0 +1 @@ +Solution by GarciaAutomation diff --git a/challenge-281/garciaautomation/perl/ch-1.pl b/challenge-281/garciaautomation/perl/ch-1.pl new file mode 100755 index 0000000000..915804b54f --- /dev/null +++ b/challenge-281/garciaautomation/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use v5.34; + +my $startCoord = $ARGV[0] ? $ARGV[0] : ''; +my $endCoord = $ARGV[1] ? $ARGV[1] : ''; +my $verbose; +my @start; +my $path; +my $moves; +my ( $x2, $y2 ); + +while ( ! $startCoord || $startCoord !~ m/^[a-hA-H][1-8]$/ ) { + say "Enter a start coordinate of the form: A1-H8" ; + $startCoord = <STDIN> ; + $startCoord = uc($startCoord); + chomp($startCoord); +} + +my $x = ord( substr( lc($startCoord), 0, 1 ) ) - 96; +my $y = substr( $startCoord, 1, 1 ); +print "$startCoord : "; +if ( ( $x % 2 ) == ( $y % 2 ) ) { say "Dark";say "false"; ;} +else { say "Light";say "true"; ;} + diff --git a/challenge-281/garciaautomation/perl/ch-2.pl b/challenge-281/garciaautomation/perl/ch-2.pl new file mode 100755 index 0000000000..3c4f6005b7 --- /dev/null +++ b/challenge-281/garciaautomation/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +use v5.34; + +my $startCoord = $ARGV[0] ? $ARGV[0] : ''; +my $endCoord = $ARGV[1] ? $ARGV[1] : ''; +my $verbose; +my @start; +my $path; +my $moves; +my ( $x2, $y2 ); +my $help = "USAGE: ./ch-2.pl a1 h8"; + +if ( ! $startCoord || $startCoord !~ m/^[a-hA-H][1-8]$/ ) { say "Coordinates must be between 'A1'-'H8'"; say $help;exit } +if ( ! $endCoord || $endCoord !~ m/^[a-hA-H][1-8]$/ ) { say "Coordinates must be between 'A1'-'H8'"; say $help;exit } + +my $x = ord( substr( lc($startCoord), 0, 1 ) ) - 96; +my $y = substr( $startCoord, 1, 1 ); +push( @start, [ $x, $y, chr( $x + 64 ) . $y ] ); + +print "Start: " . uc($startCoord) . " : "; +if ( ( $x % 2 ) == ( $y % 2 ) ) { say "Dark"; } +else { say "Light"; } + +if ( $endCoord ) { + $x2 = ord( substr( lc($endCoord), 0, 1 ) ) - 96; + $y2 = substr( $endCoord, 1, 1 ); + print " Dest: " . uc( $endCoord ) . " : "; + if ( ( $x2 % 2 ) == ( $y2 % 2 ) ) { say "Dark"; } + else { say "Light"; } + +} + +sub moveKnight() { + $moves++; + for my $l ( reverse(@start) ) { + say "at: " . chr( $l->[0] + 64 ) . $l->[1] . " path:$l->[2]" if $verbose; + push( @start, [ $l->[0] + 2, $l->[1] + 1, $l->[2] . "->" . chr( $l->[0] + 64 + 2 ) . ( $l->[1] + 1 ) ] ) if ( $l->[0] < 7 && $l->[1] < 8 ); + push( @start, [ $l->[0] + 2, $l->[1] - 1, $l->[2] . "->" . chr( $l->[0] + 64 + 2 ) . ( $l->[1] - 1 ) ] ) if ( $l->[0] < 7 && $l->[1] > 1 ); + push( @start, [ $l->[0] - 2, $l->[1] + 1, $l->[2] . "->" . chr( $l->[0] + 64 - 2 ) . ( $l->[1] + 1 ) ] ) if ( $l->[0] > 2 && $l->[1] < 8 ); + push( @start, [ $l->[0] - 2, $l->[1] - 1, $l->[2] . "->" . chr( $l->[0] + 64 - 2 ) . ( $l->[1] - 1 ) ] ) if ( $l->[0] > 2 && $l->[1] > 1 ); + push( @start, [ $l->[0] + 1, $l->[1] + 2, $l->[2] . "->" . chr( $l->[0] + 64 + 1 ) . ( $l->[1] + 2 ) ] ) if ( $l->[0] < 8 && $l->[1] < 7 ); + push( @start, [ $l->[0] + 1, $l->[1] - 2, $l->[2] . "->" . chr( $l->[0] + 64 + 1 ) . ( $l->[1] - 2 ) ] ) if ( $l->[0] < 8 && $l->[1] > 2 ); + push( @start, [ $l->[0] - 1, $l->[1] + 2, $l->[2] . "->" . chr( $l->[0] + 64 - 1 ) . ( $l->[1] + 2 ) ] ) if ( $l->[0] > 2 && $l->[1] < 7 ); + push( @start, [ $l->[0] - 1, $l->[1] - 2, $l->[2] . "->" . chr( $l->[0] + 64 - 1 ) . ( $l->[1] - 2 ) ] ) if ( $l->[0] > 2 && $l->[1] > 2 ); + } + + for ( 0 .. scalar(@start) - 1 ) { + if ( $start[$_][0] eq $x2 + && $start[$_][1] eq $y2 ) + { + $path++; + print "Path $path: " ; + local $| = 1; + for my $char (split /(->)/, $start[$_][2]) { + use Time::HiRes;Time::HiRes::sleep(0.06); + print "$char"; + } + say "" + } + } + + if ($path) { + say "In $moves moves"; + say $moves; + exit + } ; + + __SUB__->(); +} + +moveKnight() if ($endCoord);
\ No newline at end of file |
