From 2f06e8b5eac7eae1b54eac7a946fae2758759766 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 27 Jan 2021 21:19:17 +0100 Subject: Lua solution for week 3, part 1 --- challenge-003/abigail/lua/ch-1.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-003/abigail/lua/ch-1.lua (limited to 'challenge-003/abigail/lua') diff --git a/challenge-003/abigail/lua/ch-1.lua b/challenge-003/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..2d396d899c --- /dev/null +++ b/challenge-003/abigail/lua/ch-1.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for max in io . lines () do + max = tonumber (max) + base2 = 1 + -- + -- Lua doesn't have a for (expr; expr; expr) syntax. + -- This is missed here. + -- + while base2 <= max do + base3 = base2 + while base3 <= max do + base5 = base3 + while base5 <= max do + print (base5) + base5 = base5 * 5 + end + base3 = base3 * 3 + end + base2 = base2 * 2 + end +end -- cgit From 7260520cd91bd98c143d5717f57ea0281852ac8f Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 28 Jan 2021 12:04:12 +0100 Subject: Use 'local' for variables in lua. --- challenge-003/abigail/lua/ch-1.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'challenge-003/abigail/lua') diff --git a/challenge-003/abigail/lua/ch-1.lua b/challenge-003/abigail/lua/ch-1.lua index 2d396d899c..70de9b1923 100644 --- a/challenge-003/abigail/lua/ch-1.lua +++ b/challenge-003/abigail/lua/ch-1.lua @@ -9,16 +9,16 @@ -- for max in io . lines () do - max = tonumber (max) - base2 = 1 + local max = tonumber (max) + local base2 = 1 -- -- Lua doesn't have a for (expr; expr; expr) syntax. -- This is missed here. -- while base2 <= max do - base3 = base2 + local base3 = base2 while base3 <= max do - base5 = base3 + local base5 = base3 while base5 <= max do print (base5) base5 = base5 * 5 -- cgit From 3a0ad850aa19fad89a422abe0e45989fe4448137 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 28 Jan 2021 12:19:18 +0100 Subject: Lua solution for week 3, part 2 --- challenge-003/abigail/lua/ch-2.lua | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-003/abigail/lua/ch-2.lua (limited to 'challenge-003/abigail/lua') diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..321d7d25d1 --- /dev/null +++ b/challenge-003/abigail/lua/ch-2.lua @@ -0,0 +1,49 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +-- +-- Create and print row 0 +-- +local row = {1} +print "1" + +-- +-- Iterate over the input +-- +for line in io . lines () do + local rows = tonumber (line) + local r + for r = 1, rows do + -- + -- Create a new row + -- + local new = {} + for i = 1, r + 1 do + sum = 0; + if i > 1 + then sum = sum + row [i - 1] + io . write (" ") + end + if i <= r + then sum = sum + row [i] + end + -- + -- Assign new element, and print it + -- + new [i] = sum + io . write (sum) + end + io . write ("\n") + -- + -- New row becomes current row + -- + row = new + end +end -- cgit From 7d2dd894b51a42703190653da355cd952ada800c Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 28 Jan 2021 13:59:26 +0100 Subject: Printing a 0th row should be inside the main loop. --- challenge-003/abigail/lua/ch-2.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'challenge-003/abigail/lua') diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua index 321d7d25d1..0e30b0a9c6 100644 --- a/challenge-003/abigail/lua/ch-2.lua +++ b/challenge-003/abigail/lua/ch-2.lua @@ -8,17 +8,19 @@ -- Run as: lua ch-2.lua < input-file -- --- --- Create and print row 0 --- -local row = {1} -print "1" -- -- Iterate over the input -- for line in io . lines () do local rows = tonumber (line) + + -- + -- Create and print row 0 + -- + local row = {1} + print "1" + local r for r = 1, rows do -- -- cgit