aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/ash
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2020-10-10 18:34:15 +0800
committer冯昶 <seaker@qq.com>2020-10-10 18:34:15 +0800
commitf0a88ea2365d8ae7dfb90c969d83368a18b53f9a (patch)
tree64665b115f1023519b48f3f366bb743ab68375d8 /challenge-080/ash
parent353a66fe9f75298e44b27d692109259646e7d09f (diff)
parenta8ea1576ec8f1801e4c90d906c5d3c18ebde5ca6 (diff)
downloadperlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.tar.gz
perlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.tar.bz2
perlweeklychallenge-club-f0a88ea2365d8ae7dfb90c969d83368a18b53f9a.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-080/ash')
-rw-r--r--challenge-080/ash/cpp/ch-1.cpp38
-rw-r--r--challenge-080/ash/raku/ch-1.raku25
-rw-r--r--challenge-080/ash/raku/ch-2.raku38
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-080/ash/cpp/ch-1.cpp b/challenge-080/ash/cpp/ch-1.cpp
new file mode 100644
index 0000000000..278833993b
--- /dev/null
+++ b/challenge-080/ash/cpp/ch-1.cpp
@@ -0,0 +1,38 @@
+/*
+ Task 1 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/
+*/
+
+#include <iostream>
+#include <vector>
+#include <map>
+
+using namespace std;
+
+int missing_n(vector<int> n) {
+ map<int, bool> v;
+
+ int max = n[0];
+ for (auto x : n) {
+ v[x] = true;
+ if (x > max) max = x;
+ }
+
+ for (auto c = 1; c < max; c++) {
+ if (!v[c]) {
+ return c;
+ break;
+ }
+ }
+
+ return max + 1;
+}
+
+int main() {
+ cout << (missing_n(vector<int> {5, 2, -2, 0}) == 1) << endl;
+ cout << (missing_n(vector<int> {1, 8, -1}) == 2) << endl;
+ cout << (missing_n(vector<int> {5, 2, -2, 0}) == 1) << endl;
+
+ cout << (missing_n(vector<int> {5, 2, 3, 1, -2, 0}) == 4) << endl;
+ cout << (missing_n(vector<int> {5, 2, 3, 1, 4, -2, 0}) == 6) << endl;
+}
diff --git a/challenge-080/ash/raku/ch-1.raku b/challenge-080/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..d8de2c2957
--- /dev/null
+++ b/challenge-080/ash/raku/ch-1.raku
@@ -0,0 +1,25 @@
+#!/usr/bin/env raku
+#
+# Task 1 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/
+
+use Test;
+
+is missing-n(5, 2, -2, 0), 1;
+is missing-n(1, 8, -1), 2;
+is missing-n(2, 0, -1), 1;
+
+# If the smallest missing is the biggest :-)
+is missing-n(5, 2, 3, 1, -2, 0), 4;
+is missing-n(5, 2, 3, 1, 4, -2, 0), 6;
+
+sub missing-n(*@n is copy) {
+ @n.=grep: * > 0;
+
+ my $missing = max(@n);
+ for 1 .. $missing + 1 {
+ $missing = $_ and last unless @n.grep($_);
+ }
+
+ return $missing;
+}
diff --git a/challenge-080/ash/raku/ch-2.raku b/challenge-080/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..9ad29269c6
--- /dev/null
+++ b/challenge-080/ash/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+#
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/
+
+use Test;
+
+is candies([1, 2, 2]), 5;
+is candies([1, 4, 3, 2]), 7;
+
+# candies [1, 2, 2];
+# candies [1, 4, 2, 1, 5];
+# candies [1, 2, 2, 1, 3];
+# candies [1, 4, 2, 1, 5, 2, 3, 1, 1, 3, 2];
+
+sub candies(@n) {
+ my @candies = 1 xx @n.elems;
+
+ my $s;
+
+ repeat {
+ $s = @candies.sum;
+
+ @candies[0]++ if @n[0] > @n[1] && @candies[0] <= @candies[1];
+
+ for 1 ..^ @n.end -> $i {
+ @candies[$i]++ if @n[$i] > @n[$i - 1] && @candies[$i] <= @candies[$i - 1];
+ @candies[$i]++ if @n[$i] > @n[$i + 1] && @candies[$i] <= @candies[$i + 1];
+ }
+
+ @candies[*-1]++ if (@n[*-1] > @n[*-2] && @candies[*-1] <= @candies[*-2]) || (@n[*-1] == @n[*-2] && @candies[*-1] < @candies[*-2]);
+
+ say @candies;
+
+ } until $s == @candies.sum;
+
+ return $s;
+}