aboutsummaryrefslogtreecommitdiff
path: root/challenge-097/abigail/lua/ch-2.lua
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-30 17:01:45 +0100
committerAbigail <abigail@abigail.be>2021-01-30 17:51:27 +0100
commit7de0601266583815c0bfb3f00cb71672997b19c3 (patch)
tree46c1abc7925da4426abd93df53ae01feca60d531 /challenge-097/abigail/lua/ch-2.lua
parent19e332207808276edd1ed1bebd3e907136acc438 (diff)
downloadperlweeklychallenge-club-7de0601266583815c0bfb3f00cb71672997b19c3.tar.gz
perlweeklychallenge-club-7de0601266583815c0bfb3f00cb71672997b19c3.tar.bz2
perlweeklychallenge-club-7de0601266583815c0bfb3f00cb71672997b19c3.zip
Swap the role of sections and section length (size).
We initially implemented the algorithm where the number of sections is passed in as a parameter. However, it's not the number of sections, it's the length of each section. This was an easy fix, as the algorithm is pretty symmetric.
Diffstat (limited to 'challenge-097/abigail/lua/ch-2.lua')
-rw-r--r--challenge-097/abigail/lua/ch-2.lua16
1 files changed, 8 insertions, 8 deletions
diff --git a/challenge-097/abigail/lua/ch-2.lua b/challenge-097/abigail/lua/ch-2.lua
index 0136c0c1e5..fcdce75263 100644
--- a/challenge-097/abigail/lua/ch-2.lua
+++ b/challenge-097/abigail/lua/ch-2.lua
@@ -5,20 +5,20 @@
--
--
--- Run as: lua ch-2.lua -s SECTIONS < input-file
+-- Run as: lua ch-2.lua -s SIZE < input-file
--
--
-- Parse option, and exit if incorrect
--
-local sections = -1
+local size = -1
if #arg == 2 and arg [1] == "-s"
-then sections = arg [2] + 0
+then size = arg [2] + 0
end
-if sections < 0
-then io . stderr : write ("Requires a '-s SECTIONS' option\n")
+if size < 0
+then io . stderr : write ("Requires a '-s SIZE' option\n")
os . exit (1)
end
@@ -29,12 +29,12 @@ end
-- Calculate the number of 1's. Add the minimum of the two to a running sum.
--
for line in io . lines () do
- local s_len = #line / sections
+ local sections = #line / size
local sum = 0
- for i = 0, s_len - 1 do
+ for i = 0, size - 1 do
local zeros = 0
for j = 0, sections - 1 do
- index = j * s_len + i + 1
+ index = j * size + i + 1
if string . sub (line, index, index) == "0"
then zeros = zeros + 1
end