aboutsummaryrefslogtreecommitdiff
path: root/challenge-039
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-12-20 22:42:11 +0100
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-12-20 22:42:11 +0100
commit533c7ae08b0fcac5544f2945c990ea583c187ebf (patch)
tree16827f34098818fb7e9df0e6e40b2ac0ba3b87a5 /challenge-039
parent2033c83ccafc6a44d65153209fea390e44200d4d (diff)
downloadperlweeklychallenge-club-533c7ae08b0fcac5544f2945c990ea583c187ebf.tar.gz
perlweeklychallenge-club-533c7ae08b0fcac5544f2945c990ea583c187ebf.tar.bz2
perlweeklychallenge-club-533c7ae08b0fcac5544f2945c990ea583c187ebf.zip
Solution to challenge 39 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-039')
-rw-r--r--challenge-039/noud/perl6/ch1.p647
-rw-r--r--challenge-039/noud/perl6/ch2.p648
-rw-r--r--challenge-039/noud/perl6/guestbook.txt10
3 files changed, 105 insertions, 0 deletions
diff --git a/challenge-039/noud/perl6/ch1.p6 b/challenge-039/noud/perl6/ch1.p6
new file mode 100644
index 0000000000..a1e295623e
--- /dev/null
+++ b/challenge-039/noud/perl6/ch1.p6
@@ -0,0 +1,47 @@
+# A guest house had a policy that the light remain ON as long as the at least
+# one guest is in the house. There is guest book which tracks all guest in/out
+# time. Write a script to find out how long in minutes the light were ON.
+#
+# Guest Book
+#
+# 1) Alex IN: 09:10 OUT: 09:45
+# 2) Arnold IN: 09:15 OUT: 09:33
+# 3) Bob IN: 09:22 OUT: 09:55
+# 4) Charlie IN: 09:25 OUT: 10:05
+# 5) Steve IN: 09:33 OUT: 10:01
+# 6) Roger IN: 09:44 OUT: 10:12
+# 7) David IN: 09:57 OUT: 10:23
+# 8) Neil IN: 10:01 OUT: 10:19
+# 9) Chris IN: 10:10 OUT: 11:00
+
+
+# Instead of counting how long the lights were on we count how long the lights
+# where out. In this way it's possible to count the minutes in O(n). I "assume"
+# this is a the 24-hour clock.
+
+# The current time in minutes.
+my $time = 0;
+
+# The counted minutes of darkness from 00:00 till $time.
+my $time_off = 0;
+
+# Minutes of darkness after the last guest checked out.
+my $time_last = 24 * 60;
+
+for 'guestbook.txt'.IO.lines -> $line {
+ my $rs = $line ~~ /(\d+)\:(\d+)\D+(\d+)\:(\d+)/;
+ my $start = 60 * Int($rs[0]) + Int($rs[1]);
+ my $end = 60 * Int($rs[2]) + Int($rs[3]);
+
+ if ($time < $start) {
+ $time_off += $start - $time;
+ }
+
+ $time = $end;
+ $time_last = 24 * 60 - $end;
+}
+
+$time_off += $time_last;
+my $time_on = 24 * 60 - $time_off;
+
+say "The light was on for $time_on minutes";
diff --git a/challenge-039/noud/perl6/ch2.p6 b/challenge-039/noud/perl6/ch2.p6
new file mode 100644
index 0000000000..4dfdb9806b
--- /dev/null
+++ b/challenge-039/noud/perl6/ch2.p6
@@ -0,0 +1,48 @@
+# Write a script to demonstrate Reverse Polish notation(RPN). Checkout the wiki
+# page for more information about RPN.
+
+my %operator = (
+ '+' => {$^a + $^b;},
+ '-' => {$^a - $^b;},
+ '*' => {$^a * $^b;},
+ '/' => {$^a / $^b;},
+ '%' => {$^a % $^b;},
+);
+
+sub rpn($s) {
+ my @stack = [];
+
+ for $s.split(/\s+/) -> $a {
+ if (%operator{$a}:exists) {
+ if (@stack.elems < 2) {
+ die "Invalid expression";
+ }
+ # Don't forget that the last two elements on the stack need to be
+ # reversed in the operation.
+ my $y = @stack.pop();
+ my $x = @stack.pop();
+ @stack.push(%operator{$a}($x, $y));
+ } elsif ($a ~~ /\d+/) {
+ @stack.push(Int($a));
+ } else {
+ die "Unknown operator $a";
+ }
+ }
+
+ return @stack.pop();
+}
+
+
+my $s = "";
+
+# (1 + (2 + 3)) + 5 = 11
+$s = "1 2 3 + + 5 +";
+say "$s " ~ rpn($s) ~ " =";
+
+# (15 / (7 - (1 + 1))) * 3 + (2 + (1 - 1)) = 5
+$s = "15 7 1 1 + - / 3 * 2 1 1 + + -";
+say "$s " ~ rpn($s) ~ " =";
+
+# (7 * 6) % 7 = 0
+$s = "7 6 * 7 %";
+say "$s " ~ rpn($s) ~ " =";
diff --git a/challenge-039/noud/perl6/guestbook.txt b/challenge-039/noud/perl6/guestbook.txt
new file mode 100644
index 0000000000..6939dfee6a
--- /dev/null
+++ b/challenge-039/noud/perl6/guestbook.txt
@@ -0,0 +1,10 @@
+1) Alex IN: 09:10 OUT: 09:45
+2) Arnold IN: 09:15 OUT: 09:33
+3) Bob IN: 09:22 OUT: 09:55
+4) Charlie IN: 09:25 OUT: 10:05
+5) Steve IN: 09:33 OUT: 10:01
+6) Roger IN: 09:44 OUT: 10:12
+7) David IN: 09:57 OUT: 10:23
+8) Neil IN: 10:01 OUT: 10:19
+9) Chris IN: 10:10 OUT: 11:00
+10) Noud IN: 12:00 OUT: 13:00