aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/ash
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-09-28 17:13:38 +0800
committer冯昶 <seaker@qq.com>2020-09-28 17:13:38 +0800
commit77ff55eb1341ab51717bc619629cf3a39f799e50 (patch)
tree74b23f13e52303d22fa58ee592c45002e274abb8 /challenge-079/ash
parentacbc30c04977af09ff3e93e74642e325ea1e7083 (diff)
parentaa14cbf8342e04b936f40bcc720a23a258137ecd (diff)
downloadperlweeklychallenge-club-77ff55eb1341ab51717bc619629cf3a39f799e50.tar.gz
perlweeklychallenge-club-77ff55eb1341ab51717bc619629cf3a39f799e50.tar.bz2
perlweeklychallenge-club-77ff55eb1341ab51717bc619629cf3a39f799e50.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-079/ash')
-rw-r--r--challenge-079/ash/cpp/ch-1.cpp43
-rw-r--r--challenge-079/ash/raku/ch-1.raku45
-rw-r--r--challenge-079/ash/raku/ch-1a.raku20
-rw-r--r--challenge-079/ash/raku/ch-2.raku32
4 files changed, 140 insertions, 0 deletions
diff --git a/challenge-079/ash/cpp/ch-1.cpp b/challenge-079/ash/cpp/ch-1.cpp
new file mode 100644
index 0000000000..27596b8967
--- /dev/null
+++ b/challenge-079/ash/cpp/ch-1.cpp
@@ -0,0 +1,43 @@
+/*
+ Task 1 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/
+*/
+
+#include <iostream>
+#include <sstream>
+
+using namespace std;
+
+int main(int argc, char** argv) {
+ if (argc != 2) {
+ cerr << "Usage: ./a.out 42" << endl;
+ return 1;
+ }
+
+ stringstream input(argv[1]);
+
+ int n;
+ input >> n;
+
+ int total_bits = 0;
+ int scale = 1;
+ n++;
+ while (scale <= n) {
+ int scale2 = scale << 1;
+
+ int fill_full = n / scale2;
+ int fill_frac = n % scale2;
+
+ int bits_full = fill_full * scale;
+ int bits_frac = 0;
+ if (fill_frac > scale) {
+ bits_frac = fill_frac - scale;
+ }
+
+ total_bits += bits_full + bits_frac;
+
+ scale = scale2;
+ }
+
+ cout << "There are " << total_bits << " set bits in the sequence from 1 to " << n << endl;
+}
diff --git a/challenge-079/ash/raku/ch-1.raku b/challenge-079/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..1e08b79b29
--- /dev/null
+++ b/challenge-079/ash/raku/ch-1.raku
@@ -0,0 +1,45 @@
+#!/usr/bin/env raku
+#
+# Task 1 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/#
+#
+# Comments:
+#
+# This program implements a fast algorithm.
+#
+# Output:
+#
+# $ raku ch-1.raku 42
+# 108
+#
+# $ raku ch-1.raku 123456789
+# 1644989771
+
+unit sub MAIN($n = 42, :v(:$verbose));
+
+my @scale = (1, 2 * * ...^ * > $n);
+
+my $total-bits = 0;
+for @scale -> $pwr {
+
+ my $fill = ($n + 1) / (2 * $pwr);
+ my $max = $pwr * 2;
+ say "\n$pwr: fill=$fill, max=$max" if $verbose;
+
+ my $fill-full = $fill.Int;
+ my $fill-frac = $fill - $fill-full;
+
+ my $bits-full = $fill-full * $pwr;
+ my $bits-frac = 0;
+
+ if $fill-frac > 0.5 {
+ $bits-frac = ($fill-frac - 0.5) * $max;
+ say "($fill-frac - 0.5) * $pwr" if $verbose;
+ }
+
+ my $bits = $bits-full + $bits-frac;
+ say "bits-full=$bits-full, bits-frac=$bits-frac, bits=$bits" if $verbose;
+
+ $total-bits += $bits;
+}
+say $total-bits;
diff --git a/challenge-079/ash/raku/ch-1a.raku b/challenge-079/ash/raku/ch-1a.raku
new file mode 100644
index 0000000000..c23fb1eea9
--- /dev/null
+++ b/challenge-079/ash/raku/ch-1a.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/env raku
+#
+# Task 1 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/
+#
+# Comments:
+#
+# This is atraightforwad but slow (for big input numbers) solution.
+#
+# Output:
+#
+# $ raku ch-1a.raku 42
+# 108
+#
+# $ raku ch-1a.raku 500
+# 2222
+
+my $n = @*ARGS[0] // 42;
+
+say [+] ((.base(2) ~~ m:g/1/).elems for 1..$n);
diff --git a/challenge-079/ash/raku/ch-2.raku b/challenge-079/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..d04ce7a257
--- /dev/null
+++ b/challenge-079/ash/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+#
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/
+
+my @shape = 2, 1, 4, 1, 2, 5;
+
+# Make the matrix and fill it with '#' (bricks) and '~' (water);
+my $height = max(@shape);
+my @map;
+for @shape -> $cell {
+ push @map, Array.new(('#' xx $cell, '~' xx ($height - $cell)).flat);
+}
+
+blow-water-out(0, @shape.end);
+blow-water-out(@shape.end, 0);
+
+.join.say for @map;
+
+# Units of water left
+say @map.join.join.indices('~').elems;
+
+sub blow-water-out($from, $to) {
+ # Blow from left or from right to remove high waters.
+ my $max = @shape[$from]; # Height of the left-most (or right-most) cell
+ for $from ... $to -> $pos {
+ $max = @shape[$pos] if @shape[$pos] > $max; # Raising wall, increase the maximum
+
+ # Remove water above the current maximum and fill it with air
+ @map[$pos; $_] = '.' for $max ..^ $height;
+ }
+}