From c11b756d3885ab2d213b3c543e234ff200e858f1 Mon Sep 17 00:00:00 2001 From: pegasust Date: Sun, 4 Dec 2022 08:28:40 +0000 Subject: [PATCH] lua solution is in --- 2022/d4/d4.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2022/d4/flake.nix | 2 +- 2022/d4/run-lua.sh | 8 ++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 2022/d4/d4.lua create mode 100755 2022/d4/run-lua.sh diff --git a/2022/d4/d4.lua b/2022/d4/d4.lua new file mode 100644 index 0000000..23138b9 --- /dev/null +++ b/2022/d4/d4.lua @@ -0,0 +1,62 @@ +-- What we've learn: +-- - [x] Open a file as read (same signature as python's open([, mode])) +-- - [x] Read file line-by-line +-- - [x] Lua's iterator can be a generator function +-- - [x] string -> number with tonumber() +-- - [x] Syntactic sugar on collection length (or any that implements :len()): # +-- - [x] `print(..args)`; printing a nil yields an empty space. print also formats as a fancy table +-- ``` +-- > print(1,2,3,4) +-- 1 2 3 4 +-- > print(100, 200, 300, 400) +-- 100 200 300 400 +-- ``` +-- - [x] CLI arguments are globally `arg`. arg[0] is the location of script, arg[1] is the first cli arg +-- - [x] There is no loop-`continue` in Lua + +local function main(file_loc) + counter = 0 + for group in io.lines(file_loc) do + if #group > 0 then -- a better way is to strip the string first, idk how to do this, though + -- despite the fact that we establish the token consists of digits + -- Lua parses the token as "string" type. + time_matches = group:gmatch("%d+") + l_begin = tonumber(time_matches()) + l_end = tonumber(time_matches()) + r_begin = tonumber(time_matches()) + r_end = tonumber(time_matches()) + -- check if one elf's time is contained within the other + -- 4 9 10 97 + if (l_begin <= r_begin and r_end <= l_end) or + (r_begin <= l_begin and l_end <= r_end) then + counter = counter + 1 + end + end + end + print("part 1 " .. counter) -- print("part 1", counter) works as well. Guess it's var-args + + + counter = 0 + for group in io.lines(file_loc) do + if #group > 0 then -- a better way is to strip the string first, idk how to do this, though + time_matches = group:gmatch("%d+") + l_begin = tonumber(time_matches()) + l_end = tonumber(time_matches()) + r_begin = tonumber(time_matches()) + r_end = tonumber(time_matches()) + + -- check if one elf's time is contained within the other + if (l_begin >= r_begin and l_begin <= r_end) or -- l_begin within r's range + (r_begin >= l_begin and r_begin <= l_end) or -- r_begin within l's range + (l_end >= r_begin and l_end <= r_end) or -- l_end within r's range + (r_end >= l_begin and r_end <= l_end) -- r_end within l's range + then + counter = counter + 1 + end + end + end + print("part 2 " .. counter) +end + +main(arg[1]) + diff --git a/2022/d4/flake.nix b/2022/d4/flake.nix index aa68136..89f28be 100644 --- a/2022/d4/flake.nix +++ b/2022/d4/flake.nix @@ -13,7 +13,7 @@ echo "The input files should be placed under ./data/{submission,example}.txt echo "This problem shares one input between two parts" ''; - py_pkgs = [ pkgs.python39 ]; + py_pkgs = [ pkgs.python310 ]; lua_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.busted luapkgs.luafilesystem ])) ]; in { diff --git a/2022/d4/run-lua.sh b/2022/d4/run-lua.sh new file mode 100755 index 0000000..f6a1306 --- /dev/null +++ b/2022/d4/run-lua.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh + +echo "example" +lua d4.lua "./data/example.txt" + +echo "submission" +lua d4.lua "./data/submission.txt" +