From fbfe92ee23c1aff2724ed81a681d35e7698aea51 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 5 Aug 2024 22:27:48 +0200 Subject: Add solution 281 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-281/jeanluc2020/blog-1.txt | 1 + challenge-281/jeanluc2020/blog-2.txt | 1 + challenge-281/jeanluc2020/perl/ch-1.pl | 68 ++++++++++++++++++++++ challenge-281/jeanluc2020/perl/ch-2.pl | 103 +++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 challenge-281/jeanluc2020/blog-1.txt create mode 100644 challenge-281/jeanluc2020/blog-2.txt create mode 100755 challenge-281/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-281/jeanluc2020/perl/ch-2.pl diff --git a/challenge-281/jeanluc2020/blog-1.txt b/challenge-281/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..1d71d498ef --- /dev/null +++ b/challenge-281/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-281-1.html diff --git a/challenge-281/jeanluc2020/blog-2.txt b/challenge-281/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..8b72113c8a --- /dev/null +++ b/challenge-281/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-281-2.html diff --git a/challenge-281/jeanluc2020/perl/ch-1.pl b/challenge-281/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..19cee92c1f --- /dev/null +++ b/challenge-281/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-281/#TASK1 +# +# You are given coordinates, a string that represents the coordinates of a +# square of the chessboard as shown below: +# +# 8 # # # # +# 7# # # # +# 6 # # # # +# 5# # # # +# 4 # # # # +# 3# # # # +# 2 # # # # +# 1# # # # +# abcdefgh +# +# Write a script to return true if the square is light, and false if the square +# is dark. +# +## Example 1 +## +## Input: $coordinates = "d3" +## Output: true +# +## Example 2 +## +## Input: $coordinates = "g5" +## Output: false +# +## Example 3 +## +## Input: $coordinates = "e6" +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# Let's map a through h to 1 through 8. That way, each square has coordinates +# char/digit which map to digit/digit. We can observe that the sum of those +# digits is even when the square is dark (this works because at the end of a +# line, for example h1, we can jump to the beginning of the next line, and the +# mapping jumps from 8:1 to 1:2 which changes evenness in both digits, keeping +# it overall, and indeed we keep the light/dark by jumping there as well). +# So if the sum of the mapped digits is even, we have a dark square. That's easy +# to calculate. + +use strict; +use warnings; + +check_color("d3"); +check_color("g5"); +check_color("e6"); + +sub check_color { + my $coordinates = shift; + my $map = { "a" => 1, "b" => 2, "c" => 3, "d" => 4, + "e" => 5, "f" => 6, "g" => 7, "h" => 8 }; + my ($char, $digit) = split //, $coordinates; + print "Input: $coordinates\n"; + my $odd = ($map->{$char} + $digit) % 2; + if($odd) { + return print "Output: true\n"; + } + return print "Output: false\n"; +} diff --git a/challenge-281/jeanluc2020/perl/ch-2.pl b/challenge-281/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..9f10bebbe9 --- /dev/null +++ b/challenge-281/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-281/#TASK2 +# +# A Knight in chess can move from its current position to any square two rows +# or columns plus one column or row away. So in the diagram below, if it starts +# a S, it can move to any of the squares marked E. +# +# Write a script which takes a starting position and an ending position and +# calculates the least number of moves required. +# +# https://theweeklychallenge.org/images/blog/week_281_task_2.png +# +## Example 1 +## +## Input: $start = 'g2', $end = 'a8' +## Ouput: 4 +## +## g2 -> e3 -> d5 -> c7 -> a8 +# +## Example 2 +## +## Input: $start = 'g2', $end = 'h2' +## Ouput: 3 +## +## g2 -> e3 -> f1 -> h2 +# +############################################################ +## +## discussion +## +############################################################ +# +# Some observations: +# 1. The shortest path from one square to the directly neighboring +# square has length 5 +# Example: a8->c7->e6->d8->c6->b8 (for all other combinations +# it's just shifted around, mirrored, or rotated by 90°) +# 2. The shortest path from one square to its diagonal neighboar has +# length 2, except if it's in the corner (a8->b7 is one move to +# b6 or c7, then it's a case of walking to the directly neighboring +# square, so the length is 1+5=6 in this case) +# 3. The shortest path to cover most of the distance on the board from +# one end to the next (or the opposing corner) is basically 4, plus +# some more steps to jump to the exact square you want to end up in +# From this, we can assume there is an upper limit of how many steps are +# required to go from any square to any other square: 4+6=10 steps +# should be enough in any case. +# So we can brute-force this with a max-depth of 10. + +use strict; +use warnings; +use List::Util qw(min any); + +knights_move("g2", "a8"); +knights_move("g2", "h2"); + +sub knights_move { + my ($start, $end) = @_; + print "Input: $start -> $end\n"; + my $l = min_length($start, $end, 1, []); + print "Output: $l\n"; +} + +sub min_length { + my ($start, $end, $step, $seen) = @_; + # print "=> [" . join(",", @$seen) . "] $start, $end, $step\n"; + if($start eq $end) { + return 0; + } + if($step > 10 || any { $_ eq $start} @$seen) { + return 1000; + } + my $map = { "a" => 1, "b" => 2, "c" => 3, "d" => 4, + "e" => 5, "f" => 6, "g" => 7, "h" => 8 }; + my $reverse_map = { 1 => "a", 2 => "b", 3 => "c", 4 => "d", + 5 => "e", 6 => "f", 7 => "g", 8 => "h" }; + my ($char1, $digit1) = split //, $start; + my ($char2, $digit2) = split //, $end; + my @lengths = (); + foreach my $direction ( ([-2, 1], [-2,-1], [-1,-2], [-1,2], + [2,1], [2,-1], [1,2], [1,-2]) ) { + my $c = $map->{$char1} + $direction->[0]; + my $d = $digit1 + $direction->[1]; + if(legal( $c, $d)) { + next if any { $_ eq $start} @$seen; + my $l = 1 + min_length($reverse_map->{$c} . $d, $end, $step+1, [@$seen, $start] ); + next unless $l; + push @lengths, $l if $l < 1000; + } + } + return min(@lengths, 1000); +} + +sub legal { + my ($mapped_digit, $digit) = @_; + if($mapped_digit < 1 || $digit < 1) { + return 0; + } + if($mapped_digit > 8 || $digit > 8) { + return 0; + } + return 1; +} -- cgit From 5a74f63a80819bcaa7048779dadd508e09b114e5 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 5 Aug 2024 15:05:27 -0600 Subject: Solve PWC281 --- challenge-281/wlmb/blog.txt | 1 + challenge-281/wlmb/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-281/wlmb/perl/ch-2.pl | 42 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 challenge-281/wlmb/blog.txt create mode 100755 challenge-281/wlmb/perl/ch-1.pl create mode 100755 challenge-281/wlmb/perl/ch-2.pl diff --git a/challenge-281/wlmb/blog.txt b/challenge-281/wlmb/blog.txt new file mode 100644 index 0000000000..f505524efa --- /dev/null +++ b/challenge-281/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/08/04/PWC281/ diff --git a/challenge-281/wlmb/perl/ch-1.pl b/challenge-281/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..94438ac772 --- /dev/null +++ b/challenge-281/wlmb/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 281 +# Task 1: Check Color +# +# See https://wlmb.github.io/2024/08/04/PWC281/#task-1-check-color +use v5.36; +die <<~"FIN" unless @ARGV; + Usage $0 P1 P2... + to test whether position Pi in a chessboard corresponds to a white square, + where Pi is of the form xy with x a letter in the range a-h and y a digit + in the range 1-8. + FIN +for(@ARGV){ + warn("Bad format: $_"), next unless /^([a-hA-H])([1-7])$/; + my $x=ord(lc $1)-ord("a"); # convert to number 0-7 + my $y=$2-1; + my $result=($x+$y)%2?"True":"False"; + say "$_ -> $result"; +} diff --git a/challenge-281/wlmb/perl/ch-2.pl b/challenge-281/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..9911a6cb36 --- /dev/null +++ b/challenge-281/wlmb/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl +# Perl weekly challenge 281 +# Task 2: Knight’s Move +# +# See https://wlmb.github.io/2024/08/04/PWC281/#task-2-knight’s-move +use v5.36; +use PDL; +use PDL::NiceSlice; +use experimental qw(for_list); +die <<~"FIN" unless @ARGV && @ARGV%2==0; + Usage: $0 S1 E1... + to find how many moves are required for a knight to go from position Si + to position Ei in a chessboard. + The positions are given in the format xy where x is a letter in the + range a-h and denotes the horizontal position while y is a digit between 1 and 8 + and denotes vertical position. + FIN +my $moves=pdl[[1,2],[2,1]]; # Construct all moves of knight +my $signs=pdl[1,-1]; +my $allmoves=pdl($moves, $moves*$signs, -$moves*$signs,-$moves)->clump(1,2); +for my($start, $end)(@ARGV){ + my ($current, $goal)=map{ # convert to coordinate vectors, origin at (0,0) + warn("Bad format: $_"), next unless /^([a-hA-H])([1-8])$/; + pdl(ord(lc $1)-ord("a"), $2-1) + } ($start, $end); + my $iteration=0; + my $visited=zeroes(8,8); + $visited->indexND($current).=1; + until(($current==$goal)->andover->any){ # until we reach the goal + my $next=($current+$allmoves->dummy(1))->clump(1,2); # jump to new positions + $current=$next + ->dice_axis( # remove invalid positions + 1, which(($next((0))>=0)&($next((0))<8)&($next((1))>=0)&($next((1))<8))) + ->uniqvec; # remove duplicates + $current=$current->dice_axis( # remove previously visited + 1, which(!$visited->indexND($current)) + ); + $visited->indexND($current).=1; + ++$iteration; + } + say "From $start to $end -> $iteration movements"; +} -- cgit From 93a8115c85650e76505f7cb610b3a31cc61388c1 Mon Sep 17 00:00:00 2001 From: Conor Hoekstra Date: Mon, 5 Aug 2024 17:06:25 -0400 Subject: :sparkles: Week 281 in BQN --- challenge-281/conor-hoekstra/ch-1.bqn | 10 ++++++++++ challenge-281/conor-hoekstra/ch-2.bqn | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 challenge-281/conor-hoekstra/ch-1.bqn create mode 100644 challenge-281/conor-hoekstra/ch-2.bqn diff --git a/challenge-281/conor-hoekstra/ch-1.bqn b/challenge-281/conor-hoekstra/ch-1.bqn new file mode 100644 index 0000000000..3b0feeb2b6 --- /dev/null +++ b/challenge-281/conor-hoekstra/ch-1.bqn @@ -0,0 +1,10 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/281-1.bqn + +IsDark ← 2|·+´48|-⟜'0'¨ + +# Tests +•Show IsDark "d3" # 1 +•Show IsDark "g5" # 0 +•Show IsDark "e6" # 1 +•Show IsDark "a1" # 0 diff --git a/challenge-281/conor-hoekstra/ch-2.bqn b/challenge-281/conor-hoekstra/ch-2.bqn new file mode 100644 index 0000000000..4289be1dab --- /dev/null +++ b/challenge-281/conor-hoekstra/ch-2.bqn @@ -0,0 +1,16 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/281-2.bqn + +Convert ← 48|-⟜'0'¨ +deltas ← (⌽¨⊸∾)∾<˘2‿¯2⋈⌜1‿¯1 +IsValid ← ∧´1⊸≤∧8⊸≥ +Moves ← {IsValid¨⊸/⥊𝕩+⌜deltas} + +MinKnightMoves ← { + end‿start ← 𝕨 ⋈○Convert 𝕩 + 1 end { ∨´𝕗⊸≡¨Moves 𝕩 ? 𝕨 ; (𝕨+1) 𝕊 Moves 𝕩 } ⟨start⟩ +} + +# Tests +•Show "g2" MinKnightMoves "a8" # 4 +•Show "g2" MinKnightMoves "h2" # 3 -- cgit From 40e0872043f93838791d288a94b44068bbabfa71 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 5 Aug 2024 19:13:07 -0400 Subject: DAJ 281 --- challenge-281/dave-jacoby/blog.txt | 1 + challenge-281/dave-jacoby/perl/ch-1.pl | 22 ++++++++ challenge-281/dave-jacoby/perl/ch-2.pl | 99 ++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 challenge-281/dave-jacoby/blog.txt create mode 100644 challenge-281/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-281/dave-jacoby/perl/ch-2.pl diff --git a/challenge-281/dave-jacoby/blog.txt b/challenge-281/dave-jacoby/blog.txt new file mode 100644 index 0000000000..fd60fc879a --- /dev/null +++ b/challenge-281/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2024/08/05/pawning-things-off-weekly-challenge-281.html diff --git a/challenge-281/dave-jacoby/perl/ch-1.pl b/challenge-281/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..ca81d9bcd6 --- /dev/null +++ b/challenge-281/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +my @examples = (qw{ d3 g5 e6 }); + +for my $example (@examples) { + my $output = color_check($example); + say <<"END"; + Input: \@str = "$example" + Output: $output +END +} + +sub color_check ($input) { + my ( $l, $n ) = split //, $input; + my @lets = 'a' .. 'h'; + my %lets = map { $lets[$_] => $_ } 0 .. -1 + scalar @lets; + return ( $lets{$l} + ( $n % 2 ) ) % 2 ? 'false' : 'true '; +} diff --git a/challenge-281/dave-jacoby/perl/ch-2.pl b/challenge-281/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..84a7402b54 --- /dev/null +++ b/challenge-281/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( # added a couple test entries + + [ 'g2', 'a8' ], + [ 'g2', 'h2' ], + [ 'a1', 'h8' ], + [ 'd5', 'e4' ], +); + +for my $input (@examples) { + my $output = knights_move($input); + my ( $start, $end ) = $input->@*; + say <<"END"; + Input: \$start = "$start", \$end = "$end" + Output: $output +END +} + +sub knights_move($input) { + my ( $start, $end ) = $input->@*; + my $board = {}; # used for display + my $done = {}; # used to track the win + my @end = split //, $end; + $board->{ $end[0] }{ $end[1] } = -2; + $done->{$end} = 'E'; + + + # these get us from 'a1' to [7,0] and back + my @lets = 'a' .. 'h'; + my %lets = map { $lets[$_] => $_ } 0 .. -1 + scalar @lets; + my %stel = reverse %lets; + + my @nums = reverse 1 .. 8; + my %nums = map { $nums[$_] => $_ } 0 .. -1 + scalar @nums; + my %smun = reverse %nums; + + my @moves = ( [ $start, 0 ] ); + my @jumps = ( + [ -1, -2 ], [ -1, 2 ], [ -2, -1 ], [ -2, 1 ], + [ 1, -2 ], [ 1, 2 ], [ 2, -1 ], [ 2, 1 ], + ); + + for my $move (@moves) { + my ( $space, $depth ) = $move->@*; + my ( $l, $n ) = split //, $space; + if ( defined $done->{$space} ) { + if ( $done->{$space} eq 'E' ) { + # display_board($board); + return $depth; + } + next; + } + + $board->{$l}{$n} = $depth; + $done->{$space} = $depth; + + for my $jump (@jumps) { + my ( $i, $j ) = $jump->@*; + my $ll = $lets{$l} + $i; + my $nn = $nums{$n} + $j; + + if ( $ll >= 0 && $ll <= 7 ) { + if ( $nn >= 0 && $nn <= 7 ) { + my $new = join '', $stel{$ll}, $smun{$nn}; + push @moves, [ $new, $depth + 1 ]; + } + } + } + } + + # There's always a way, but I don't like not providing an unaccounted + # case + return 'fail'; +} + +sub display_board ($board) { + my @lets = 'a' .. 'h'; + my @nums = reverse 1 .. 8; + for my $num (@nums) { + print qq{$num }; + for my $let (@lets) { + my $char = '_'; + if ( defined $board->{$let}{$num} ) { + $char = + $board->{$let}{$num} == -2 + ? 'E' + : $board->{$let}{$num}; + } + print qq{ [$char] }; + } + say ''; + } + say join ' ', ' ', @lets; +} -- cgit From b029a5cccde12c8c49738508cfa0d003e5856adc Mon Sep 17 00:00:00 2001 From: KjetilS Date: Tue, 6 Aug 2024 01:34:57 +0200 Subject: https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ --- challenge-281/kjetillll/perl/ch-1.pl | 6 ++++++ challenge-281/kjetillll/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 challenge-281/kjetillll/perl/ch-1.pl create mode 100644 challenge-281/kjetillll/perl/ch-2.pl diff --git a/challenge-281/kjetillll/perl/ch-1.pl b/challenge-281/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..cae2900422 --- /dev/null +++ b/challenge-281/kjetillll/perl/ch-1.pl @@ -0,0 +1,6 @@ +use strict; use warnings; use Test::More tests=>2; + +sub is_light { pop() =~ /^( [aceg][2468] | [bdfh][1357] )$/x } + +ok is_light( "d3" ) == 1; +ok is_light( "g5" ) == 0; diff --git a/challenge-281/kjetillll/perl/ch-2.pl b/challenge-281/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..6d2836b302 --- /dev/null +++ b/challenge-281/kjetillll/perl/ch-2.pl @@ -0,0 +1,26 @@ +use strict; use warnings; use Test::More; + +sub knight_steps { + #represent the input positions in @_ of the form a1, b2 and so on as + #numeric positions instead where the letters a-h becomes 10 20 30 40 50 60 70 80 + #and concat that with the digit so for example a1 becomes 101, c7 -> 307, h8 -> 808 + + my( $from, $to ) = map s/./ 10*ord($&)-960 /er, @_; #convert input as described + my @check = ( [$from, 0] ); + while( my($pos, $steps) = @{ shift @check } ){ + return $steps if $pos eq $to; + push @check, #push into work list + map [$_, $steps+1], #register new posible jumps + grep /[1-8]0[1-8]/, #don't keep jumps outside board + map $pos + $_, #jump to + 201, 102, 199, 98, #valid jumps + -201, -102, -199, -98; + } +} + +ok knight_steps('g2' => 'a8') == 4; +ok knight_steps('g2' => 'h2') == 3; +ok knight_steps('h1' => 'a8') == 6; +ok knight_steps('c3' => 'c4') == 3; +ok knight_steps('a1' => 'a1') == 0; +done_testing; -- cgit From 39c5867315fd94c62bdf0bef5767247348b1781e Mon Sep 17 00:00:00 2001 From: KjetilS Date: Tue, 6 Aug 2024 01:35:25 +0200 Subject: improvement of last weeks answer, uncomplicate it --- challenge-280/kjetillll/perl/ch-2.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-280/kjetillll/perl/ch-2.pl b/challenge-280/kjetillll/perl/ch-2.pl index 904a03eb3f..afc5b87f93 100644 --- a/challenge-280/kjetillll/perl/ch-2.pl +++ b/challenge-280/kjetillll/perl/ch-2.pl @@ -2,7 +2,8 @@ use strict; use warnings; use Test::More tests=>3; sub count { local $_ = shift; - s/ \| .*? \| //x ? count($_) : y/*// + s/ \| .*? \| //xg; + y/*// } is count( $$_{input} ) => $$_{output} -- cgit From 070329afb38475de9ad40bcadd50ea1672126940 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 6 Aug 2024 11:21:46 +1000 Subject: pwc281 solution in python --- challenge-281/pokgopun/python/ch-1.py | 52 ++++++++++++++++++++++ challenge-281/pokgopun/python/ch-2.py | 83 +++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 challenge-281/pokgopun/python/ch-1.py create mode 100644 challenge-281/pokgopun/python/ch-2.py diff --git a/challenge-281/pokgopun/python/ch-1.py b/challenge-281/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..6b74504f58 --- /dev/null +++ b/challenge-281/pokgopun/python/ch-1.py @@ -0,0 +1,52 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ +""" + +Task 1: Check Color + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given coordinates, a string that represents the coordinates of + a square of the chessboard as shown below: + + Week_281_Task_1 + + Write a script to return true if the square is light, and false if the + square is dark. + +Example 1 + +Input: $coordinates = "d3" +Output: true + +Example 2 + +Input: $coordinates = "g5" +Output: false + +Example 3 + +Input: $coordinates = "e6" +Output: true + +Task 2: Knight’s Move +""" +### solution by pokgopun@gmail.com + +def checkColor(string: str): + return sum( + ord(c) for c in string + ) % 2 == 1 + +import unittest + +class TestCheckColor(unittest.TestCase): + def test(self): + for inpt, otpt in { + "d3": True, + "g5": False, + "e6": True, + }.items(): + self.assertEqual(checkColor(inpt),otpt) + +unittest.main() diff --git a/challenge-281/pokgopun/python/ch-2.py b/challenge-281/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..7e6b5f5807 --- /dev/null +++ b/challenge-281/pokgopun/python/ch-2.py @@ -0,0 +1,83 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ +""" + +Task 2: Knight’s Move + +Submitted by: [48]Peter Campbell Smith + __________________________________________________________________ + + A Knight in chess can move from its current position to any square two + rows or columns plus one column or row away. So in the diagram below, + if it starts a S, it can move to any of the squares marked E. + + Write a script which takes a starting position and an ending position + and calculates the least number of moves required. + + Week_281_Task_2 + +Example 1 + +Input: $start = 'g2', $end = 'a8' +Ouput: 4 + +g2 -> e3 -> d5 -> c7 -> a8 + +Example 2 + +Input: $start = 'g2', $end = 'h2' +Ouput: 3 + +g2 -> e3 -> f1 -> h2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com +### https://afteracademy.com/blog/knight-on-chessboard/#:~:text=Solution,the%20shape%20of%20an%20L). + +def pos2xy(string: str): + return ord(string[0])-97, ord(string[1])-49 + +def xy2pos(x, y): + return chr(97+x)+chr(49+y) + +def isOnBoard(x, y): + return x >=0 and x < 8 and y >= 0 and y < 8 + +def knightMove(start, end): + XYd = ( (-2,-1), (-1,-2), ( 1,-2), ( 2,-1), (-2, 1), (-1, 2), ( 1, 2), ( 2, 1) ) + XYa = tuple([None for y in range(8)] for x in range(8)) + x0, y0 = pos2xy(start) + x1, y1 = pos2xy(end) + #XYa[x0][y0] = 0 + XYa[x0][y0] = "" + XYq = [(x0, y0)] + while len(XYq) > 0: + x, y = XYq.pop(0) + if (x,y) == (x1,y1): + #return XYa[x][y] + print(XYa[x][y]+end,"=>",int(len(XYa[x][y])/2)) + return int(len(XYa[x][y])/2) + for dx, dy in XYd: + xn, yn = x + dx, y + dy + if isOnBoard(xn, yn) and XYa[xn][yn] is None: + #XYa[xn][yn] = XYa[x][y] + 1 + XYa[xn][yn] = XYa[x][y] + xy2pos(x,y) + XYq.append((xn,yn)) + return -1 + +import unittest + +class TestKnightMove(unittest.TestCase): + def test(self): + for (start,end), count in { + ('g2', 'a8'): 4, + ('g2', 'h2'): 3, + }.items(): + self.assertEqual(knightMove(start, end), count) + +unittest.main() -- cgit From 4b08f96f7df8de14e0c77b7fabbde2ade95718d2 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 5 Aug 2024 23:00:40 -0400 Subject: Challenge 281 solutions by Packy Anderson * Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir (Task 1 only so far) 1 Blog post --- challenge-281/packy-anderson/README.md | 2 +- challenge-281/packy-anderson/blog.txt | 1 + challenge-281/packy-anderson/blog/graphviz.dot | 59 ++++++++++++++++++ challenge-281/packy-anderson/blog/graphviz.png | Bin 0 -> 468918 bytes challenge-281/packy-anderson/elixir/ch-1.exs | 31 ++++++++++ challenge-281/packy-anderson/elixir/ch-2.exs | 20 ++++++ challenge-281/packy-anderson/perl/ch-1.pl | 25 ++++++++ challenge-281/packy-anderson/perl/ch-2.pl | 81 +++++++++++++++++++++++++ challenge-281/packy-anderson/python/ch-1.py | 23 +++++++ challenge-281/packy-anderson/python/ch-2.py | 77 +++++++++++++++++++++++ challenge-281/packy-anderson/raku/ch-1.raku | 25 ++++++++ challenge-281/packy-anderson/raku/ch-2.raku | 81 +++++++++++++++++++++++++ 12 files changed, 424 insertions(+), 1 deletion(-) create mode 100644 challenge-281/packy-anderson/blog.txt create mode 100644 challenge-281/packy-anderson/blog/graphviz.dot create mode 100644 challenge-281/packy-anderson/blog/graphviz.png create mode 100755 challenge-281/packy-anderson/elixir/ch-1.exs create mode 100755 challenge-281/packy-anderson/elixir/ch-2.exs create mode 100755 challenge-281/packy-anderson/perl/ch-1.pl create mode 100755 challenge-281/packy-anderson/perl/ch-2.pl create mode 100755 challenge-281/packy-anderson/python/ch-1.py create mode 100755 challenge-281/packy-anderson/python/ch-2.py create mode 100755 challenge-281/packy-anderson/raku/ch-1.raku create mode 100755 challenge-281/packy-anderson/raku/ch-2.raku diff --git a/challenge-281/packy-anderson/README.md b/challenge-281/packy-anderson/README.md index 7974906632..69be440f4b 100644 --- a/challenge-281/packy-anderson/README.md +++ b/challenge-281/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Perl Weekly Challenge: Appear Twice, Count Once](https://packy.dardan.com/b/P_) +[Perl Weekly Challenge: The Ultimate Test of Cerebral Fitness](https://packy.dardan.com/b/Pd) diff --git a/challenge-281/packy-anderson/blog.txt b/challenge-281/packy-anderson/blog.txt new file mode 100644 index 0000000000..84c5dd54ff --- /dev/null +++ b/challenge-281/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Pd \ No newline at end of file diff --git a/challenge-281/packy-anderson/blog/graphviz.dot b/challenge-281/packy-anderson/blog/graphviz.dot new file mode 100644 index 0000000000..5651258a6a --- /dev/null +++ b/challenge-281/packy-anderson/blog/graphviz.dot @@ -0,0 +1,59 @@ +digraph G { + node [style=filled] + edge [style=dashed] + g2 -> e3 -> c4 -> b6 -> a8 [style=solid, color=red]; + g2 -> e1 + g2 -> f4 + g2 -> h4 + e1 -> c2 + e1 -> d3 + e1 -> f3 + e3 -> d1 + e3 -> d5 + e3 -> g4 + e3 -> f1 + e3 -> f5 + f4 -> e2 + f4 -> e6 + f4 -> h3 + f4 -> h5 + f4 -> g6 + c2 -> a1 + c2 -> a3 + c2 -> b4 + c2 -> d4 + d3 -> b2 + d3 -> c1 + d3 -> c5 + d3 -> f2 + d3 -> e5 + f3 -> d2 + f3 -> h2 + f3 -> g1 + f3 -> g5 + c4 -> a5 + c4 -> d6 + d1 -> c3 + d5 -> c7 + d5 -> f6 + d5 -> e7 + g4 -> h6 + f1 -> g3 + f5 -> g7 + e6 -> d8 + e6 -> f8 + g6 -> h8 + a1 -> b3 + a3 -> b1 + a3 -> b5 + b4 -> a2 + b4 -> a6 + b4 -> c6 + b2 -> a4 + c5 -> b7 + c5 -> e4 + c5 -> d7 + f2 -> h1 + e5 -> f7 + g5 -> h7 +} \ No newline at end of file diff --git a/challenge-281/packy-anderson/blog/graphviz.png b/challenge-281/packy-anderson/blog/graphviz.png new file mode 100644 index 0000000000..a0ec8a2060 Binary files /dev/null and b/challenge-281/packy-anderson/blog/graphviz.png differ diff --git a/challenge-281/packy-anderson/elixir/ch-1.exs b/challenge-281/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..ebf52874c3 --- /dev/null +++ b/challenge-281/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,31 @@ +#!/usr/bin/env elixir + +defmodule PWC do + require Integer + + def isLight(coordinates) do + letter = String.first(coordinates) + {num, _} = Integer.parse(String.last(coordinates)) + cond do + String.contains?("aceg", letter) + and Integer.is_even(num) -> "true" + String.contains?("bdfh", letter) + and Integer.is_odd(num) -> "true" + true -> "false" + end + end + + def solution(coordinates) do + IO.puts("Input: $coordinates = \"#{coordinates}\"") + IO.puts("Output: " <> isLight(coordinates) ) + end +end + +IO.puts("Example 1:") +PWC.solution("d3") + +IO.puts("\nExample 2:") +PWC.solution("g5") + +IO.puts("\nExample 3:") +PWC.solution("e6") diff --git a/challenge-281/packy-anderson/elixir/ch-2.exs b/challenge-281/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..04b5c8529b --- /dev/null +++ b/challenge-281/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,20 @@ +#!/usr/bin/env elixir + +defmodule PWC do + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + {sign, explain} = PWC.productSign(ints) + IO.puts("Output: " <> to_string(sign) ) + IO.puts("\n" <> explain) + end +end + +IO.puts("Example 1:") +PWC.solution() + +IO.puts("\nExample 2:") +PWC.solution() + +IO.puts("\nExample 3:") +PWC.solution() diff --git a/challenge-281/packy-anderson/perl/ch-1.pl b/challenge-281/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..ff595d7c57 --- /dev/null +++ b/challenge-281/packy-anderson/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use v5.40; + +sub isLight($coordinates) { + my ($letter, $num) = split //, $coordinates; + return ( + ( ($letter =~ /[aceg]/) && ($num % 2 == 0) ) + || + ( ($letter =~ /[bdfh]/) && ($num % 2 == 1) ) + ) ? 'true' : 'false'; +} + +sub solution($coordinates) { + say qq/Input: \$coordinates = "$coordinates"/; + say 'Output: ' . isLight($coordinates); +} + +say "Example 1:"; +solution("d3"); + +say "\nExample 2:"; +solution("g5"); + +say "\nExample 3:"; +solution("e6"); diff --git a/challenge-281/packy-anderson/perl/ch-2.pl b/challenge-281/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..92eeb1cbd4 --- /dev/null +++ b/challenge-281/packy-anderson/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +use v5.40; + +my @knightMoveList = ( + [-2, -1], [-2, +1], [-1, -2], [-1, +2], + [+2, -1], [+2, +1], [+1, -2], [+1, +2], +); + +sub knightMoves($coordinates) { + my ($letter, $num) = split //, $coordinates; + my @endpoints; + foreach my $colRow ( @knightMoveList ) { + my ($col, $row) = @$colRow; + my $newcol = chr(ord($letter) + $col); + next unless $newcol ge "a" && $newcol le "h"; + my $newrow = $num + $row; + next unless 1 <= $newrow <= 8; + push @endpoints, ($newcol . $newrow); + } + return @endpoints; +} + +sub leastMoves($start, $end) { + # trivial case: we're already at the end point + return ( 0, $end ) if $start eq $end; + + # Ok, we're going to need to search for a solution. + + # Keep track of how many moves it takes to get to + # a particular position, starting at $start + my %moves = ( $start => 0 ); + + # also keep track of the path to get there + my %path_to = ( $start => $start ); + + # make a queue of starting points + my @queue = ( $start ); + + while ( @queue ) { + $start = shift @queue; + + # figure out the valid moves that we haven't been to yet + my @endpoints = grep { + ! exists $path_to{$_} + } knightMoves($start); + + foreach my $next ( @endpoints ) { + # build the path to the next endpoint + $path_to{$next} = $path_to{$start} . " -> $next"; + + # increment the number of moves it takes to get there + $moves{$next} = $moves{$start} + 1; + + # have we arrived at our destination + return ( $moves{$next}, $path_to{$next} ) if $next eq $end; + + # no? then push this space onto our processing queue + push @queue, $next; + } + } + + # we can't get there from here! + # (only possible when the chessboard is an odd size) + return ( -1, "no path found" ); +} + +sub solution($start, $end) { + say qq/Input: \$start = '$start', \$end = '$end'/; + my ($count, $moves) = leastMoves($start, $end); + say 'Output: ' . $count; + say "\n$moves\n"; +} + +say "Example 1:"; +solution('g2', 'a8'); + +say "\nExample 2:"; +solution('g2', 'h2'); + +say "\nExample 3:"; +solution('a1', 'h8'); diff --git a/challenge-281/packy-anderson/python/ch-1.py b/challenge-281/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..a327de57b8 --- /dev/null +++ b/challenge-281/packy-anderson/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +def isLight(coordinates): + letter = coordinates[0:1] + num = int(coordinates[1:]) + return ( + ( (letter in "aceg") and (num % 2 == 0) ) + or + ( (letter in "bdfh") and (num % 2 == 1) ) + ) + +def solution(coordinates): + print(f'Input: $coordinates = "{coordinates}"') + print(f'Output: {isLight(coordinates)}') + +print('Example 1:') +solution("d3") + +print('\nExample 2:') +solution("g5") + +print('\nExample 3:') +solution("e6") diff --git a/challenge-281/packy-anderson/python/ch-2.py b/challenge-281/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..f1b8803a71 --- /dev/null +++ b/challenge-281/packy-anderson/python/ch-2.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +knightMoveList = [ + [-2, -1], [-2, +1], [-1, -2], [-1, +2], + [+2, -1], [+2, +1], [+1, -2], [+1, +2], +] + +def knightMoves(coordinates): + letter = coordinates[0:1] + num = int(coordinates[1:]) + endpoints = [] + for colRow in knightMoveList: + col, row = colRow + newcol = chr(ord(letter) + col) + if "a" <= newcol <= "h": + newrow = num + row + if 1 <= newrow <= 8: + endpoints.append(newcol + str(newrow)) + return endpoints + + +def leastMoves(start, end): + # trivial case: we're already at the end point + if start == end: + return ( 0, end ) + + # Ok, we're going to need to search for a solution. + + # Keep track of how many moves it takes to get to + # a particular position, starting at $start + moves = { start: 0 } + + # also keep track of the path to get there + path_to = { start: start } + + # make a queue of starting points + queue = [ start ] + + while ( queue ): + start = queue.pop(0) + + # figure out the valid moves that we haven't been to yet + endpoints = [ + m for m in knightMoves(start) if m not in path_to + ] + + for next in endpoints: + # build the path to the next endpoint + path_to[next] = f'{path_to[start]} -> {next}' + + # increment the number of moves it takes to get there + moves[next] = moves[start] + 1 + + # have we arrived at our destination + if next == end: + return ( moves[next], path_to[next] ) + + # no? then push this space onto our processing queue + queue.append(next) + + # we can't get there from here! + # (only possible when the chessboard is an odd size) + return ( -1, "no path found" ) + +def solution(start, end): + print(f'Input: $start = \'{start}\', $end = \'{end}\'') + count, moves = leastMoves(start, end) + print(f'Output: {count}\n\n{moves}\n') + +print('Example 1:') +solution('g2', 'a8') + +print('\nExample 2:') +solution('g2', 'h2') + +print('\nExample 3:') +solution('a1', 'h8') diff --git a/challenge-281/packy-anderson/raku/ch-1.raku b/challenge-281/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..44ea131900 --- /dev/null +++ b/challenge-281/packy-anderson/raku/ch-1.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku +use v6; + +sub isLight($coordinates) { + my ($letter, $num) = $coordinates.comb; + return ( + $letter.match(/<[aceg]>/) && ($num %% 2) + || + $letter.match(/<[bdfh]>/) && !($num %% 2) + ) ?? 'true' !! 'false'; +} + +sub solution($coordinates) { + say qq/Input: \$coordinates = "$coordinates"/; + say 'Output: ' ~ isLight($coordinates); +} + +say "Example 1:"; +solution("d3"); + +say "\nExample 2:"; +solution("g5"); + +say "\nExample 3:"; +solution("e6"); diff --git a/challenge-281/packy-anderson/raku/ch-2.raku b/challenge-281/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..4ab5b7f4b1 --- /dev/null +++ b/challenge-281/packy-anderson/raku/ch-2.raku @@ -0,0 +1,81 @@ +#!/usr/bin/env raku +use v6; + +my @knightMoveList = ( + (-2, -1), (-2, +1), (-1, -2), (-1, +2), + (+2, -1), (+2, +1), (+1, -2), (+1, +2), +); + +sub knightMoves($coordinates) { + my ($letter, $num) = $coordinates.comb; + my @endpoints; + for @knightMoveList -> @colRow { + my ($col, $row) = @colRow; + my $newcol = ($letter.ord + $col).chr; + next unless $newcol ge "a" && $newcol le "h"; + my $newrow = $num + $row; + next unless 1 <= $newrow <= 8; + @endpoints.push($newcol ~ $newrow); + } + return @endpoints; +} + +sub leastMoves($start is copy, $end) { + # trivial case: we're already at the end point + return ( 0, $end ) if $start eq $end; + + # Ok, we're going to need to search for a solution. + + # Keep track of how many moves it takes to get to + # a particular position, starting at $start + my %moves = ( $start => 0 ); + + # also keep track of the path to get there + my %path_to = ( $start => $start ); + + # make a queue of starting points + my @queue = ( $start ); + + while ( @queue ) { + $start = @queue.shift; + + # figure out the valid moves that we haven't been to yet + my @endpoints = knightMoves($start).grep({ + %path_to{$_}:!exists + }); + + for @endpoints -> $next { + # build the path to the next endpoint + %path_to{$next} = %path_to{$start} ~ " -> $next"; + + # increment the number of moves it takes to get there + %moves{$next} = %moves{$start} + 1; + + # have we arrived at our destination + return ( %moves{$next}, %path_to{$next} ) if $next eq $end; + + # no? then push this space onto our processing queue + @queue.push: $next; + } + } + + # we can't get there from here! + # (only possible when the chessboard is an odd size) + return ( -1, "no path found" ); +} + +sub solution($start, $end) { + say qq/Input: \$start = '$start', \$end = '$end'/; + my ($count, $moves) = leastMoves($start, $end); + say 'Output: ' ~ $count; + say "\n$moves\n"; +} + +say "Example 1:"; +solution('g2', 'a8'); + +say "\nExample 2:"; +solution('g2', 'h2'); + +say "\nExample 3:"; +solution('a1', 'h8'); -- cgit From 24a5e6524c7a1548a7cd66e4696e0dec64929523 Mon Sep 17 00:00:00 2001 From: Tymoteusz Moryto Date: Tue, 6 Aug 2024 09:59:49 +0200 Subject: solutions for week 281 in uiua --- challenge-281/pieviero/ch-1.ua | 4 ++++ challenge-281/pieviero/ch-2.ua | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 challenge-281/pieviero/ch-1.ua create mode 100644 challenge-281/pieviero/ch-2.ua diff --git a/challenge-281/pieviero/ch-1.ua b/challenge-281/pieviero/ch-1.ua new file mode 100644 index 0000000000..d12fbc8f59 --- /dev/null +++ b/challenge-281/pieviero/ch-1.ua @@ -0,0 +1,4 @@ +CheckColor ← ¬◿2+⊓-⋕@a°⊂ +CheckColor "d3" +CheckColor "g5" +CheckColor "e6" diff --git a/challenge-281/pieviero/ch-2.ua b/challenge-281/pieviero/ch-2.ua new file mode 100644 index 0000000000..bc1e37e89a --- /dev/null +++ b/challenge-281/pieviero/ch-2.ua @@ -0,0 +1,14 @@ +Diffs ← [[1 2] [2 1] + [1 ¯2] [2 ¯1] + [¯1 2] [¯2 1] + [¯1 ¯2] [¯2 ¯1]] +Convert ← ⍜°⊂(⊓-⋕-1@a) +IsValid ← ×⊓>≤0,8 +FilterMoves ← ▽≡/×IsValid. +ApplyDiffs ← +Diffs¤ + +# ? end start +KnightsMove ← ◌◌⍢(⊙⊙(+1)◴FilterMoves☇1≡ApplyDiffs|=0/+∊:,,)⊙⊙(0)¤:∩Convert +KnightsMove "a8" "g2" +KnightsMove "h2" "g2" + -- cgit From 8bd7329fb0dec9a57bb33590877839ac69728b82 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 6 Aug 2024 18:59:36 +1000 Subject: pwc281 solution in go --- challenge-281/pokgopun/go/ch-1.go | 60 +++++++++++++++++++++ challenge-281/pokgopun/go/ch-2.go | 109 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 challenge-281/pokgopun/go/ch-1.go create mode 100644 challenge-281/pokgopun/go/ch-2.go diff --git a/challenge-281/pokgopun/go/ch-1.go b/challenge-281/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..7a10b39811 --- /dev/null +++ b/challenge-281/pokgopun/go/ch-1.go @@ -0,0 +1,60 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ +/*# + +Task 1: Check Color + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given coordinates, a string that represents the coordinates of + a square of the chessboard as shown below: + + Week_281_Task_1 + + Write a script to return true if the square is light, and false if the + square is dark. + +Example 1 + +Input: $coordinates = "d3" +Output: true + +Example 2 + +Input: $coordinates = "g5" +Output: false + +Example 3 + +Input: $coordinates = "e6" +Output: true + +Task 2: Knight’s Move +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func checkColor(str string) bool { + return ((str[0] % 2) + (str[1] % 2)) == 1 +} + +func main() { + for _, data := range []struct { + input string + output bool + }{ + {"d3", true}, + {"g5", false}, + {"e6", true}, + } { + io.WriteString(os.Stdout, cmp.Diff(checkColor(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-281/pokgopun/go/ch-2.go b/challenge-281/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9f4f229646 --- /dev/null +++ b/challenge-281/pokgopun/go/ch-2.go @@ -0,0 +1,109 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-281/ +/*# + +Task 2: Knight’s Move + +Submitted by: [48]Peter Campbell Smith + __________________________________________________________________ + + A Knight in chess can move from its current position to any square two + rows or columns plus one column or row away. So in the diagram below, + if it starts a S, it can move to any of the squares marked E. + + Write a script which takes a starting position and an ending position + and calculates the least number of moves required. + + Week_281_Task_2 + +Example 1 + +Input: $start = 'g2', $end = 'a8' +Ouput: 4 + +g2 -> e3 -> d5 -> c7 -> a8 + +Example 2 + +Input: $start = 'g2', $end = 'h2' +Ouput: 3 + +g2 -> e3 -> f1 -> h2 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com +// https://afteracademy.com/blog/knight-on-chessboard/#:~:text=Solution,the%20shape%20of%20an%20L). + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type position struct { + x, y int +} + +func newPosition(label string) position { + return position{int(label[0]) - 96, int(label[1]) - 48} +} + +func (pt position) String() string { + return string([]byte{byte(pt.x) + 96, byte(pt.y) + 48}) +} + +type data struct { + count int + visited bool +} + +type board [8][8]data + +func (bd board) contains(p position) bool { + return p.x >= 1 && p.x <= 8 && p.y >= 1 && p.y <= 8 +} + +func knightMove(start, end string) int { + var bd board + p0 := newPosition(start) + p1 := newPosition(end) + queue := []position{p0} + bd[p0.x-1][p0.y-1] = data{visited: true} + dx := [8]int{-2, -1, 1, 2, -2, -1, 1, 2} + dy := [8]int{-1, -2, -2, -1, 1, 2, 2, 1} + for len(queue) > 0 { + p := queue[0] + queue = queue[1:] + if p.x == p1.x && p.y == p1.y { + return bd[p.x-1][p.y-1].count + } + for i := 0; i < 8; i++ { + pn := position{p.x + dx[i], p.y + dy[i]} + if bd.contains(pn) && !bd[pn.x-1][pn.y-1].visited { + bd[pn.x-1][pn.y-1] = data{visited: true, count: bd[p.x-1][p.y-1].count + 1} + queue = append(queue, pn) + } + } + } + return -1 +} + +func main() { + for _, data := range []struct { + start, end string + count int + }{ + {"g2", "a8", 4}, + {"g2", "h2", 3}, + } { + io.WriteString(os.Stdout, cmp.Diff(knightMove(data.start, data.end), data.count)) // blank if ok, otherwise show the difference + } +} -- cgit From 4628d1d540ae4e64ef1919b57437bce2f962248b Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 6 Aug 2024 10:05:21 +0100 Subject: - Added solutions by Conor Hoekstra. - Added solutions by Thomas Kohler. - Added solutions by W. Luis Mochan. - Added solutions by Kjetil Skotheim. - Added solutions by Dave Jacoby. - Added solutions by Packy Anderson. - Added solutions by Feng Chang. - Added solutions by Tymoteusz Moryto. - Added solutions by PokGoPun. --- stats/pwc-current.json | 266 +++++++--- stats/pwc-language-breakdown-2019.json | 320 ++++++------ stats/pwc-language-breakdown-2020.json | 770 ++++++++++++++--------------- stats/pwc-language-breakdown-2021.json | 414 ++++++++-------- stats/pwc-language-breakdown-2022.json | 782 ++++++++++++++--------------- stats/pwc-language-breakdown-2023.json | 784 +++++++++++++++--------------- stats/pwc-language-breakdown-2024.json | 500 +++++++++---------- stats/pwc-language-breakdown-summary.json | 76 +-- stats/pwc-leaders.json | 766 ++++++++++++++--------------- stats/pwc-summary-1-30.json | 114 ++--- stats/pwc-summary-121-150.json | 108 ++-- stats/pwc-summary-151-180.json | 116 ++--- stats/pwc-summary-181-210.json | 106 ++-- stats/pwc-summary-211-240.json | 100 ++-- stats/pwc-summary-241-270.json | 50 +- stats/pwc-summary-271-300.json | 48 +- stats/pwc-summary-301-330.json | 42 +- stats/pwc-summary-31-60.json | 96 ++-- stats/pwc-summary-61-90.json | 102 ++-- stats/pwc-summary-91-120.json | 38 +- stats/pwc-summary.json | 60 +-- stats/pwc-yearly-language-summary.json | 146 +++--- 22 files changed, 2957 insertions(+), 2847 deletions(-) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 4e0ea5448a..153a1f7fac 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,103 +1,57 @@ { - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "xAxis" : { - "type" : "category" - }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - } - } + "title" : { + "text" : "The Weekly Challenge - 281" }, "legend" : { "enabled" : 0 }, - "series" : [ - { - "data" : [ - { - "y" : 2, - "name" : "Alexander Karelas", - "drilldown" : "Alexander Karelas" - }, - { - "y" : 2, - "name" : "David Ferrone", - "drilldown" : "David Ferrone" - }, - { - "y" : 2, - "name" : "E. Choroba", - "drilldown" : "E. Choroba" - }, - { - "drilldown" : "Jan Krnavek", - "name" : "Jan Krnavek", - "y" : 2 - }, - { - "drilldown" : "Laurent Rosenfeld", - "y" : 3, - "name" : "Laurent Rosenfeld" - }, - { - "drilldown" : "Mariano Ortega", - "y" : 2, - "name" : "Mariano Ortega" - }, - { - "drilldown" : "Mark Anderson", - "y" : 2, - "name" : "Mark Anderson" - } - ], - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 281" - } - ], "chart" : { "type" : "column" }, - "tooltip" : { - "pointFormat" : "{point.name}: {point.y:f}
", - "followPointer" : 1, - "headerFormat" : "{series.name}
" - }, - "title" : { - "text" : "The Weekly Challenge - 281" + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } }, "drilldown" : { "series" : [ { - "id" : "Alexander Karelas", "data" : [ [ "Perl", 2 ] ], - "name" : "Alexander Karelas" + "name" : "Alexander Karelas", + "id" : "Alexander Karelas" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Dave Jacoby", + "id" : "Dave Jacoby" }, { - "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] ], - "id" : "David Ferrone" + "id" : "David Ferrone", + "name" : "David Ferrone" }, { - "name" : "E. Choroba", "id" : "E. Choroba", + "name" : "E. Choroba", "data" : [ [ "Perl", @@ -106,17 +60,36 @@ ] }, { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Feng Chang", + "id" : "Feng Chang" + }, + { + "id" : "Jan Krnavek", "name" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] ], - "id" : "Jan Krnavek" + "name" : "Kjetil Skotheim", + "id" : "Kjetil Skotheim" }, { - "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -131,6 +104,7 @@ 1 ] ], + "id" : "Laurent Rosenfeld", "name" : "Laurent Rosenfeld" }, { @@ -140,22 +114,158 @@ 2 ] ], - "id" : "Mariano Ortega", - "name" : "Mariano Ortega" + "name" : "Mariano Ortega", + "id" : "Mariano Ortega" }, { + "data" : [ + [ + "Raku", + 2 + ] + ], "name" : "Mark Anderson", - "id" : "Mark Anderson", + "id" : "Mark Anderson" + }, + { "data" : [ + [ + "Perl", + 2 + ], [ "Raku", 2 + ], + [ + "Blog", + 1 ] - ] + ], + "name" : "Packy Anderson", + "id" : "Packy Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ], + "name" : "Thomas Kohler", + "id" : "Thomas Kohler" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan" } ] }, "subtitle" : { - "text" : "[Champions: 7] Last updated at 2024-08-05 19:01:15 GMT" + "text" : "[Champions: 13] Last updated at 2024-08-06 09:04:30 GMT" + }, + "tooltip" : { + "headerFormat" : "{series.name}
", + "followPointer" : 1, + "pointFormat" : "{point.name}: {point.y:f}
" + }, + "xAxis" : { + "type" : "category" + }, + "series" : [ + { + "name" : "The Weekly Challenge - 281", + "colorByPoint" : 1, + "data" : [ + { + "y" : 2, + "drilldown" : "Alexander Karelas", + "name" : "Alexander Karelas" + }, + { + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby", + "y" : 3 + }, + { + "y" : 2, + "drilldown" : "David Ferrone", + "name" : "David Ferrone" + }, + { + "drilldown" : "E. Choroba", + "name" : "E. Choroba", + "y" : 2 + }, + { + "y" : 2, + "name" : "Feng Chang", + "drilldown" : "Feng Chang" + }, + { + "y" : 2, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "drilldown" : "Kjetil Skotheim", + "name" : "Kjetil Skotheim", + "y" : 2 + }, + { + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "y" : 3 + }, + { + "drilldown" : "Mariano Ortega", + "name" : "Mariano Ortega", + "y" : 2 + }, + { + "y" : 2, + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson" + }, + { + "y" : 5, + "drilldown" : "Packy Anderson", + "name" : "Packy Anderson" + }, + { + "y" : 4, + "name" : "Thomas Kohler", + "drilldown" : "Thomas Kohler" + }, + { + "y" : 3, + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" + } + ] + } + ], + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } } } diff --git a/stats/pwc-language-breakdown-2019.json b/stats/pwc-language-breakdown-2019.json index c3d8544f7d..d68e38e212 100644 --- a/stats/pwc-language-breakdown-2019.json +++ b/stats/pwc-language-breakdown-2019.json @@ -1,14 +1,10 @@ { - "chart" : { - "type" : "column" - }, - "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2024-08-05 19:01:15 GMT" + "legend" : { + "enabled" : "false" }, "drilldown" : { "series" : [ { - "id" : "001", "data" : [ [ "Perl", @@ -23,7 +19,8 @@ 12 ] ], - "name" : "001" + "name" : "001", + "id" : "001" }, { "name" : "002", @@ -44,7 +41,6 @@ ] }, { - "name" : "003", "data" : [ [ "Perl", @@ -59,10 +55,10 @@ 9 ] ], + "name" : "003", "id" : "003" }, { - "name" : "004", "data" : [ [ "Perl", @@ -77,10 +73,10 @@ 10 ] ], + "name" : "004", "id" : "004" }, { - "id" : "005", "data" : [ [ "Perl", @@ -95,10 +91,10 @@ 12 ] ], + "id" : "005", "name" : "005" }, { - "id" : "006", "data" : [ [ "Perl", @@ -113,9 +109,12 @@ 7 ] ], + "id" : "006", "name" : "006" }, { + "id" : "007", + "name" : "007", "data" : [ [ "Perl", @@ -129,13 +128,9 @@ "Blog", 10 ] - ], - "id" : "007", - "name" : "007" + ] }, { - "name" : "008", - "id" : "008", "data" : [ [ "Perl", @@ -149,10 +144,11 @@ "Blog", 12 ] - ] + ], + "id" : "008", + "name" : "008" }, { - "name" : "009", "data" : [ [ "Perl", @@ -167,9 +163,11 @@ 13 ] ], + "name" : "009", "id" : "009" }, { + "id" : "010", "name" : "010", "data" : [ [ @@ -184,8 +182,7 @@ "Blog", 11 ] - ], - "id" : "010" + ] }, { "data" : [ @@ -224,7 +221,6 @@ "name" : "012" }, { - "name" : "013", "data" : [ [ "Perl", @@ -239,10 +235,10 @@ 13 ] ], + "name" : "013", "id" : "013" }, { - "name" : "014", "data" : [ [ "Perl", @@ -257,6 +253,7 @@ 15 ] ], + "name" : "014", "id" : "014" }, { @@ -274,12 +271,10 @@ 15 ] ], - "id" : "015", - "name" : "015" + "name" : "015", + "id" : "015" }, { - "name" : "016", - "id" : "016", "data" : [ [ "Perl", @@ -293,11 +288,11 @@ "Blog", 13 ] - ] + ], + "id" : "016", + "name" : "016" }, { - "name" : "017", - "id" : "017", "data" : [ [ "Perl", @@ -311,7 +306,9 @@ "Blog", 12 ] - ] + ], + "id" : "017", + "name" : "017" }, { "data" : [ @@ -328,12 +325,10 @@ 14 ] ], - "id" : "018", - "name" : "018" + "name" : "018", + "id" : "018" }, { - "name" : "019", - "id" : "019", "data" : [ [ "Perl", @@ -347,10 +342,11 @@ "Blog", 13 ] - ] + ], + "name" : "019", + "id" : "019" }, { - "id" : "020", "data" : [ [ "Perl", @@ -365,9 +361,11 @@ 13 ] ], + "id" : "020", "name" : "020" }, { + "id" : "021", "name" : "021", "data" : [ [ @@ -382,12 +380,11 @@ "Blog", 10 ] - ], - "id" : "021" + ] }, { - "name" : "022", "id" : "022", + "name" : "022", "data" : [ [ "Perl", @@ -404,7 +401,6 @@ ] }, { - "name" : "023", "data" : [ [ "Perl", @@ -419,9 +415,11 @@ 12 ] ], + "name" : "023", "id" : "023" }, { + "name" : "024", "id" : "024", "data" : [ [ @@ -436,12 +434,11 @@ "Blog", 11 ] - ], - "name" : "024" + ] }, { - "name" : "025", "id" : "025", + "name" : "025", "data" : [ [ "Perl", @@ -458,8 +455,8 @@ ] }, { - "name" : "026", "id" : "026", + "name" : "026", "data" : [ [ "Perl", @@ -476,8 +473,8 @@ ] }, { - "name" : "027", "id" : "027", + "name" : "027", "data" : [ [ "Perl", @@ -494,8 +491,8 @@ ] }, { - "name" : "028", "id" : "028", + "name" : "028", "data" : [ [ "Perl", @@ -512,8 +509,6 @@ ] }, { - "name" : "029", - "id" : "029", "data" : [ [ "Perl", @@ -527,9 +522,13 @@ "Blog", 12 ] - ] + ], + "id" : "029", + "name" : "029" }, { + "name" : "030", + "id" : "030", "data" : [ [ "Perl", @@ -543,13 +542,9 @@ "Blog", 10 ] - ], - "id" : "030", - "name" : "030" + ] }, { - "name" : "031", - "id" : "031", "data" : [ [ "Perl", @@ -563,9 +558,12 @@ "Blog", 9 ] - ] + ], + "name" : "031", + "id" : "031" }, { + "name" : "032", "id" : "032", "data" : [ [ @@ -580,8 +578,7 @@ "Blog", 10 ] - ], - "name" : "032" + ] }, { "name" : "033", @@ -602,8 +599,6 @@ ] }, { - "name" : "034", - "id" : "034", "data" : [ [ "Perl", @@ -617,10 +612,11 @@ "Blog", 11 ] - ] + ], + "name" : "034", + "id" : "034" }, { - "id" : "035", "data" : [ [ "Perl", @@ -635,10 +631,10 @@ 9 ] ], - "name" : "035" + "name" : "035", + "id" : "035" }, { - "name" : "036", "data" : [ [ "Perl", @@ -653,10 +649,10 @@ 11 ] ], + "name" : "036", "id" : "036" }, { - "name" : "037", "data" : [ [ "Perl", @@ -671,9 +667,11 @@ 9 ] ], - "id" : "037" + "id" : "037", + "name" : "037" }, { + "name" : "038", "id" : "038", "data" : [ [ @@ -688,12 +686,9 @@ "Blog", 12 ] - ], - "name" : "038" + ]