From f6a8d5af7bb92ef0d021fe409b466c9ed25bb0dd Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 12:42:22 +0200 Subject: README for week 118 --- challenge-118/abigail/README.md | 88 +++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 51 deletions(-) diff --git a/challenge-118/abigail/README.md b/challenge-118/abigail/README.md index 7db94b7f39..05677d379c 100644 --- a/challenge-118/abigail/README.md +++ b/challenge-118/abigail/README.md @@ -1,27 +1,20 @@ # Solutions by Abigail -## [Missing Row](https://perlweeklychallenge.org/blog/perl-weekly-challenge-117/#TASK1) +## [Binary Palindrome](https://perlweeklychallenge.org/blog/perl-weekly-challenge-118/#TASK1) -> You are given text file with rows numbered `1-15` in random order but -> there is a catch one row in missing in the file. +> You are given a positive integer $N. +> +> Write a script to find out if the binary representation of the given +> integer is Palindrome. Print 1 if it is otherwise 0. +> +### Examples ~~~~ -11, Line Eleven -1, Line one -9, Line Nine -13, Line Thirteen -2, Line two -6, Line Six -8, Line Eight -10, Line Ten -7, Line Seven -4, Line Four -14, Line Fourteen -3, Line three -15, Line Fifteen -5, Line Five -~~~~ +Input: $N = 5 +Output: 1 as binary representation of 5 is 101 which is Palindrome. -Write a script to find the missing row number. +Input: $N = 4 +Output: 0 as binary representation of 4 is 100 which is NOT Palindrome. +~~~~ ### Solutions * [AWK](awk/ch-1.awk) @@ -36,41 +29,34 @@ Write a script to find the missing row number. * [Ruby](ruby/ch-1.rb) ### Blog -[Missing Row](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-117-1.html) +[Binary Palindrome](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-118-1.html) -## [Find Possible Paths](https://perlweeklychallenge.org/blog/perl-weekly-challenge-115/#TASK2) +## [Adventure of Knight](https://perlweeklychallenge.org/blog/perl-weekly-challenge-118/#TASK2) -> You are given size of a triangle. -> -> Write a script to find all possible paths from top to the bottom -> right corner. -> -> In each step, we can either move horizontally to the right (`H`), or -> move downwards to the left (`L`) or right (`R`). +> A knight is restricted to move on an 8x8 chessboard. The knight is denoted +> by `N` and its way of movement is the same as what it is defined in Chess. +> `*` represents an empty square. `x` represents a square with treasure. +> +> > The Knight's movement is unique. It may move two squares vertically and +> > one square horizontally, or two squares horizontally and one square +> > vertically (with both forming the shape of an L). +> +> There are 6 squares with treasures. +> +> Write a script to find the path such that Knight can capture all +> treasures. The Knight can start from the top-left square. > -> BONUS: Try if it can handle triangle of size `10` or `20`. - -### Examples -~~~~ -Input: $N = 2 - - S - / \ - / _ \ - /\ /\ - /__\ /__\ E - -Output: RR, LHR, LHLH, LLHH, RLH, LRH -~~~~ - ~~~~ -Input: $N = 1 - - S - / \ - / _ \ E - -Output: R, LH + a b c d e f g h + 8 N * * * * * * * 8 + 7 * * * * * * * * 7 + 6 * * * * x * * * 6 + 5 * * * * * * * * 5 + 4 * * x * * * * * 4 + 3 * x * * * * * * 3 + 2 x x * * * * * * 2 + 1 * x * * * * * * 1 + a b c d e f g h ~~~~ ### Solutions @@ -86,4 +72,4 @@ Output: R, LH * [Ruby](ruby/ch-2.rb) ### Blog -[Find Possible Paths](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-117-2.html) +[Adventure of Knight](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-118-2.html) -- cgit From eff92d47c584ac99c453a0f5572d6bc0a2843da3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 13:20:20 +0200 Subject: Perl solution for week 118, part 1 --- challenge-118/abigail/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-118/abigail/perl/ch-1.pl diff --git a/challenge-118/abigail/perl/ch-1.pl b/challenge-118/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..99df6f0f65 --- /dev/null +++ b/challenge-118/abigail/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# More efficient is to store the binary representation of the number +# in a variable, and see whether it's the same as its reverse, but +# using a pattern turns it into a one liner. +# + +say sprintf ("%b" => $_) =~ /^(([01])(?1)\g{2}|[01]?)$/ || 0 while <>; -- cgit From fb8b828cd7c83894fed534d9d1894981a0dce203 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 14:12:53 +0200 Subject: AWK solution for week 118, part 1 --- challenge-118/abigail/awk/ch-1.awk | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 challenge-118/abigail/awk/ch-1.awk diff --git a/challenge-118/abigail/awk/ch-1.awk b/challenge-118/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..490a5ceefc --- /dev/null +++ b/challenge-118/abigail/awk/ch-1.awk @@ -0,0 +1,36 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +# +# Get a binary representation +# +function dec2bin (dec, bin) { + while (dec) { + bin = dec % 2 bin + dec = int (dec / 2) + } + return (bin) +} + +{ + bin = dec2bin($1) + l = length (bin) + # + # Check if it's a palindrome + # + for (i = 1; i < l / 2; i ++) { + if (substr (bin, i, 1) != substr (bin, l - i + 1, 1)) { + print (0) + next + } + } + print (1) +} + -- cgit From 5ab7990c1199c721345fe3f9beb52634862f8396 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 14:56:54 +0200 Subject: Bash solution for week 118, part 1 --- challenge-118/abigail/bash/ch-1.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-118/abigail/bash/ch-1.sh diff --git a/challenge-118/abigail/bash/ch-1.sh b/challenge-118/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..873decd636 --- /dev/null +++ b/challenge-118/abigail/bash/ch-1.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +function dec2bin () { + dec=$1 + bin="" + while ((dec > 0)) + do bin=$((dec % 2))$bin + ((dec /= 2)) + done +} + +while read dec +do dec2bin $dec + for ((i = 0; i < ${#bin} / 2; i ++)) + do if [ "${bin:$i:1}" = "${bin:$((${#bin} - i - 1)):1}" ] + then continue + fi + echo 0 + continue 2 + done + echo 1 +done -- cgit From 8f7b0abc3ed0410970c5944e912f449a44f14a80 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 15:25:11 +0200 Subject: C solution for week 118, part 1 --- challenge-118/abigail/c/ch-1.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 challenge-118/abigail/c/ch-1.c diff --git a/challenge-118/abigail/c/ch-1.c b/challenge-118/abigail/c/ch-1.c new file mode 100644 index 0000000000..637c938d0e --- /dev/null +++ b/challenge-118/abigail/c/ch-1.c @@ -0,0 +1,42 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + + +int main (void) { + long long dec; + while (scanf ("%lld", &dec) == 1) { + /* + * Find the largest power of 2 smaller than dec; + * this is the position of the leading 1. + */ + long long i = 1; + int k = 0; + for (k = 0; i <= dec; k ++, i = i << 1); + /* + * We overshot by 1 + */ + k -= 1; + + /* + * Now, compare the bits + */ + int is_palin = 1; + for (int j = 0; j < k; k --, j ++) { + if (((dec & (1 << j)) >> j) != ((dec & (1 << k)) >> k)) { + is_palin = 0; + break; + } + } + printf ("%d\n", is_palin); + } + return (0); +} -- cgit From 7ac27fdb94896a8250018ff135e72f9a99071f54 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 18:48:27 +0200 Subject: Lua solution for week 118, part 1 --- challenge-118/abigail/lua/ch-1.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-118/abigail/lua/ch-1.lua diff --git a/challenge-118/abigail/lua/ch-1.lua b/challenge-118/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..a129db19e4 --- /dev/null +++ b/challenge-118/abigail/lua/ch-1.lua @@ -0,0 +1,32 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +-- +-- Find the binary representation of a number. +-- Note that the function return a string in reverse order; +-- this will do for our purpose as we want a palindrome anyway. +-- +function dec2bin (dec) + local bin = {} + while dec > 0 do + bin [#bin + 1] = dec % 2 + dec = math . floor (dec / 2) + end + return table . concat (bin) +end + +for line in io . lines () do + bin = dec2bin (tonumber (line)) + if bin == string . reverse (bin) then + print (1) + else + print (0) + end +end -- cgit From 7160801add8cbeff393124515ad2846da6265a69 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 19:02:10 +0200 Subject: Node.js solution for week 118, part 1 --- challenge-118/abigail/node/ch-1.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-118/abigail/node/ch-1.js diff --git a/challenge-118/abigail/node/ch-1.js b/challenge-118/abigail/node/ch-1.js new file mode 100644 index 0000000000..7247cb5eef --- /dev/null +++ b/challenge-118/abigail/node/ch-1.js @@ -0,0 +1,16 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', (line) => { + let bin = (+line) . toString (2) + console . log (bin == bin . split ("") . reverse () . join ("") ? 1 : 0) +}) -- cgit From f245cfefa46f122166a0034aa82507ea2b84ee5e Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 19:29:32 +0200 Subject: Python solution for week 118, part 1 --- challenge-118/abigail/python/ch-1.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-118/abigail/python/ch-1.py diff --git a/challenge-118/abigail/python/ch-1.py b/challenge-118/abigail/python/ch-1.py new file mode 100644 index 0000000000..df0451a44e --- /dev/null +++ b/challenge-118/abigail/python/ch-1.py @@ -0,0 +1,19 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + bin = '{:b}' . format (int (line)) # Turn to binary representation + if bin == bin [::-1]: # bin [::-1] reverse the string + print (1) + else: + print (0) + -- cgit From 1b31356bcb86b22fa81bd86f0a4345cf0eb9e784 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 19:42:25 +0200 Subject: Ruby solution for week 118, part 1 --- challenge-118/abigail/ruby/ch-1.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-118/abigail/ruby/ch-1.rb diff --git a/challenge-118/abigail/ruby/ch-1.rb b/challenge-118/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..2609253a2d --- /dev/null +++ b/challenge-118/abigail/ruby/ch-1.rb @@ -0,0 +1,15 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + |dec| + bin = "%b" % dec + puts (bin == bin . reverse ? 1 : 0) +end -- cgit From d37b3bd215636fd3dbac1935a6cfd4dd42a1d7cb Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 20:24:14 +0200 Subject: Go solution for week 118, part 1 --- challenge-118/abigail/go/ch-1.go | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-118/abigail/go/ch-1.go diff --git a/challenge-118/abigail/go/ch-1.go b/challenge-118/abigail/go/ch-1.go new file mode 100644 index 0000000000..466a5e574f --- /dev/null +++ b/challenge-118/abigail/go/ch-1.go @@ -0,0 +1,41 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "strconv" +) + +func reverse (str string) string { + rev := [] rune (str) + for i, j := 0, len (rev) - 1; i < j; i, j = i + 1, j - 1 { + rev [i], rev [j] = rev [j], rev [i] + } + return string (rev) +} + +func main () { + var dec int64 + for { + var n, err = fmt . Scanf ("%d", &dec) + if (n != 1 || err != nil) { + break + } + var bin = strconv . FormatInt (dec, 2) + if (bin == reverse (bin)) { + fmt . Println (1) + } else { + fmt . Println (0) + } + } +} + + + -- cgit From c96706ce3310f524797452e334c84bbff90a9a5d Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 21 Jun 2021 22:02:41 +0200 Subject: Java solution for week 118, part 1 --- challenge-118/abigail/java/ch-1.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-118/abigail/java/ch-1.java diff --git a/challenge-118/abigail/java/ch-1.java b/challenge-118/abigail/java/ch-1.java new file mode 100644 index 0000000000..a806d2c237 --- /dev/null +++ b/challenge-118/abigail/java/ch-1.java @@ -0,0 +1,34 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + try { + while (true) { + int dec = scanner . nextInt (); + String bin = Integer . toBinaryString (dec); + + if (bin . equals (new StringBuilder (bin) . + reverse () . toString ())) { + System . out . println (1); + } + else { + System . out . println (0); + } + } + } + catch (Exception e) { + // + // EOF + // + } + } +} -- cgit From 0b3b62e85d589d3389982c7eefb6865459c93934 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 18:31:06 +0200 Subject: Perl solution for week 118, part 2 --- challenge-118/abigail/perl/ch-2.pl | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-118/abigail/perl/ch-2.pl diff --git a/challenge-118/abigail/perl/ch-2.pl b/challenge-118/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..e0dafb03f0 --- /dev/null +++ b/challenge-118/abigail/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl +# +# First thing which spring to mind is: there is a closed knight's tour +# on a chess board. In fact, there are 19,591,828,170,979,904 of them +# (counting rotations and reflections, but not direction). See A165134 +# in the OEIS. (And there are also billions of open tours visiting all +# squares...) +# +# And any knight's tour will visit all squares, including the ones with +# treasure. So, we could just pick a tour visiting all squares and +# calling it a day. This would turn the challenge into a glorified +# Hello World! program. +# +# So, perhaps we should focus on the bonus part. +# +# But wait, there's no variable input. There's just a fixed board +# to work with. And one can quickly see there's an 11 step path +# visiting all the treasure, and no 10 step path. +# +# So, this challenge is yet another boring Hello, World! program. +# After all, it's much easier to write down the 11 knights moves than +# to write a program. +# +# To up the ante, we'll write a second program (ch-2a.pl) which actually +# reads input and does calculations. But the main solution will just print +# out the 11-move path. +# +# I really do hope the PWC steps away from fixed output challenges. +# + +say "c7 e6 c5 b3 c1 a2 c3 b1 d2 c4 b2"; + +__END__ -- cgit From cea0a95b6889a97885e46c6147faf21a4888af25 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 18:31:47 +0200 Subject: Upping the ante for week 118, part 2 (Perl) --- challenge-118/abigail/perl/ch-2a.pl | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 challenge-118/abigail/perl/ch-2a.pl diff --git a/challenge-118/abigail/perl/ch-2a.pl b/challenge-118/abigail/perl/ch-2a.pl new file mode 100644 index 0000000000..f2bbef804c --- /dev/null +++ b/challenge-118/abigail/perl/ch-2a.pl @@ -0,0 +1,102 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2a.pl < input-file +# +# Second solution; this time we actually read a challenge from +# STDIN. Each line of input stands for another challenge, +# Each line consists of 2 or more chess squares; the first is +# the starting point of the knight, the rest treasures to be visited. +# + +my $MAX_FILE = 8; +my $MAX_RANK = 8; + +# +# Given a file and rank as numbers, return the algebraic name of a square +# +sub c2a ($file, $rank) { + sprintf "%c%d", ord ('a') + $file - 1, $rank +} + +# +# Precompute all knight moves on the board. +# +my %knight_moves; # Maps square to all squares reachable from it. +for my $file (1 .. $MAX_FILE) { + for my $rank (1 .. $MAX_RANK) { + my $square = c2a $file, $rank; + # + # Consider only moves in one direction; we add reverse moves as well. + # Knight moves 2 in one direction, and 1 orthogonally from that. + # + for my $move ([1, 2], [1, -2], [2, 1], [2, -1]) { + next unless 1 <= $file + $$move [0] <= $MAX_FILE && + 1 <= $rank + $$move [1] <= $MAX_RANK; + my $target = c2a $file + $$move [0], $rank + $$move [1]; + push @{$knight_moves {$square}} => $target; + push @{$knight_moves {$target}} => $square; + } + } +} + + + +# +# Given a starting square, and a set of treasure locations, do a +# breath first search to find a shortest path visiting all the +# treasure locations. +# +# Note that we do allow to revisit squares, but only if they have a +# different set of visited "treasure" squares. It is after all possible +# that the shortest path revisits squares. +# +sub find_treasure ($start_square, @treasure) { + my %treasure = map {$_ => 1} @treasure; + + my %visited; # Maps square x seen treasure to 1 + + my @todo = ([[$start_square], {}]); + # Entries in the @todo list are tuples: + # - The path that got us here; the last element being + # the "current" square + # - A mapping of which treasure squares we have visited + # on the path. + + while (@todo) { + my ($path, $old_seen) = @{shift @todo}; + my $square = $$path [-1]; + my $seen = {%$old_seen}; + $$seen {$square} = 1 if $treasure {$square}; + + my $key = join " " => $square, sort keys %$seen; + next if $visited {$key} ++; + + if (keys %$seen == keys %treasure) { + # + # We have seen all the treasure: return the path. + # + return $path; + } + + push @todo => map {[[@$path => $_], $seen]} @{$knight_moves {$square}}; + } +} + + +say "@{find_treasure split}" while <>; + +__END__ -- cgit From ef87285d2c8d743bf79a781d1a4314d4c30d12a3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 19:57:37 +0200 Subject: Print starting square --- challenge-118/abigail/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-118/abigail/perl/ch-2.pl b/challenge-118/abigail/perl/ch-2.pl index e0dafb03f0..3a28598fac 100644 --- a/challenge-118/abigail/perl/ch-2.pl +++ b/challenge-118/abigail/perl/ch-2.pl @@ -44,6 +44,6 @@ use experimental 'lexical_subs'; # I really do hope the PWC steps away from fixed output challenges. # -say "c7 e6 c5 b3 c1 a2 c3 b1 d2 c4 b2"; +say "a8 c7 e6 c5 b3 c1 a2 c3 b1 d2 c4 b2"; __END__ -- cgit From d2907c9dfe40252ea7e11c7c822f06ea2bc793bf Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 19:57:51 +0200 Subject: Tests for week 118 --- challenge-118/abigail/t/ctest.ini | 7 +++++++ challenge-118/abigail/t/input-1-1 | 2 ++ challenge-118/abigail/t/input-2-1 | 1 + challenge-118/abigail/t/output-1-1.exp | 2 ++ challenge-118/abigail/t/output-2-1.exp | 1 + 5 files changed, 13 insertions(+) create mode 100644 challenge-118/abigail/t/ctest.ini create mode 100644 challenge-118/abigail/t/input-1-1 create mode 100644 challenge-118/abigail/t/input-2-1 create mode 100644 challenge-118/abigail/t/output-1-1.exp create mode 100644 challenge-118/abigail/t/output-2-1.exp diff --git a/challenge-118/abigail/t/ctest.ini b/challenge-118/abigail/t/ctest.ini new file mode 100644 index 0000000000..d1a3f6baf6 --- /dev/null +++ b/challenge-118/abigail/t/ctest.ini @@ -0,0 +1,7 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples diff --git a/challenge-118/abigail/t/input-1-1 b/challenge-118/abigail/t/input-1-1 new file mode 100644 index 0000000000..b5104f3ba9 --- /dev/null +++ b/challenge-118/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +5 +4 diff --git a/challenge-118/abigail/t/input-2-1 b/challenge-118/abigail/t/input-2-1 new file mode 100644 index 0000000000..376243accc --- /dev/null +++ b/challenge-118/abigail/t/input-2-1 @@ -0,0 +1 @@ +a8 b1 a2 b2 b3 c4 e6 diff --git a/challenge-118/abigail/t/output-1-1.exp b/challenge-118/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..b261da18d5 --- /dev/null +++ b/challenge-118/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +1 +0 diff --git a/challenge-118/abigail/t/output-2-1.exp b/challenge-118/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..47e497232b --- /dev/null +++ b/challenge-118/abigail/t/output-2-1.exp @@ -0,0 +1 @@ +a8 c7 e6 c5 b3 c1 a2 c3 b1 d2 c4 b2 -- cgit From f23ea63093e2ae3a4cba10e54b374d9b46f9b764 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 20:04:40 +0200 Subject: Move via a3 instead of d2 --- challenge-118/abigail/perl/ch-2.pl | 2 +- challenge-118/abigail/t/output-2-1.exp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-118/abigail/perl/ch-2.pl b/challenge-118/abigail/perl/ch-2.pl index 3a28598fac..2d224fd1fc 100644 --- a/challenge-118/abigail/perl/ch-2.pl +++ b/challenge-118/abigail/perl/ch-2.pl @@ -44,6 +44,6 @@ use experimental 'lexical_subs'; # I really do hope the PWC steps away from fixed output challenges. # -say "a8 c7 e6 c5 b3 c1 a2 c3 b1 d2 c4 b2"; +say "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2"; __END__ diff --git a/challenge-118/abigail/t/output-2-1.exp b/challenge-118/abigail/t/output-2-1.exp index 47e497232b..f0803658c1 100644 --- a/challenge-118/abigail/t/output-2-1.exp +++ b/challenge-118/abigail/t/output-2-1.exp @@ -1 +1 @@ -a8 c7 e6 c5 b3 c1 a2 c3 b1 d2 c4 b2 +a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2 -- cgit From 9c47a695c2f01a7fb23e1647c40b5c31ade33c2f Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 20:12:51 +0200 Subject: Solutions for 29 languages. Since we have a gloried Hello, World! challenge.... --- challenge-118/abigail/awk/ch-2.awk | 14 ++++++++++++++ challenge-118/abigail/bash/ch-2.sh | 11 +++++++++++ challenge-118/abigail/basic/ch-2.bas | 9 +++++++++ challenge-118/abigail/bc/ch-2.bc | 10 ++++++++++ challenge-118/abigail/befunge-93/ch-2.bf93 | 2 ++ challenge-118/abigail/c/ch-2.c | 16 ++++++++++++++++ challenge-118/abigail/cobol/ch-2.cb | 14 ++++++++++++++ challenge-118/abigail/csh/ch-2.csh | 11 +++++++++++ challenge-118/abigail/erlang/ch-2.erl | 15 +++++++++++++++ challenge-118/abigail/forth/ch-2.fs | 5 +++++ challenge-118/abigail/fortran/ch-2.f90 | 12 ++++++++++++ challenge-118/abigail/go/ch-2.go | 15 +++++++++++++++ challenge-118/abigail/java/ch-2.java | 13 +++++++++++++ challenge-118/abigail/lua/ch-2.lua | 11 +++++++++++ challenge-118/abigail/m4/ch-2.m4 | 1 + challenge-118/abigail/mmix/ch-2.mms | 16 ++++++++++++++++ challenge-118/abigail/node/ch-2.js | 11 +++++++++++ challenge-118/abigail/ocaml/ch-2.ml | 9 +++++++++ challenge-118/abigail/pascal/ch-2.p | 13 +++++++++++++ challenge-118/abigail/php/ch-2.php | 11 +++++++++++ challenge-118/abigail/postscript/ch-2.ps | 10 ++++++++++ challenge-118/abigail/python/ch-2.py | 11 +++++++++++ challenge-118/abigail/r/ch-2.r | 9 +++++++++ challenge-118/abigail/rexx/ch-2.rexx | 9 +++++++++ challenge-118/abigail/ruby/ch-2.rb | 11 +++++++++++ challenge-118/abigail/scheme/ch-2.scm | 9 +++++++++ challenge-118/abigail/sed/ch-2.sed | 12 ++++++++++++ challenge-118/abigail/sql/ch-2.sql | 9 +++++++++ challenge-118/abigail/tcl/ch-2.tcl | 9 +++++++++ 29 files changed, 308 insertions(+) create mode 100644 challenge-118/abigail/awk/ch-2.awk create mode 100644 challenge-118/abigail/bash/ch-2.sh create mode 100644 challenge-118/abigail/basic/ch-2.bas create mode 100644 challenge-118/abigail/bc/ch-2.bc create mode 100644 challenge-118/abigail/befunge-93/ch-2.bf93 create mode 100644 challenge-118/abigail/c/ch-2.c create mode 100644 challenge-118/abigail/cobol/ch-2.cb create mode 100644 challenge-118/abigail/csh/ch-2.csh create mode 100644 challenge-118/abigail/erlang/ch-2.erl create mode 100644 challenge-118/abigail/forth/ch-2.fs create mode 100644 challenge-118/abigail/fortran/ch-2.f90 create mode 100644 challenge-118/abigail/go/ch-2.go create mode 100644 challenge-118/abigail/java/ch-2.java create mode 100644 challenge-118/abigail/lua/ch-2.lua create mode 100644 challenge-118/abigail/m4/ch-2.m4 create mode 100644 challenge-118/abigail/mmix/ch-2.mms create mode 100644 challenge-118/abigail/node/ch-2.js create mode 100644 challenge-118/abigail/ocaml/ch-2.ml create mode 100644 challenge-118/abigail/pascal/ch-2.p create mode 100644 challenge-118/abigail/php/ch-2.php create mode 100644 challenge-118/abigail/postscript/ch-2.ps create mode 100644 challenge-118/abigail/python/ch-2.py create mode 100644 challenge-118/abigail/r/ch-2.r create mode 100644 challenge-118/abigail/rexx/ch-2.rexx create mode 100644 challenge-118/abigail/ruby/ch-2.rb create mode 100644 challenge-118/abigail/scheme/ch-2.scm create mode 100644 challenge-118/abigail/sed/ch-2.sed create mode 100644 challenge-118/abigail/sql/ch-2.sql create mode 100644 challenge-118/abigail/tcl/ch-2.tcl diff --git a/challenge-118/abigail/awk/ch-2.awk b/challenge-118/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..d4cf905f2e --- /dev/null +++ b/challenge-118/abigail/awk/ch-2.awk @@ -0,0 +1,14 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk +# + +BEGIN { + print "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" +} + diff --git a/challenge-118/abigail/bash/ch-2.sh b/challenge-118/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..e48430410e --- /dev/null +++ b/challenge-118/abigail/bash/ch-2.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh +# + +echo "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/basic/ch-2.bas b/challenge-118/abigail/basic/ch-2.bas new file mode 100644 index 0000000000..8e07ba8840 --- /dev/null +++ b/challenge-118/abigail/basic/ch-2.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic ch-2.bas +060 REM + +100 PRINT "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/bc/ch-2.bc b/challenge-118/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..5125d6a827 --- /dev/null +++ b/challenge-118/abigail/bc/ch-2.bc @@ -0,0 +1,10 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc +# +"a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2 +" +quit diff --git a/challenge-118/abigail/befunge-93/ch-2.bf93 b/challenge-118/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..b31e3efee9 --- /dev/null +++ b/challenge-118/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,2 @@ +< v,_@#:< "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" +55 + > ^ diff --git a/challenge-118/abigail/c/ch-2.c b/challenge-118/abigail/c/ch-2.c new file mode 100644 index 0000000000..74531ee18d --- /dev/null +++ b/challenge-118/abigail/c/ch-2.c @@ -0,0 +1,16 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o + */ + +int main (void) { + printf ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n"); + exit (0); +} diff --git a/challenge-118/abigail/cobol/ch-2.cb b/challenge-118/abigail/cobol/ch-2.cb new file mode 100644 index 0000000000..a756c0f8d2 --- /dev/null +++ b/challenge-118/abigail/cobol/ch-2.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. XXX. + +*> +*> See ../README.md +*> + +*> +*> Run as: cobc -xF -o ch-2.o ch-2.cb; ./ch-2.o +*> + +PROCEDURE DIVISION. + DISPLAY "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2". + STOP RUN. diff --git a/challenge-118/abigail/csh/ch-2.csh b/challenge-118/abigail/csh/ch-2.csh new file mode 100644 index 0000000000..bdcc9cee6d --- /dev/null +++ b/challenge-118/abigail/csh/ch-2.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See ../README.md +# + +# +# Run as: csh ch-2.csh +# + +echo "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/erlang/ch-2.erl b/challenge-118/abigail/erlang/ch-2.erl new file mode 100644 index 0000000000..8357a62e7d --- /dev/null +++ b/challenge-118/abigail/erlang/ch-2.erl @@ -0,0 +1,15 @@ +% +% See ../README.md +% + +% +% Run as: ln ch-2.erl ch2.erl +% erl -compile ch2 +% erl -noshell -s ch2 main -s init stop +% + +-module (ch2). +-export ([main/0]). + +main () -> + io:fwrite ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n"). diff --git a/challenge-118/abigail/forth/ch-2.fs b/challenge-118/abigail/forth/ch-2.fs new file mode 100644 index 0000000000..adf08a7137 --- /dev/null +++ b/challenge-118/abigail/forth/ch-2.fs @@ -0,0 +1,5 @@ +\ +\ See ../README.md +\ + +.( a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2) diff --git a/challenge-118/abigail/fortran/ch-2.f90 b/challenge-118/abigail/fortran/ch-2.f90 new file mode 100644 index 0000000000..bb8c2851b0 --- /dev/null +++ b/challenge-118/abigail/fortran/ch-2.f90 @@ -0,0 +1,12 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-2.o ch-2.f90; ./ch-2.o +! + +program ch2 + implicit none + write (*, *) "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" +end diff --git a/challenge-118/abigail/go/ch-2.go b/challenge-118/abigail/go/ch-2.go new file mode 100644 index 0000000000..6fd5616aec --- /dev/null +++ b/challenge-118/abigail/go/ch-2.go @@ -0,0 +1,15 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go +// + +import "fmt" + +func main () { + fmt . Print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n") +} diff --git a/challenge-118/abigail/java/ch-2.java b/challenge-118/abigail/java/ch-2.java new file mode 100644 index 0000000000..d9f0c4edcc --- /dev/null +++ b/challenge-118/abigail/java/ch-2.java @@ -0,0 +1,13 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2; java ch2 +// + +public class ch2 { + public static void main (String [] args) { + System . out . print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n"); + } +} diff --git a/challenge-118/abigail/lua/ch-2.lua b/challenge-118/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..5c40dfc99c --- /dev/null +++ b/challenge-118/abigail/lua/ch-2.lua @@ -0,0 +1,11 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua +-- + +print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2") diff --git a/challenge-118/abigail/m4/ch-2.m4 b/challenge-118/abigail/m4/ch-2.m4 new file mode 100644 index 0000000000..6ebcee6dd2 --- /dev/null +++ b/challenge-118/abigail/m4/ch-2.m4 @@ -0,0 +1 @@ +a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2 diff --git a/challenge-118/abigail/mmix/ch-2.mms b/challenge-118/abigail/mmix/ch-2.mms new file mode 100644 index 0000000000..416ce1c81e --- /dev/null +++ b/challenge-118/abigail/mmix/ch-2.mms @@ -0,0 +1,16 @@ +% +% See ../README.md +% + +% +% Run as: mmixal -o ch-2.mmo ch-2.mms; mmix -q ch-2.mmo +% + LOC Data_Segment + GREG @ +Text BYTE "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2",10,0 + + LOC #100 + +Main LDA $255,Text + TRAP 0,Fputs,StdOut + TRAP 0,Halt,0 diff --git a/challenge-118/abigail/node/ch-2.js b/challenge-118/abigail/node/ch-2.js new file mode 100644 index 0000000000..e032ef0448 --- /dev/null +++ b/challenge-118/abigail/node/ch-2.js @@ -0,0 +1,11 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js +// + +console . log ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2") diff --git a/challenge-118/abigail/ocaml/ch-2.ml b/challenge-118/abigail/ocaml/ch-2.ml new file mode 100644 index 0000000000..553e3af70f --- /dev/null +++ b/challenge-118/abigail/ocaml/ch-2.ml @@ -0,0 +1,9 @@ +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: ocaml ch-2.ml *) +(* *) + +print_endline "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2"; diff --git a/challenge-118/abigail/pascal/ch-2.p b/challenge-118/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..e95a839a4b --- /dev/null +++ b/challenge-118/abigail/pascal/ch-2.p @@ -0,0 +1,13 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-2.p; ./ch-2.out *) +(* *) + +begin + writeln ('a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2') +end. diff --git a/challenge-118/abigail/php/ch-2.php b/challenge-118/abigail/php/ch-2.php new file mode 100644 index 0000000000..46cfeff6a7 --- /dev/null +++ b/challenge-118/abigail/php/ch-2.php @@ -0,0 +1,11 @@ + diff --git a/challenge-118/abigail/postscript/ch-2.ps b/challenge-118/abigail/postscript/ch-2.ps new file mode 100644 index 0000000000..97a3a77cd3 --- /dev/null +++ b/challenge-118/abigail/postscript/ch-2.ps @@ -0,0 +1,10 @@ +%!PS +% +% See ../README.md +% + +% +% Run as: ps2ascii ch-2.ps +% + +(a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2) = diff --git a/challenge-118/abigail/python/ch-2.py b/challenge-118/abigail/python/ch-2.py new file mode 100644 index 0000000000..12dd966554 --- /dev/null +++ b/challenge-118/abigail/python/ch-2.py @@ -0,0 +1,11 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py +# + +print ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2") diff --git a/challenge-118/abigail/r/ch-2.r b/challenge-118/abigail/r/ch-2.r new file mode 100644 index 0000000000..b021f84006 --- /dev/null +++ b/challenge-118/abigail/r/ch-2.r @@ -0,0 +1,9 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-2.r +# + +cat ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n") diff --git a/challenge-118/abigail/rexx/ch-2.rexx b/challenge-118/abigail/rexx/ch-2.rexx new file mode 100644 index 0000000000..f77556e087 --- /dev/null +++ b/challenge-118/abigail/rexx/ch-2.rexx @@ -0,0 +1,9 @@ +/* + * See ../README.md + */ + +/* + * Run as: rexx ch-2.rexx + */ + +say "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" diff --git a/challenge-118/abigail/ruby/ch-2.rb b/challenge-118/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..0a575319f9 --- /dev/null +++ b/challenge-118/abigail/ruby/ch-2.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb +# + +puts ("a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2"); diff --git a/challenge-118/abigail/scheme/ch-2.scm b/challenge-118/abigail/scheme/ch-2.scm new file mode 100644 index 0000000000..def3bfb2b5 --- /dev/null +++ b/challenge-118/abigail/scheme/ch-2.scm @@ -0,0 +1,9 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-2.scm +;;; + +(display "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2\n") diff --git a/challenge-118/abigail/sed/ch-2.sed b/challenge-118/abigail/sed/ch-2.sed new file mode 100644 index 0000000000..e7d2004aa5 --- /dev/null +++ b/challenge-118/abigail/sed/ch-2.sed @@ -0,0 +1,12 @@ +# +# See ../README.md +# + +# +# Run as: sed -f ch-2.sed +# +# For each line in the input file, we write the first three +# self-describing numbers. +# + +s/.*/a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2/ diff --git a/challenge-118/abigail/sql/ch-2.sql b/challenge-118/abigail/sql/ch-2.sql new file mode 100644 index 0000000000..5ff02b7df8 --- /dev/null +++ b/challenge-118/abigail/sql/ch-2.sql @@ -0,0 +1,9 @@ +-- +-- See ../README.md +-- + +-- +-- Run as: sqlite3 < ch-2.sql +-- + +SELECT "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2"; diff --git a/challenge-118/abigail/tcl/ch-2.tcl b/challenge-118/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..f1e2f7b109 --- /dev/null +++ b/challenge-118/abigail/tcl/ch-2.tcl @@ -0,0 +1,9 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-2.tcl +# + +puts "a8 c7 e6 c5 b3 c1 a2 c3 b1 a3 c4 b2" -- cgit From ed65f9e4f5f0e1200acedd279bcb6e52b4cecb2b Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 22 Jun 2021 20:14:46 +0200 Subject: Don't give the SQL solution input --- challenge-118/abigail/t/ctest.ini | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/challenge-118/abigail/t/ctest.ini b/challenge-118/abigail/t/ctest.ini index d1a3f6baf6..261644e4f1 100644 --- a/challenge-118/abigail/t/ctest.ini +++ b/challenge-118/abigail/t/ctest.ini @@ -5,3 +5,7 @@ [names] 1-1 = Given Examples +2-1 = Given Example + +[2-1/sql] +no_input = 1 -- cgit From cf638c1ad15fad8e756291ea904de1547d286592 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:37:00 +0100 Subject: Add bc solution to challenge 003 --- challenge-003/paulo-custodio/bc/ch-1.bc | 55 +++++++++++++++++++++++++++++++++ challenge-006/paulo-custodio/bc/ch-2.bc | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 challenge-003/paulo-custodio/bc/ch-1.bc diff --git a/challenge-003/paulo-custodio/bc/ch-1.bc b/challenge-003/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..119c7557f5 --- /dev/null +++ b/challenge-003/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,55 @@ +#!/usr/bin/bc -ql + +/* +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less or +equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. +*/ + +scale = 0 + +num = read() + +define min(a,b) { + if (a < b) { return a; } else { return b; } +} + +define min3(a,b,c) { + return min(a,min(b,c)); +} + +q2[0] = 1; q2size = 1 +q3[0] = 1; q3size = 1 +q5[0] = 1; q5size = 1 + +for (i = 0; i < num; i++) { + /* next hamming: get smallest of the queue heads */ + h = min3(q2[0], q3[0], q5[0]) + h + + /* shift used multiples */ + if (h == q2[0]) { + for (j = 1; j < q2size; j++) + q2[j-1] = q2[j] + q2size = q2size-1 + } + if (h == q3[0]) { + for (j = 1; j < q3size; j++) + q3[j-1] = q3[j] + q3size = q3size-1 + } + if (h == q5[0]) { + for (j = 1; j < q5size; j++) + q5[j-1] = q5[j] + q5size = q5size-1 + } + + /* push next multiples */ + q2[q2size] = 2*h; q2size = q2size+1 + q3[q3size] = 3*h; q3size = q3size+1 + q5[q5size] = 5*h; q5size = q5size+1 +} +quit diff --git a/challenge-006/paulo-custodio/bc/ch-2.bc b/challenge-006/paulo-custodio/bc/ch-2.bc index 631718706d..3fa72be981 100644 --- a/challenge-006/paulo-custodio/bc/ch-2.bc +++ b/challenge-006/paulo-custodio/bc/ch-2.bc @@ -5,4 +5,4 @@ ex=pi*sqrt(163) rk=e(ex) scale=12 rk/1 -quit \ No newline at end of file +quit -- cgit From 47c31b2fddb7e0695795d799aaf1cdbf82083a3f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:40:05 +0100 Subject: Add bc solution to challenge 007 --- challenge-007/paulo-custodio/bc/ch-1.bc | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-007/paulo-custodio/bc/ch-1.bc diff --git a/challenge-007/paulo-custodio/bc/ch-1.bc b/challenge-007/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..217322c92b --- /dev/null +++ b/challenge-007/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,35 @@ +#!/usr/bin/bc -ql + +/* +Challenge 007 + +Challenge #1 +Print all the niven numbers from 0 to 50 inclusive, each on their own line. +A niven number is a non-negative number that is divisible by the sum of its +digits. +*/ + +scale = 0 + +num = read() + +define niven(n) { + auto rem, sum + sum = 0 + rem = n + while (rem > 0) { + sum += rem % 10 + rem /= 10 + } + if ((n % sum) == 0) { + return 1 + } else { + return 0 + } +} + +for (i = 1; i <= num; i++) + if (niven(i)) + print i, "\n" + +quit -- cgit From dd2a41d26cfab5d551a88ff16d0b6062518d34e0 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:43:49 +0100 Subject: Add bc solution to challenge 008 --- challenge-008/paulo-custodio/bc/ch-1.bc | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-008/paulo-custodio/bc/ch-1.bc diff --git a/challenge-008/paulo-custodio/bc/ch-1.bc b/challenge-008/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..12053a2445 --- /dev/null +++ b/challenge-008/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,49 @@ +#!/usr/bin/bc -ql + +/* +Challenge 008 + +Challenge #1 +Write a script that computes the first five perfect numbers. A perfect number +is an integer that is the sum of its positive proper divisors (all divisors +except itself). Please check Wiki for more information. +This challenge was proposed by Laurent Rosenfeld. +*/ + +scale = 0 + +num = read() + +define is_prime(n) { + auto i + if (n <= 1) return 0 + if (n <= 3) return 1 + if ((n % 2) == 0 || (n % 3) == 0) return 0 + for (i = 5; i*i <= n; i += 6) { + if ((n % i) == 0 || (n % (i + 2)) == 0) return 0 + } + return 1 +} + +define next_prime(n) { + if (n <= 1) return 2 + n = n+1 + while (!is_prime(n)) n = n+1 + return n; +} + +p = 1 +define next_perfect() { + auto f + while (1) { + p = next_prime(p) + f = (2 ^ p) - 1 + if (is_prime(f)) return (2 ^ (p-1)) * f + } +} + +for (i = 0; i < num; i++) { + print next_perfect(), "\n" +} + +quit -- cgit From 786579fc8f23db25a7c8de1b2d420cf357d25c7b Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:55:43 +0100 Subject: Add bc solution to challenge 009 --- challenge-009/paulo-custodio/bc/ch-1.bc | 31 ++++++++++++++++++++++++++++ challenge-009/paulo-custodio/t/test-1.yaml | 25 ++++++++++++++++++++++ challenge-009/paulo-custodio/t/test-2.yaml | 9 ++++++++ challenge-009/paulo-custodio/test.pl | 33 +++--------------------------- 4 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 challenge-009/paulo-custodio/bc/ch-1.bc create mode 100644 challenge-009/paulo-custodio/t/test-1.yaml create mode 100644 challenge-009/paulo-custodio/t/test-2.yaml diff --git a/challenge-009/paulo-custodio/bc/ch-1.bc b/challenge-009/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..0449047840 --- /dev/null +++ b/challenge-009/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,31 @@ +#!/usr/bin/bc -ql + +/* +Challenge 009 + +Challenge #1 +Write a script that finds the first square number that has at least 5 distinct +digits. This was proposed by Laurent Rosenfeld. +*/ + +scale = 0 +num = read() + +define num_diff_digits(n) { + auto digit, digits[], i, sum + while (n > 0) { + digit = n % 10 + n /= 10 + digits[digit] = 1 + } + sum = 0 + for (i = 0; i < 10; i++) + sum += digits[i] + return sum; +} + +n = 1 +while (num_diff_digits(n*n) < num) + n = n+1 +n*n +quit diff --git a/challenge-009/paulo-custodio/t/test-1.yaml b/challenge-009/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0e7966c514 --- /dev/null +++ b/challenge-009/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 2 + input: + output: 16 +- setup: + cleanup: + args: 3 + input: + output: 169 +- setup: + cleanup: + args: 4 + input: + output: 1024 +- setup: + cleanup: + args: 5 + input: + output: 12769 diff --git a/challenge-009/paulo-custodio/t/test-2.yaml b/challenge-009/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..06e3445fb4 --- /dev/null +++ b/challenge-009/paulo-custodio/t/test-2.yaml @@ -0,0 +1,9 @@ +- setup: + cleanup: + args: 2 3 1 4 2 2 1 0 + input: + output: | + |Data: 2, 3, 1, 4, 2, 2, 1, 0 + |Standard ranking: 3, 2, 6, 1, 3, 3, 6, 8 + |Modified ranking: 5, 2, 7, 1, 5, 5, 7, 8 + |Dense ranking: 3, 2, 4, 1, 3, 3, 4, 5 diff --git a/challenge-009/paulo-custodio/test.pl b/challenge-009/paulo-custodio/test.pl index c5275ed8f4..ba6c37260b 100644 --- a/challenge-009/paulo-custodio/test.pl +++ b/challenge-009/paulo-custodio/test.pl @@ -1,31 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; - -is capture("perl perl/ch-1.pl 1"), "1\n"; -is capture("perl perl/ch-1.pl 2"), "16\n"; -is capture("perl perl/ch-1.pl 3"), "169\n"; -is capture("perl perl/ch-1.pl 4"), "1024\n"; -is capture("perl perl/ch-1.pl 5"), "12769\n"; - - -is capture("perl perl/ch-2.pl 2 3 1 4 2 2 1 0"), < Date: Thu, 24 Jun 2021 20:07:39 +0100 Subject: Add bc solution to challenge 011 --- challenge-011/paulo-custodio/bc/ch-1.bc | 39 ++++++++++++++++++++++++ challenge-011/paulo-custodio/t/test-1.yaml | 5 ++++ challenge-011/paulo-custodio/t/test-2.yaml | 37 +++++++++++++++++++++++ challenge-011/paulo-custodio/test.pl | 48 ++---------------------------- 4 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 challenge-011/paulo-custodio/bc/ch-1.bc create mode 100644 challenge-011/paulo-custodio/t/test-1.yaml create mode 100644 challenge-011/paulo-custodio/t/test-2.yaml diff --git a/challenge-011/paulo-custodio/bc/ch-1.bc b/challenge-011/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..c8e8ea3a1c --- /dev/null +++ b/challenge-011/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,39 @@ +#!/usr/bin/bc -ql + +/* +Challenge 011 + +Challenge #1 +Write a script that computes the equal point in the Fahrenheit and Celsius +scales, knowing that the freezing point of water is 32 °F and 0 °C, and that +the boiling point of water is 212 °F and 100 °C. This challenge was proposed +by Laurent Rosenfeld. +*/ + +scale = 10 + +/* +F = (C * 9/5) + 32 +F = C = x + => f(x) = (x * 9/5) + 32 - x = 0 +<=> f(x) = (9/5 - 1) * x + 32 + f'(x) = 9/5 - 1 +*/ + +define f(x) { return (9/5 - 1) * x + 32 } +define df(x) { return 9/5 - 1 } + +define newton(x0) { + auto x1, tmp + x1 = x0+1 + while (x0 != x1) { + x1 = x0 - f(x0)/df(x0) + tmp = x1; x1 = x0; x0 = tmp + } + return x0 +} + +t = newton(0) +scale = 0 +t/1 +quit diff --git a/challenge-011/paulo-custodio/t/test-1.yaml b/challenge-011/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5ea8bd975e --- /dev/null +++ b/challenge-011/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: -40 diff --git a/challenge-011/paulo-custodio/t/test-2.yaml b/challenge-011/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..3d3d7b7bb3 --- /dev/null +++ b/challenge-011/paulo-custodio/t/test-2.yaml @@ -0,0 +1,37 @@ +- setup: + cleanup: + args: 1 + input: + output: | + |[[1]] +- setup: + cleanup: + args: 2 + input: + output: | + |[[1, 0] + | [0, 1]] +- setup: + cleanup: + args: 4 + input: + output: | + |[[1, 0, 0, 0] + | [0, 1, 0, 0] + | [0, 0, 1, 0] + | [0, 0, 0, 1]] +- setup: + cleanup: + args: 10 + input: + output: | + |[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0] + | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] + | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0] + | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] + | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] + | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0] + | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0] + | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] + | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0] + | [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]] diff --git a/challenge-011/paulo-custodio/test.pl b/challenge-011/paulo-custodio/test.pl index 68b96b8cf7..ba6c37260b 100644 --- a/challenge-011/paulo-custodio/test.pl +++ b/challenge-011/paulo-custodio/test.pl @@ -1,46 +1,4 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use 5.030; +#!/usr/bin/env perl +use Modern::Perl; use Test::More; - -is capture("perl perl/ch-1.pl"), "-40\n"; - -is capture("perl perl/ch-2.pl 1"), < Date: Thu, 24 Jun 2021 22:52:22 +0100 Subject: Add C and C++ solutions to challenge 009 --- challenge-009/paulo-custodio/c/ch-1.c | 38 +++++++++ challenge-009/paulo-custodio/c/ch-2.c | 134 ++++++++++++++++++++++++++++++ challenge-009/paulo-custodio/cpp/ch-1.cpp | 37 +++++++++ challenge-009/paulo-custodio/cpp/ch-2.cpp | 127 ++++++++++++++++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 challenge-009/paulo-custodio/c/ch-1.c create mode 100644 challenge-009/paulo-custodio/c/ch-2.c create mode 100644 challenge-009/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-009/paulo-custodio/cpp/ch-2.cpp diff --git a/challenge-009/paulo-custodio/c/ch-1.c b/challenge-009/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..1a199fdf2c --- /dev/null +++ b/challenge-009/paulo-custodio/c/ch-1.c @@ -0,0 +1,38 @@ +/* +Challenge 009 + +Challenge #1 +Write a script that finds the first square number that has at least 5 distinct +digits.This was proposed by Laurent Rosenfeld. +*/ + +#include +#include +#include + +int num_diff_digits(int n) { + bool digits[10] = {0}; + int count = 0; + while (n > 0) { + int digit = n % 10; + n /= 10; + if (!digits[digit]) { + digits[digit] = true; + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + int num = 0; + if (argc == 2) + num = atoi(argv[1]); + if (num == 0) + num = 5; + + int n = 1; + while (num_diff_digits(n * n) < num) + n++; + printf("%d\n", n * n); +} diff --git a/challenge-009/paulo-custodio/c/ch-2.c b/challenge-009/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..c5b0a0be59 --- /dev/null +++ b/challenge-009/paulo-custodio/c/ch-2.c @@ -0,0 +1,134 @@ +/* +Challenge 009 + +Challenge #2 +Write a script to perform different types of ranking as described below: + +1. Standard Ranking (1224): Items that compare equal receive the same ranking + number, and then a gap is left in the ranking numbers. +2. Modified Ranking (1334): It is done by leaving the gaps in the ranking + numbers before the sets of equal-ranking items. +3. Dense Ranking (1223): Items that compare equally receive the same + ranking number, and the next item(s) receive the immediately following + ranking number. +*/ + +#include +#include +#include + +typedef struct score_t { + int seq; + int score; + int rank; +} score_t; + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int rev_by_score(const void* a, const void* b) { + return -(((score_t*)a)->score - ((score_t*)b)->score); // reverse-sort +} + +int by_seq(const void* a, const void* b) { + return ((score_t*)a)->seq - ((score_t*)b)->seq; +} + +void standard_ranking(int num_scores, score_t* scores) { + qsort(scores, num_scores, sizeof(score_t), rev_by_score); + int rank = 1; + for (int i = 0; i < num_scores; i++) { + int count = 0; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) { + count++; + scores[j].rank = rank; + } + rank += count; + i += count - 1; + } + qsort(scores, num_scores, sizeof(score_t), by_seq); +} + +void modified_ranking(int num_scores, score_t* scores) { + qsort(scores, num_scores, sizeof(score_t), rev_by_score); + int rank = 1; + for (int i = 0; i < num_scores; i++) { + int count = 0; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) + count++; + rank += count-1; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) + scores[j].rank = rank; + rank++; + i += count - 1; + } + qsort(scores, num_scores, sizeof(score_t), by_seq); +} + +void dense_ranking(int num_scores, score_t* scores) { + qsort(scores, num_scores, sizeof(score_t), rev_by_score); + int rank = 1; + for (int i = 0; i < num_scores; i++) { + int count = 0; + for (int j = i; j < num_scores && scores[i].score == scores[j].score; j++) { + count++; + scores[j].rank = rank; + } + rank++; + i += count - 1; + } + qsort(scores, num_scores, sizeof(score_t), by_seq); +} + + +int main(int argc, char* argv[]) { + if (argc < 2) return EXIT_FAILURE; + int num_scores = argc - 1; + score_t* scores = check_mem(calloc(num_scores, sizeof(score_t))); + for (int i = 0; i < num_scores; i++) { + scores[i].seq = i; + scores[i].score = atoi(argv[i + 1]); + } + + printf("Data: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].score); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + standard_ranking(num_scores, scores); + printf("Standard ranking: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].rank); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + modified_ranking(num_scores, scores); + printf("Modified ranking: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].rank); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + dense_ranking(num_scores, scores); + printf("Dense ranking: "); + for (int i = 0; i < num_scores; i++) { + printf("%d", scores[i].rank); + if (i + 1 < num_scores) + printf(", "); + } + printf("\n"); + + free(scores); +} diff --git a/challenge-009/paulo-custodio/cpp/ch-1.cpp b/challenge-009/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..a647b3cfd7 --- /dev/null +++ b/challenge-009/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,37 @@ +/* +Challenge 009 + +Challenge #1 +Write a script that finds the first square number that has at least 5 distinct +digits.This was proposed by Laurent Rosenfeld. +*/ + +#include +using namespace std; + +int num_diff_digits(int n) { + bool digits[10] = {0}; + int count = 0; + while (n > 0) { + int digit = n % 10; + n /= 10; + if (!digits[digit]) { + digits[digit] = true; + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + int num = 0; + if (argc == 2) + num = atoi(argv[1]); + if (num == 0) + num = 5; + + int n = 1; + while (num_diff_digits(n * n) < num) + n++; + cout << n * n << endl; +} diff --git a/challenge-009/paulo-custodio/cpp/ch-2.cpp b/challenge-009/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..fe621cfa33 --- /dev/null +++ b/challenge-009/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,127 @@ +/* +Challenge 009 + +Challenge #2 +Write a script to perform different types of ranking as described below: + +1. Standard Ranking (1224): Items that compare equal receive the same ranking + number, and then a gap is left in the ranking numbers. +2. Modified Ranking (1334): It is done by leaving the gaps in the ranking + numbers before the sets of equal-ranking items. +3. Dense Ranking (1223): Items that compare equally receive the same + ranking number, and the next item(s) receive the immediately following + ranking number. +*/ + +#include +#include +#include +using namespace std; + +struct Score { + int seq; + int score; + int rank; +}; + +bool rev_by_score(const Score& a, const Score& b) { + return a.score > b.score; +} + +bool by_seq(const Score& a, const Score& b) { + return a.seq < b.seq; +} + +void standard_ranking(vector& scores) { + sort(scores.begin(), scores.end(), rev_by_score); + int rank = 1; + for (size_t i = 0; i < scores.size(); i++) { + int count = 0; + for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++) { + count++; + scores[j].rank = rank; + } + rank += count; + i += count - 1; + } + sort(scores.begin(), scores.end(), by_seq); +} + +void modified_ranking(vector& scores) { + sort(scores.begin(), scores.end(), rev_by_score); + int rank = 1; + for (size_t i = 0; i < scores.size(); i++) { + int count = 0; + for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++) + count++; + rank += count - 1; + for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++) + scores[j].rank = rank; + rank++; + i += count - 1; + } + sort(scores.begin(), scores.end(), by_seq); +} + +void dense_ranking(vector& scores) { + sort(scores.begin(), scores.end(), rev_by_score); + int rank = 1; + for (size_t i = 0; i < scores.size(); i++) { + int count = 0; + for (size_t j = i; j < scores.size() && scores[i].score == scores[j].score; j++) { + count++; + scores[j].rank = rank; + } + rank++; + i += count - 1; + } + sort(scores.begin(), scores.end(), by_seq); +} + + +int main(int argc, char* argv[]) { + if (argc < 2) return EXIT_FAILURE; + vector scores; + for (int i = 1; i < argc; i++) { + Score s; + s.seq = i; + s.score = atoi(argv[i]); + s.rank = 0; + scores.push_back(s); + } + + cout << "Data: "; + for (size_t i = 0; i < scores.size(); i++) { + cout << scores[i].score; + if (i + 1 < scores.size()) + cout << ", "; + } + printf("\n"); + + standard_ranking(scores); + cout << "Standard ranking: "; + for (size_t i = 0; i < scores.size(); i++) { + cout << scores[i].rank; + if (i + 1 < scores.size()) + cout << ", "; + } + cout << endl; + + modified_ranking(scores); + cout << "Modified ranking: "; + for (size_t i = 0; i < scores.size(); i++) { + cout << scores[i].rank; + if (i + 1 < scores.size()) + cout << ", "; + } + cout << endl; + + dense_ranking(scores); + cout << "Dense ranking: "; + for (size_t i = 0; i < scores.size(); i++) { + cout << scores[i].rank; + if (i + 1 < scores.size()) + cout << ", "; + } + cout << endl; +} -- cgit From b262986570ff9ba130d2ce4d96389b7b8e1129fa Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Fri, 25 Jun 2021 23:26:15 +0800 Subject: Week 118, wooo~ --- challenge-118/cheok-yin-fung/perl/ch-1.pl | 43 +++++++ challenge-118/cheok-yin-fung/perl/ch-2.pl | 175 ++++++++++++++++++++++++++ challenge-118/cheok-yin-fung/perl/pre-ch-2.pl | 76 +++++++++++ 3 files changed, 294 insertions(+) create mode 100644 challenge-118/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-118/cheok-yin-fung/perl/ch-2.pl create mode 100644 challenge-118/cheok-yin-fung/perl/pre-ch-2.pl diff --git a/challenge-118/cheok-yin-fung/perl/ch-1.pl b/challenge-118/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..655951468f --- /dev/null +++ b/challenge-118/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More tests => 7; + +my $r = $ARGV[0] || 0; +print binpali($r),"\n"; + + + +sub binpali { + my @d = dec2binarr($_[0])->@*; + return (join "", @d) eq (join "", reverse @d) ? 1 : 0; +} + + +sub dec2binarr { + my $s = $_[0]; + my $i = 0; + my @digit; + while ($s != 0) { + ($s, $digit[$i] ) = divmod($s)->@*; + $i++; + } + return [@digit]; +} + + +sub divmod { + my $num = $_[0]; + return [int $num / 2 , $num % 2]; +} + + + +ok (binpali(3) == 1, "test target: 3"); +ok (binpali(4) == 0, "test target: 4"); +ok (binpali(6) == 0, "test target: 6"); +ok (binpali(1023) == 1, "test target 1023"); +ok (binpali(oct "0b1001001" ) == 1, "the 5th test"); +ok (binpali(oct "0b1000011100001" ) == 1, "the 6th test"); +ok (binpali(oct "0b101001" ) == 0, "the 7th test"); + diff --git a/challenge-118/cheok-yin-fung/perl/ch-2.pl b/challenge-118/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..9a3e282d32 --- /dev/null +++ b/challenge-118/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,175 @@ +#!/usr/bin/perl + + +=pod +# N 3 2 3^ +# 3 2^ 1 2 +# 2 1 4 3 +# 3^ 2 3 2 + + a b c d + -------- + N * * * |4 + * * * * |3 + * x * * |2 + * x x * |1 + +x : b1, b2, c1 + +b1 <-> b2 : 3 +b1 <-> c1 : 3 +b2 <-> c1 : 2 +a4(N) <-> b1 : 2 +a4 <-> b2 : 1 +a4 <-> c1 : 3 + +N -> b1 -> b2 -> c1 : 2 + 3 + 2 = 7 +N -> b1 -> c1 -> b2 : 2 + 3 + 2 = 7 +N -> b2 -> b1 -> c1 : 1 + 3 + 3 = 7 +N -> b2 -> c1 -> b1 : 1 + 2 + 3 = 6 +N -> c1 -> b1 -> b2 : 3 + 3 + 3 = 9 +N -> c1 -> b2 -> b1 : 3 + 2 + 3 = 8 +=cut + +# The Weekly Challenge 118 +# Task 2 Adventure of Knight +# Usage: ch-2.pl a2 b1 b2 b3 c4 e6 + +use strict; +use warni