From 5b9b66407e7630bcbed0e88cdf7a37ed1bdf1cf5 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 6 Jan 2022 19:00:39 +0100 Subject: Week 003 & Week 123: Tcl solution for part 1. --- challenge-003/abigail/README.md | 3 ++- challenge-003/abigail/tcl/ch-1.tcl | 37 +++++++++++++++++++++++++++++++++++++ challenge-123/abigail/README.md | 1 + challenge-123/abigail/tcl/ch-1.tcl | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 challenge-003/abigail/tcl/ch-1.tcl create mode 100644 challenge-123/abigail/tcl/ch-1.tcl 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) +} diff --git a/challenge-123/abigail/README.md b/challenge-123/abigail/README.md index 449bc1d0e3..a82d00c1a8 100644 --- a/challenge-123/abigail/README.md +++ b/challenge-123/abigail/README.md @@ -32,6 +32,7 @@ Output: 12 * [Python](python/ch-1.py) * [R](r/ch-1.r) * [Ruby](ruby/ch-1.rb) +* [Tcl](tcl/ch-1.tcl) ### Blog [Perl Weekly Challenge 123: Ugly Numbers][blog1] diff --git a/challenge-123/abigail/tcl/ch-1.tcl b/challenge-123/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..4d9358ea5c --- /dev/null +++ b/challenge-123/abigail/tcl/ch-1.tcl @@ -0,0 +1,37 @@ +#!/usr/local/opt/tcl-tk/bin/tclsh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 +# + +# +# 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) +} -- cgit