From 5cc673384d60321f9c7845d6a2d444946852703b Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 12 Sep 2024 22:38:06 -0500 Subject: [PATCH] Compress code --- lua/src/lib/primes.lua | 2 +- lua/src/p0001.lua | 9 +++------ lua/src/p0002.lua | 6 ++---- lua/src/p0003.lua | 3 +-- lua/src/p0004.lua | 9 +++------ lua/src/p0006.lua | 3 +-- lua/src/p0008.lua | 9 +++------ lua/src/p0009.lua | 12 ++++-------- lua/src/p0011.lua | 24 ++++++++---------------- lua/src/p0013.lua | 15 +++++---------- lua/src/p0017.lua | 21 +++++++-------------- lua/src/p0028.lua | 3 +-- lua/src/p0034.lua | 9 +++------ lua/src/p0076.lua | 15 +++++---------- lua/test.lua | 12 ++++-------- 15 files changed, 51 insertions(+), 101 deletions(-) diff --git a/lua/src/lib/primes.lua b/lua/src/lib/primes.lua index d78f092c..20e6d658 100644 --- a/lua/src/lib/primes.lua +++ b/lua/src/lib/primes.lua @@ -53,7 +53,7 @@ local function prime_factors(n) return nil end - while p and n % p ~= 0 + while p and n % p do p = pgen.next() end diff --git a/lua/src/p0001.lua b/lua/src/p0001.lua index b3b56a96..f3c17e85 100644 --- a/lua/src/p0001.lua +++ b/lua/src/p0001.lua @@ -18,18 +18,15 @@ return { solution = function() local answer = 0 - for i = 3,999,3 - do + for i = 3,999,3 do answer = answer + i end - for i = 5,999,5 - do + for i = 5,999,5 do answer = answer + i end - for i = 15,999,15 - do + for i = 15,999,15 do answer = answer - i end diff --git a/lua/src/p0002.lua b/lua/src/p0002.lua index f6926e4f..321ac712 100644 --- a/lua/src/p0002.lua +++ b/lua/src/p0002.lua @@ -18,12 +18,10 @@ return { local a = 1 local b = 2 - while b < 4000000 - do + while b < 4000000 do answer = answer + b - for _ = 1,3,1 - do + for _ = 1,3 do a, b = b, a + b end end diff --git a/lua/src/p0003.lua b/lua/src/p0003.lua index cc9730e6..e7ea1c4d 100644 --- a/lua/src/p0003.lua +++ b/lua/src/p0003.lua @@ -14,8 +14,7 @@ return { local f = fgen.next() local answer = 0 - while f ~= nil - do + while f do answer = f f = fgen.next() end diff --git a/lua/src/p0004.lua b/lua/src/p0004.lua index 72ea166c..d2c4a8f1 100644 --- a/lua/src/p0004.lua +++ b/lua/src/p0004.lua @@ -11,15 +11,12 @@ return { solution = function() local answer = 0 - for v = 101,999,1 - do - for u = 100,(v-1),1 - do + for v = 101,999 do + for u = 100,(v-1) do local p = u * v local ps = tostring(p) - if ps == string.reverse(ps) and p > answer - then + if ps == string.reverse(ps) and p > answer then answer = p end end diff --git a/lua/src/p0006.lua b/lua/src/p0006.lua index 30ca9569..c981efdf 100644 --- a/lua/src/p0006.lua +++ b/lua/src/p0006.lua @@ -21,8 +21,7 @@ return { local sum = 1 local sum_of_squares = 1 - for i = 2,100,1 - do + for i = 2,100 do sum = sum + i sum_of_squares = sum_of_squares + (i * i) end diff --git a/lua/src/p0008.lua b/lua/src/p0008.lua index d150bdee..0a28c2ec 100644 --- a/lua/src/p0008.lua +++ b/lua/src/p0008.lua @@ -53,16 +53,13 @@ return { .. "71636269561882670428252483600823257530420752963450") local answer = 0 - for i = 1,(#str-13),1 - do + for i = 1,(#str-13) do local product = 1 - for j = i,(i+12),1 - do + for j = i,(i+12) do product = product * tonumber(str:sub(j, j)) end - if product > answer - then + if product > answer then answer = product end end diff --git a/lua/src/p0009.lua b/lua/src/p0009.lua index 5cd9b51d..db919876 100644 --- a/lua/src/p0009.lua +++ b/lua/src/p0009.lua @@ -16,20 +16,16 @@ return { solution = function() local c = 3 - while true - do + while true do local c_square = c * c - for b = 2,c,1 - do + for b = 2,c do local b_square = b * b - for a = 1,b,1 - do + for a = 1,b do local a_square = a * a - if a_square + b_square == c_square and a + b + c == 1000 - then + if a_square + b_square == c_square and a + b + c == 1000 then return a * b * c end end diff --git a/lua/src/p0011.lua b/lua/src/p0011.lua index f14ed777..089f257e 100644 --- a/lua/src/p0011.lua +++ b/lua/src/p0011.lua @@ -58,41 +58,33 @@ return { local answer = 0 local tmp - for i = 1,20,1 - do - for j = 1,17,1 - do + for i = 1,20 do + for j = 1,17 do -- horizontal section tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; - if tmp > answer - then + if tmp > answer then answer = tmp end -- vertical section tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; - if tmp > answer - then + if tmp > answer then answer = tmp end end end - for i = 1,17,1 - do - for j = 1,17,1 - do + for i = 1,17 do + for j = 1,17 do -- right diagonal section tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; - if tmp > answer - then + if tmp > answer then answer = tmp end -- left diagonal section tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; - if tmp > answer - then + if tmp > answer then answer = tmp end end diff --git a/lua/src/p0013.lua b/lua/src/p0013.lua index c0425f19..b6ff2582 100644 --- a/lua/src/p0013.lua +++ b/lua/src/p0013.lua @@ -213,25 +213,20 @@ local ten10 = 10000000000 return { solution = function() local arr = { 0, 0, 0 } - for i = 1,100,1 - do - for j = 1,3,1 - do + for i = 1,100 do + for j = 1,3 do arr[j] = arr[j] + numbers[i][j] end - for j = 2,1,-1 - do - if arr[j] > ten18 - then + for j = 2,1,-1 do + if arr[j] > ten18 then arr[j - 1] = math.floor(arr[j - 1] + arr[j] / ten18) arr[j] = arr[j] % ten18 end end end - while arr[1] > ten10 - do + while arr[1] > ten10 do arr[1] = math.floor(arr[1] / 10) end diff --git a/lua/src/p0017.lua b/lua/src/p0017.lua index c342378e..979ddb65 100644 --- a/lua/src/p0017.lua +++ b/lua/src/p0017.lua @@ -16,32 +16,27 @@ -- British usage. local function to_string_len(n) - if n >= 1000 - then + if n >= 1000 then local thousands = to_string_len(math.floor(n / 1000 % 100)) + 8 - if n % 1000 ~= 0 - then + if n % 1000 then thousands = thousands + to_string_len(n % 1000) end return thousands end - if n >= 100 - then + if n >= 100 then local hundreds = to_string_len(math.floor(n / 100 % 10)) + 7 - if n % 100 ~= 0 - then + if n % 100 then hundreds = hundreds + 3 + to_string_len(n % 100) end return hundreds end - if n >= 20 - then + if n >= 20 then local tens_t = { [2] = 6, [3] = 6, @@ -54,8 +49,7 @@ local function to_string_len(n) } local tens = tens_t[math.floor(n / 10)] - if n % 10 ~= 0 - then + if n % 10 then tens = tens + to_string_len(n % 10) end @@ -91,8 +85,7 @@ return { solution = function() local answer = 0 - for x = 1,1000,1 - do + for x = 1,1000 do answer = answer + to_string_len(x) end diff --git a/lua/src/p0028.lua b/lua/src/p0028.lua index 17c6d272..ba2983e1 100644 --- a/lua/src/p0028.lua +++ b/lua/src/p0028.lua @@ -51,8 +51,7 @@ return { solution = function() local answer = 1 - for i = 1,(1000 / 2),1 - do + for i = 1,(1000 / 2) do local start = (2 * i - 1)^2 + 1 answer = answer + range_entry(start, 1, (1 * 2 * i - 1)) answer = answer + range_entry(start, 1, (2 * 2 * i - 1)) diff --git a/lua/src/p0034.lua b/lua/src/p0034.lua index 3b6e29e2..015bb0b2 100644 --- a/lua/src/p0034.lua +++ b/lua/src/p0034.lua @@ -19,17 +19,14 @@ return { solution = function() local answer = 0 - for x = 10,99999,1 - do + for x = 10,99999 do local xs = tostring(x) local sum = 0 - for i = 1,#xs,1 - do + for i = 1,#xs do sum = sum + factorial(tonumber(xs:sub(i, i))) end - if sum == x - then + if sum == x then answer = answer + x end end diff --git a/lua/src/p0076.lua b/lua/src/p0076.lua index 8ad84320..b5bc4034 100644 --- a/lua/src/p0076.lua +++ b/lua/src/p0076.lua @@ -20,19 +20,16 @@ return { local sum = 100 local counts = {} - for i = 2,100,1 - do + for i = 2,100 do counts[i] = 0 end counts[2] = 100 - while counts[100] == 0 - do + while counts[100] == 0 do counts[2] = counts[2] + 2 - if sum >= 100 - then + if sum >= 100 then answer = answer + math.floor((100 + counts[2] - sum) / 2) idx = 2 @@ -42,16 +39,14 @@ return { counts[idx] = counts[idx] + idx sum = counts[idx] - for i = (idx+1),100,1 - do + for i = (idx+1),100 do sum = sum + counts[i] end until sum <= 100 end sum = counts[2] - for i = 3,100,1 - do + for i = 3,100 do sum = sum + counts[i] end end diff --git a/lua/test.lua b/lua/test.lua index ec2c82ec..8a23d137 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -5,20 +5,17 @@ function loadlib(...) local length = select("#", ...) local lib, err = loadfile("src/lib/" .. select(1, ...) .. ".lua") - if not lib - then + if not lib then error("Failed to load lib " .. libname .. ": " .. err) end lib = lib() - if length == 1 - then + if length == 1 then return lib end local ret = {} - for i = 2,length,1 - do + for i = 2,length do local fname = select(i, ...) ret[fname] = lib[fname] end @@ -46,8 +43,7 @@ end local function check_problem(file_name, expected_answer, is_slow, problem_name) print("Starting: " .. file_name) local problem_func = load_problem("src/" .. file_name) - if is_slow and has_luacov - then + if is_slow and has_luacov then return end local start_time = os.clock()