aboutsummaryrefslogtreecommitdiff
path: root/challenge-003
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2022-01-06 19:00:39 +0100
committerAbigail <abigail@abigail.freedom.nl>2022-01-06 19:00:39 +0100
commit5b9b66407e7630bcbed0e88cdf7a37ed1bdf1cf5 (patch)
treec78893aed4195ab074bdb61b19684f864bb2a987 /challenge-003
parentb97c7e9a60e75f3a5daedbc80fe7d767305b4d53 (diff)
downloadperlweeklychallenge-club-5b9b66407e7630bcbed0e88cdf7a37ed1bdf1cf5.tar.gz
perlweeklychallenge-club-5b9b66407e7630bcbed0e88cdf7a37ed1bdf1cf5.tar.bz2
perlweeklychallenge-club-5b9b66407e7630bcbed0e88cdf7a37ed1bdf1cf5.zip
Week 003 & Week 123: Tcl solution for part 1.
Diffstat (limited to 'challenge-003')
-rw-r--r--challenge-003/abigail/README.md3
-rw-r--r--challenge-003/abigail/tcl/ch-1.tcl37
2 files changed, 39 insertions, 1 deletions
diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md
index 6d798bf875..5a50b02ed5 100644
--- a/challenge-003/abigail/README.md
+++ b/challenge-003/abigail/README.md
@@ -12,8 +12,9 @@
* [Pascal](pascal/ch-1.p)
* [Perl](perl/ch-1.pl)
* [Python](python/ch-1.py)
-* [Ruby](ruby/ch-1.rb)
* [R](r/ch-1.r)
+* [Ruby](ruby/ch-1.rb)
+* [Tcl](tcl/ch-1.tcl)
### Part 2
diff --git a/challenge-003/abigail/tcl/ch-1.tcl b/challenge-003/abigail/tcl/ch-1.tcl
new file mode 100644
index 0000000000..3c1b5f888c
--- /dev/null
+++ b/challenge-003/abigail/tcl/ch-1.tcl
@@ -0,0 +1,37 @@
+#!/usr/local/opt/tcl-tk/bin/tclsh
+
+#
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
+#
+
+#
+# Run as: tclsh ch-1.tcl < input-file
+#
+
+while {[gets stdin max] >= 0} {
+ set ugly(0) 1
+ set count 0
+ set next_2 0
+ set next_3 0
+ set next_5 0
+
+ while {$count < $max - 1} {
+ incr count
+
+ set c2 [expr 2 * $ugly($next_2)]
+ set c3 [expr 3 * $ugly($next_3)]
+ set c5 [expr 5 * $ugly($next_5)]
+
+ if {$c2 <= $c3 && $c2 <= $c5} {set min $c2}
+ if {$c3 <= $c2 && $c3 <= $c5} {set min $c3}
+ if {$c5 <= $c2 && $c5 <= $c3} {set min $c5}
+
+ set ugly($count) $min
+
+ if {$c2 <= $ugly($count)} {incr next_2}
+ if {$c3 <= $ugly($count)} {incr next_3}
+ if {$c5 <= $ugly($count)} {incr next_5}
+ }
+
+ puts $ugly($count)
+}