From f1bc02f8fcc156205b2a8ccabaf0063f32ab8106 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 22:37:05 +0100 Subject: Week 151: Lua solutions --- challenge-151/abigail/lua/ch-2.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-151/abigail/lua/ch-2.lua (limited to 'challenge-151/abigail/lua/ch-2.lua') diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..98df0aae1d --- /dev/null +++ b/challenge-151/abigail/lua/ch-2.lua @@ -0,0 +1,33 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local houses = {} + local best = {} + for val in line : gmatch ("%d+") do + houses [#houses + 1] = val + end + + for i = #houses, 1, -1 do + if 2 >= #houses then + best [i] = houses [i] + elseif i == #houses then + best [i] = houses [i] + elseif i == 0 then + best [i] = houses [i] + best [i + 2] + elseif i == #houses - 1 then + best [i] = math . max (houses [i], best [i + 1]) + else + best [i] = math . max (houses [i] + best [i + 2], + best [i + 1]) + end + end + print (best [1]) +end -- cgit From 5045026f0fe4bf7ba93d00350f8bb6bbde68f5be Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 11:51:15 +0100 Subject: Week 151: Simplified algorithm for part 2. By adding two dummy 0's to the "best" array, and not bothering the calculate the best values for the first two houses, we don't have a case study inside the main loop. --- challenge-151/abigail/lua/ch-2.lua | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'challenge-151/abigail/lua/ch-2.lua') diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua index 98df0aae1d..0e644d0a6c 100644 --- a/challenge-151/abigail/lua/ch-2.lua +++ b/challenge-151/abigail/lua/ch-2.lua @@ -15,19 +15,12 @@ for line in io . lines () do houses [#houses + 1] = val end - for i = #houses, 1, -1 do - if 2 >= #houses then - best [i] = houses [i] - elseif i == #houses then - best [i] = houses [i] - elseif i == 0 then - best [i] = houses [i] + best [i + 2] - elseif i == #houses - 1 then - best [i] = math . max (houses [i], best [i + 1]) - else - best [i] = math . max (houses [i] + best [i + 2], - best [i + 1]) - end + best [#houses + 1] = 0 + best [#houses + 2] = 0 + + for i = #houses, 3, -1 do + best [i] = math . max (houses [i] + best [i + 2], best [i + 1]) end - print (best [1]) + + print (houses [1] + best [3]) end -- cgit From c6bb015992c65d4248161ca12c22a1af6dd29711 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 18:33:35 +0100 Subject: Week 151: Make the algorithm for part 2 even simpler. No need for an additional array: add two 0's to the array of valuables, and modify in situ. --- challenge-151/abigail/lua/ch-2.lua | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'challenge-151/abigail/lua/ch-2.lua') diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua index 0e644d0a6c..d8ac15c101 100644 --- a/challenge-151/abigail/lua/ch-2.lua +++ b/challenge-151/abigail/lua/ch-2.lua @@ -9,18 +9,17 @@ -- for line in io . lines () do - local houses = {} - local best = {} + local h = {} for val in line : gmatch ("%d+") do - houses [#houses + 1] = val + h [#h + 1] = val end - best [#houses + 1] = 0 - best [#houses + 2] = 0 + h [#h + 1] = 0 + h [#h + 1] = 0 - for i = #houses, 3, -1 do - best [i] = math . max (houses [i] + best [i + 2], best [i + 1]) + for i = #h - 2, 3, -1 do + h [i] = math . max (h [i] + h [i + 2], h [i + 1]) end - print (houses [1] + best [3]) + print (h [1] + h [3]) end -- cgit