diff options
| author | Abigail <abigail@abigail.be> | 2021-11-28 02:18:01 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-11-28 02:18:01 +0100 |
| commit | 8d07862431f748b31ab6c8e4cf6963e2e9a67e3a (patch) | |
| tree | ef055386f472c9e87ea79e56235c4daf8a9d9266 | |
| parent | 1b165d3e3899a7411dfa3dc965076c50c86ac942 (diff) | |
| download | perlweeklychallenge-club-8d07862431f748b31ab6c8e4cf6963e2e9a67e3a.tar.gz perlweeklychallenge-club-8d07862431f748b31ab6c8e4cf6963e2e9a67e3a.tar.bz2 perlweeklychallenge-club-8d07862431f748b31ab6c8e4cf6963e2e9a67e3a.zip | |
bc solution for week 126, part 1
| -rw-r--r-- | challenge-126/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-126/abigail/bc/ch-1.bc | 51 | ||||
| -rw-r--r-- | challenge-126/abigail/t/ctest.ini | 3 |
3 files changed, 55 insertions, 0 deletions
diff --git a/challenge-126/abigail/README.md b/challenge-126/abigail/README.md index 954f574de7..be2c32b5f4 100644 --- a/challenge-126/abigail/README.md +++ b/challenge-126/abigail/README.md @@ -28,6 +28,7 @@ There are 13 numbers between `1` and `25` that don't contain digit `1`: ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [bc](bc/ch-1.bc) * [C](c/ch-1.c) * [Go](go/ch-1.go) * [Lua](lua/ch-1.lua) diff --git a/challenge-126/abigail/bc/ch-1.bc b/challenge-126/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..a3204ccc31 --- /dev/null +++ b/challenge-126/abigail/bc/ch-1.bc @@ -0,0 +1,51 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# + +# +# Kind of reverses a number. Trailing zero's will be dropped though +# +define reverse (n) { + result = 0 + while (n > 0) { + result = 10 * result + (n % 10) + n = n / 10 + } + return result +} + + +while (1) { + n = read (); if (n == 0) {break} + # + # Reverse the number -- after adding a 1 + # + n = reverse (10 * n + 1) + + result = 0 + seen_one = 0 + while (n > 9) { + digit = n % 10 + n = n / 10 + result = result * 9 + if (seen_one == 1) { + result = result + 8 + } else { + if (digit == 1) { + seen_one = 1 + } else { + if (digit > 0) { + result = result + digit - 1 + } + } + } + } + result +} + + +halt diff --git a/challenge-126/abigail/t/ctest.ini b/challenge-126/abigail/t/ctest.ini index 84c6227ab4..dd16bcecfe 100644 --- a/challenge-126/abigail/t/ctest.ini +++ b/challenge-126/abigail/t/ctest.ini @@ -7,3 +7,6 @@ 1-1 = Given Examples
1-2 = Other Examples
2-1 = Given Example
+
+[1-1,1-2/bc]
+add_to_input = 0
|
