From 495c230a90bd20dac94816da117ecbb16c2c20e8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 9 Feb 2022 18:27:40 +0100 Subject: Week 151: bc solution for part 2 --- challenge-151/abigail/README.md | 1 + challenge-151/abigail/bc/ch-2.bc | 37 +++++++++++++++++++++++++++++++++++++ challenge-151/abigail/t/ctest.ini | 3 +++ 3 files changed, 41 insertions(+) create mode 100644 challenge-151/abigail/bc/ch-2.bc diff --git a/challenge-151/abigail/README.md b/challenge-151/abigail/README.md index 96d5223161..0d3e8807d3 100644 --- a/challenge-151/abigail/README.md +++ b/challenge-151/abigail/README.md @@ -16,6 +16,7 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) * [C](c/ch-2.c) * [Go](go/ch-2.go) * [Java](java/ch-2.java) diff --git a/challenge-151/abigail/bc/ch-2.bc b/challenge-151/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..d4f479a047 --- /dev/null +++ b/challenge-151/abigail/bc/ch-2.bc @@ -0,0 +1,37 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: bc ch-2.bc < input-file +# +# End each line of input with a 0 +# End input with a -1 + +while (1) { + n = read () + if (n < 0) { + break + } + sz = 0 + while (n > 0) { + h [sz] = n + sz = sz + 1 + n = read () + } + h [sz + 0] = 0 + h [sz + 1] = 0 + for (i = sz - i; i >= 2; i --) { + if (h [i] + h [i + 2] > h [i + 1]) { + h [i] = h [i] + h [i + 2] + } else { + h [i] = h [i + 1] + } + } + h [0] + h [2] +} + + +halt diff --git a/challenge-151/abigail/t/ctest.ini b/challenge-151/abigail/t/ctest.ini index 0a8cb97d18..f2021a0482 100644 --- a/challenge-151/abigail/t/ctest.ini +++ b/challenge-151/abigail/t/ctest.ini @@ -7,3 +7,6 @@ 1-1 = Given Examples 2-1 = Given Examples +[2-1/bc] +add_to_line = 0 +add_to_input = -1 -- cgit