diff options
| author | Packy Anderson <packy@cpan.org> | 2024-08-05 23:00:40 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-08-05 23:00:40 -0400 |
| commit | 4b08f96f7df8de14e0c77b7fabbde2ade95718d2 (patch) | |
| tree | 1b6d14e8e1b4c4a1fa6f4935c99c4da66249ba9b /challenge-281/packy-anderson/python | |
| parent | d4691a9d293de4cf2f42e44cdefabb0b455045fb (diff) | |
| download | perlweeklychallenge-club-4b08f96f7df8de14e0c77b7fabbde2ade95718d2.tar.gz perlweeklychallenge-club-4b08f96f7df8de14e0c77b7fabbde2ade95718d2.tar.bz2 perlweeklychallenge-club-4b08f96f7df8de14e0c77b7fabbde2ade95718d2.zip | |
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
Diffstat (limited to 'challenge-281/packy-anderson/python')
| -rwxr-xr-x | challenge-281/packy-anderson/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-281/packy-anderson/python/ch-2.py | 77 |
2 files changed, 100 insertions, 0 deletions
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') |
