aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/abigail/r
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-18 21:08:19 +0100
committerAbigail <abigail@abigail.be>2021-03-18 21:12:03 +0100
commite6c8b5142c50afdc9fb43178f55e48415040c701 (patch)
treec297f4e21e39a75e47bb320209dbbf6326dd04f2 /challenge-104/abigail/r
parent3b126f6e1e8ebd10ac4201a45076ef58e0d368c4 (diff)
downloadperlweeklychallenge-club-e6c8b5142c50afdc9fb43178f55e48415040c701.tar.gz
perlweeklychallenge-club-e6c8b5142c50afdc9fb43178f55e48415040c701.tar.bz2
perlweeklychallenge-club-e6c8b5142c50afdc9fb43178f55e48415040c701.zip
R solution for week 104, part 2
Diffstat (limited to 'challenge-104/abigail/r')
-rw-r--r--challenge-104/abigail/r/ch-2.r27
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-104/abigail/r/ch-2.r b/challenge-104/abigail/r/ch-2.r
new file mode 100644
index 0000000000..e4ec31fe30
--- /dev/null
+++ b/challenge-104/abigail/r/ch-2.r
@@ -0,0 +1,27 @@
+#
+# See ../README.md
+#
+
+#
+# Run as: Rscript ch-2.r < input-file
+#
+
+input <- file ('stdin', 'r')
+
+tokens <- 12
+max_take <- 3
+
+while (tokens > 0) {
+ cat ("How many tokens do you take? ")
+ cat (sprintf ("(%2d tokens are left) ", tokens))
+ take <- readLines (input, n = 1)
+ take <- as.integer (take)
+ if (1 <= take && take <= max_take) {
+ takes <- max_take + 1 - take
+ s <- if (takes == 1) "" else "s"
+ cat (sprintf ("Computer takes %d token%s\n", takes, s))
+ tokens <- tokens - (max_take + 1)
+ }
+}
+
+cat ("Computer wins\n")