aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-121/adam-russell/blog.txt1
-rw-r--r--challenge-121/adam-russell/blog1.txt1
-rw-r--r--challenge-121/adam-russell/perl/ch-1.pl27
-rw-r--r--challenge-121/adam-russell/perl/ch-2.pl68
-rw-r--r--challenge-121/adam-russell/prolog/ch-1.p41
5 files changed, 138 insertions, 0 deletions
diff --git a/challenge-121/adam-russell/blog.txt b/challenge-121/adam-russell/blog.txt
new file mode 100644
index 0000000000..297696e467
--- /dev/null
+++ b/challenge-121/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/07/18
diff --git a/challenge-121/adam-russell/blog1.txt b/challenge-121/adam-russell/blog1.txt
new file mode 100644
index 0000000000..4275477d70
--- /dev/null
+++ b/challenge-121/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/07/18
diff --git a/challenge-121/adam-russell/perl/ch-1.pl b/challenge-121/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..21a610a169
--- /dev/null
+++ b/challenge-121/adam-russell/perl/ch-1.pl
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+##
+# You are given integers 0 <= $m <= 255 and 1 <= $n <= 8.
+# Write a script to invert $n bit from the end of the binary
+# representation of $m and print the decimal representation
+# of the new binary number.
+##
+
+sub flip_bit_n{
+ my($x, $n) = @_;
+ my $bits = substr(unpack("B32", pack("N", $x)), 24, 8);
+ my @bits = split(//, $bits);
+ $bits[@bits - $n] ^= 1;
+ my $flipped_decimal = unpack("N", pack("B32", substr("0" x 32 . join("", @bits), -32)));
+ return $flipped_decimal;
+}
+
+MAIN:{
+ my($M, $N);
+ $M = 12;
+ $N = 3;
+ print flip_bit_n($M, $N) . "\n";
+ $M = 18;
+ $N = 4;
+ print flip_bit_n($M, $N) . "\n";
+}
diff --git a/challenge-121/adam-russell/perl/ch-2.pl b/challenge-121/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..c07d9c66d7
--- /dev/null
+++ b/challenge-121/adam-russell/perl/ch-2.pl
@@ -0,0 +1,68 @@
+use strict;
+use warnings;
+##
+# You are given a NxN matrix containing the distances between N cities.
+# Write a script to find a round trip of minimum length visiting
+# all N cities exactly once and returning to the start.
+##
+use boolean;
+use AI::Genetic;
+
+use constant N => 20;
+
+my @matrix= ([0, 5, 2, 7],
+ [5, 0, 5, 3],
+ [3, 1, 0, 6],
+ [4, 5, 4, 0]);
+
+sub fitness{
+ my($genes) = @_;
+ my $cost = 0;
+ return -1 if $genes->[0] != $genes->[@{$genes} - 1];
+ my @path = sort {$a <=> $b} @{$genes}[0 .. @{$genes} - 2];
+ for my $i (0 .. (@path - 2)){
+ return -1 if $path[$i] == $path[$i + 1];
+ }
+ for my $i (0 .. @{$genes} - 2){
+ $cost += $matrix[$genes->[$i]][$genes->[$i + 1]];
+ }
+ return 1/$cost;
+}
+
+sub terminate{
+ return true;
+}
+
+MAIN:{
+ srand(121);
+ my $aig = new AI::Genetic(
+ -fitness => \&fitness,
+ -type => "rangevector",
+ -population => 500,
+ -crossover => 0.9,
+ -mutation => 0.1,
+ );
+ my $genes = [];
+ for (0 .. N + 1){
+ push @{$genes}, [0, N];
+ }
+ @matrix = ();
+ for (0 .. N){
+ my $row = [];
+ for my $i (0 .. N){
+ push @{$row}, int(rand(N * 2 + 1));
+ }
+ push @matrix, $row;
+ }
+ $aig->init(
+ $genes
+ );
+ $aig->evolve("tournamentUniform", 100000);
+ my $path = $aig->getFittest()->genes();
+ print join(",", @{$path}) . "\n";
+ my $cost;
+ for my $i (0 .. @{$path} - 2){
+ $cost += $matrix[$path->[$i]][$path->[$i + 1]];
+ }
+ print "cost: $cost\n";
+}
diff --git a/challenge-121/adam-russell/prolog/ch-1.p b/challenge-121/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..4acb12a5c6
--- /dev/null
+++ b/challenge-121/adam-russell/prolog/ch-1.p
@@ -0,0 +1,41 @@
+:-initialization(main).
+
+pad(Bits, Padded):-
+ length(Bits, L),
+ PadLength is 8 - L,
+ length(Padding, PadLength),
+ maplist(=(0), Padding),
+ append(Padding, Bits, Padded).
+
+bits(N, Bits):-
+ bits(N, [], Bits).
+bits(0, Bits, Bits).
+bits(N, Bit_Accum, Bits):-
+ B is N /\ 1,
+ N0 is N >> 1,
+ bits(N0, [B|Bit_Accum], Bits).
+
+flip_nth_bit(N, Bits, NthFlipped):-
+ N0 is 9 - N,
+ N1 is 8 - N,
+ nth(N0, Bits, B),
+ Flipped is xor(B, 1),
+ length(Bits0, N1),
+ append(Bits0, [B|T], Bits),
+ append(Bits0, [Flipped|T], NthFlipped).
+
+decimal(Bits, Decimal):-
+ decimal(Bits, 0, Decimal).
+decimal([], Decimal, Decimal).
+decimal([H|T], DecimalAccum, Decimal):-
+ length([H|T], B),
+ D is (H * 2 ** (B - 1)) + DecimalAccum,
+ decimal(T, D, Decimal).
+
+main:-
+ bits(12, B),
+ pad(B, Padded),
+ flip_nth_bit(3, Padded, Flipped),
+ decimal(Flipped, Decimal),
+ write(Decimal), nl,
+ halt.