From c1db8bac0eaafcc7e7c94d712ce24dc69b776c53 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 14 Nov 2021 13:03:11 +0100 Subject: Solutions for week 138 in 14 languages. --- challenge-138/abigail/lua/ch-1.lua | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 challenge-138/abigail/lua/ch-1.lua (limited to 'challenge-138/abigail/lua') diff --git a/challenge-138/abigail/lua/ch-1.lua b/challenge-138/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..5f61cb3ec6 --- /dev/null +++ b/challenge-138/abigail/lua/ch-1.lua @@ -0,0 +1,44 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local SUNDAY = 0 +local MONDAY = 1 +local TUESDAY = 2 +local WEDNESDAY = 3 +local THURSDAY = 4 +local FRIDAY = 5 +local SATURDAY = 6 + +local lookup = { + {261, 261, 260, 260, 261, 261, 261}, + {262, 262, 261, 260, 261, 262, 262}, +} + +local anchors = {TUESDAY, SUNDAY, FRIDAY, WEDNESDAY} + +function doomsday (year) + local anchor = anchors [1 + math . floor (year / 100) % 4] + local y = year % 100 + return ((math . floor (y / 12) + (y % 12) + math . floor ((y % 12) / 4)) + + anchor) % 7 +end + +function is_leap (year) + if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 ~= 0)) then + return 1 + end + return 0 +end + + +for year in io . lines () do + year = tonumber (year) + print (lookup [1 + is_leap (year)] [1 + doomsday (year)]) +end -- cgit