diff options
| -rw-r--r-- | challenge-324/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-324/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-324/adam-russell/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-324/adam-russell/perl/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-324/adam-russell/prolog/ch-1.p | 13 | ||||
| -rw-r--r-- | challenge-324/adam-russell/prolog/ch-2.p | 21 |
6 files changed, 118 insertions, 0 deletions
diff --git a/challenge-324/adam-russell/blog.txt b/challenge-324/adam-russell/blog.txt new file mode 100644 index 0000000000..4fbaa49651 --- /dev/null +++ b/challenge-324/adam-russell/blog.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/perl/2025/06/08 diff --git a/challenge-324/adam-russell/blog1.txt b/challenge-324/adam-russell/blog1.txt new file mode 100644 index 0000000000..588a87cddc --- /dev/null +++ b/challenge-324/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/prolog/2025/06/08 diff --git a/challenge-324/adam-russell/perl/ch-1.pl b/challenge-324/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..3adee3b8e6 --- /dev/null +++ b/challenge-324/adam-russell/perl/ch-1.pl @@ -0,0 +1,49 @@ + + use v5.40; + + sub create_array{ + my($i, $r, $c) = @_; + my @a = (); + for (0 .. $r - 1){ + my $row = []; + for (0 .. $c - 1){ + push @{$row}, shift @{$i}; + } + push @a, $row; + } + return @a; + } + + +MAIN:{ + my $s = q//; + $s .= q/(/; + do{ + $s.= (q/[/ . join(q/, /, @{$_}) . q/], /); + } for create_array [1, 2, 3, 4], 2, 2; + chop $s; + chop $s; + $s .= q/)/; + say $s; + + $s = q//; + $s .= q/(/; + do{ + $s.= (q/[/ . join(q/, /, @{$_}) . q/], /); + } for create_array [1, 2, 3], 1, 3; + chop $s; + chop $s; + $s .= q/)/; + say $s; + + $s = q//; + $s .= q/(/; + do{ + $s.= (q/[/ . join(q/, /, @{$_}) . q/], /); + } for create_array [1, 2, 3, 4], 4, 1; + chop $s; + chop $s; + $s .= q/)/; + say $s; +} + diff --git a/challenge-324/adam-russell/perl/ch-2.pl b/challenge-324/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..6f741983d8 --- /dev/null +++ b/challenge-324/adam-russell/perl/ch-2.pl @@ -0,0 +1,33 @@ + + use v5.40; + + sub power_set{ + my @a = (); + for my $i (1 .. 2 ** @_- 1){ + my @digits = (); + for my $j (0 .. @_ - 1){ + push @digits, $_[$j] if 1 == ($i >> $j & 1); + } + push @a, \@digits; + } + return @a; + } + + + sub calculate_total_xor{ + my $total = 0; + for my $a (power_set @_){ + my $t = 0; + $t = eval join q/ ^ /, ($t, @{$a}); + $total += $t; + } + return $total; + } + + +MAIN:{ + say calculate_total_xor 1, 3; + say calculate_total_xor 5, 1, 6; + say calculate_total_xor 3, 4, 5, 6, 7, 8; +} + diff --git a/challenge-324/adam-russell/prolog/ch-1.p b/challenge-324/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..1f463460af --- /dev/null +++ b/challenge-324/adam-russell/prolog/ch-1.p @@ -0,0 +1,13 @@ + + + create_array(_, 0, _, []). + create_array(L, Rows, Columns, [Row|T]) :- + create_row(L, Columns, Row, L1), + R is Rows - 1, + create_array(L1, R, Columns, T). + + create_row(L, 0, [], L). + create_row([H|T], Columns, [H|Row], L) :- + C is Columns - 1, + create_row(T, C, Row, L). + diff --git a/challenge-324/adam-russell/prolog/ch-2.p b/challenge-324/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..0947aeb539 --- /dev/null +++ b/challenge-324/adam-russell/prolog/ch-2.p @@ -0,0 +1,21 @@ + + + subtotal(Combined, X):- + X is Combined. + + + total_xor(L, Total):- + findall(S, ( + sublist(S, L), + \+ S = [] + ), SubLists), + maplist(combine, SubLists, Combined), + maplist(subtotal, Combined, SubTotals), + sum_list(SubTotals, Total). + + + combine([], 0). + combine([H|T], Combined):- + combine(T, Combined1), + Combined = xor(H, Combined1). + |
