diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-07-19 14:26:28 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-07-19 14:26:28 -0500 |
| commit | e542ee52b8f6be5d340550000cbe98f4b2aefb1e (patch) | |
| tree | f419b7bb3501203a1d2b637eeb61df2eba87768d /challenge-121/abigail/lua | |
| parent | 51feed7f6f1ba280658615d36ee2f7ccf77b5000 (diff) | |
| parent | 675c4ed9a3b441729b9558c051638027242ba77a (diff) | |
| download | perlweeklychallenge-club-e542ee52b8f6be5d340550000cbe98f4b2aefb1e.tar.gz perlweeklychallenge-club-e542ee52b8f6be5d340550000cbe98f4b2aefb1e.tar.bz2 perlweeklychallenge-club-e542ee52b8f6be5d340550000cbe98f4b2aefb1e.zip | |
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
Diffstat (limited to 'challenge-121/abigail/lua')
| -rw-r--r-- | challenge-121/abigail/lua/ch-1.lua | 23 | ||||
| -rw-r--r-- | challenge-121/abigail/lua/ch-2.lua | 69 |
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-121/abigail/lua/ch-1.lua b/challenge-121/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..52b7529007 --- /dev/null +++ b/challenge-121/abigail/lua/ch-1.lua @@ -0,0 +1,23 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + _, _, m, n = line : find ("(%d+)%s+(%d+)") + x = 1 + for i = 1, n - 1 do + x = x * 2 + end + if math . floor (m / x) % 2 == 1 then + m = m - x + else + m = m + x + end + print (m) +end diff --git a/challenge-121/abigail/lua/ch-2.lua b/challenge-121/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..72ad70f2eb --- /dev/null +++ b/challenge-121/abigail/lua/ch-2.lua @@ -0,0 +1,69 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function shortest_path (matrix, from, to, exclude) + if (1 + #exclude == #matrix) then + return matrix [from] [to], {to} + end + + local shortest = 10 ^ 999 -- Should be big enough... + local sh_path = {} + local new_exclude = {} + for k, v in pairs (exclude) do + new_exclude [k] = v + end + new_exclude [from] = 1 + + local next + for next = 1, #matrix do + if next == from or next == to or new_exclude [next] == 1 then + goto end_loop + end + + local length + local path + length, path = shortest_path (matrix, next, to, new_exclude) + if shortest > length + matrix [from] [next] then + shortest = length + matrix [from] [next] + sh_path = {} + sh_path [1] = next + for k, v in pairs (path) do + sh_path [k + 1] = v + end + end + ::end_loop:: + end + + return shortest, sh_path +end + + +matrix = {} + +for line in io . lines () do + row = {} + for d in line : gmatch ("%d+") do + table . insert (row, d) + end + table . insert (matrix, row) +end + + +local length +local path +length, path = shortest_path (matrix, 1, 1, {}) + +print (length) +io . write ("0") +for i = 1, #path do + io . write (" ") + io . write (path [i] - 1) +end +print ("") |
