aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2023-03-06 00:00:57 +0100
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2023-03-06 00:00:57 +0100
commit18086c67c1583352041b964491b7dd54381302cb (patch)
tree2ea9e016e71c1c419c9b3438be1b5c81ea073678
parentb8a8e4a6f319b191d5d38786fd7c55eda14a6575 (diff)
downloadperlweeklychallenge-club-18086c67c1583352041b964491b7dd54381302cb.tar.gz
perlweeklychallenge-club-18086c67c1583352041b964491b7dd54381302cb.tar.bz2
perlweeklychallenge-club-18086c67c1583352041b964491b7dd54381302cb.zip
feat: add solutions for challenge 206 from BarrOff
-rw-r--r--challenge-206/barroff/blog.txt1
-rw-r--r--challenge-206/barroff/raku/ch-1.raku42
-rw-r--r--challenge-206/barroff/raku/ch-2.raku24
3 files changed, 67 insertions, 0 deletions
diff --git a/challenge-206/barroff/blog.txt b/challenge-206/barroff/blog.txt
new file mode 100644
index 0000000000..3d05782eca
--- /dev/null
+++ b/challenge-206/barroff/blog.txt
@@ -0,0 +1 @@
+https://barroff.codeberg.page/posts/2023/2023-03-05-challenge-206/
diff --git a/challenge-206/barroff/raku/ch-1.raku b/challenge-206/barroff/raku/ch-1.raku
new file mode 100644
index 0000000000..cb269c58e2
--- /dev/null
+++ b/challenge-206/barroff/raku/ch-1.raku
@@ -0,0 +1,42 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+constant $time-format = /
+ $<hours>=(\d ** 2)
+ ':'
+ $<minutes>=(\d ** 2)
+/;
+
+sub convert-to-minutes(Str $time --> Int) {
+ $time ~~ $time-format or die "Time string not in the right format.\nExpected 'HH:MM' but got $time";
+ $<hours> × 60 + $<minutes>;
+}
+
+sub time-difference(Str $time1, Str $time2 --> Int) {
+ constant $minutes-of-a-day = 24 × 60;
+ my Int $minutes1 = convert-to-minutes($time1);
+ my Int $minutes2 = convert-to-minutes($time2);
+ my Int $time-diff = abs($minutes1 - $minutes2);
+ $time-diff < $minutes-of-a-day ÷ 2 ?? $time-diff !! $minutes-of-a-day - $time-diff;
+}
+
+sub shortest-time(Str @times --> Int) {
+ min(map({ time-difference(@_[0], @_[1]) }, @times.combinations(2)));
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is shortest-time(Array[Str].new(["00:00", "23:55", "20:00"])), 5, 'works for ("00:00", "23:55", "20:00")';
+ is shortest-time(Array[Str].new(["01:01", "00:50", "00:57"])), 4, 'works for ("01:01", "00:50", "00:57")';
+ is shortest-time(Array[Str].new(["10:10", "09:30", "09:00", "09:55"])), 15, 'works for ("10:10", "09:30", "09:00", "09:55")';
+}
+
+#| Take user provided list like 1 2 2 3
+multi sub MAIN(*@elements where @elements.elems ≥ 2 && all(@elements) ~~ $time-format) {
+ my Str @str-elements = @elements;
+ say shortest-time(@str-elements);
+}
diff --git a/challenge-206/barroff/raku/ch-2.raku b/challenge-206/barroff/raku/ch-2.raku
new file mode 100644
index 0000000000..916f1adc0d
--- /dev/null
+++ b/challenge-206/barroff/raku/ch-2.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub array-pairings(Int @array --> Int) {
+ my Int @sorted-array = sort(@array);
+ my Int @even-indices = grep({ $_ % 2 == 0 }, 0 ..^ @array.elems);
+ return sum(@sorted-array[@even-indices]);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 2;
+
+ is array-pairings(Array[Int].new([1, 2, 3, 4])), 4, 'works for (1, 2, 3, 4)';
+ is array-pairings(Array[Int].new([0, 2, 1, 3])), 2, 'works for (0, 2, 1, 3)';
+}
+
+#| Take user provided list like 1 2 2 3
+multi sub MAIN(*@elements where @elements.elems ≥ 2 && @elements.elems % 2 == 0 && all(@elements) ~~ /^<[+-]>?<[0..9]>+$/) {
+ my Int @int-elements = @elements;
+ say array-pairings(@int-elements);
+}