add partial fennel solution for d5

master
pegasust 2022-12-06 04:58:14 +00:00
parent 2dd975ea24
commit 053ca4fa25
3 changed files with 80 additions and 4 deletions

View File

@ -14,16 +14,16 @@
echo "This problem shares one input between two parts" echo "This problem shares one input between two parts"
''; '';
py_pkgs = [ pkgs.python310 ]; py_pkgs = [ pkgs.python310 ];
lua_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.busted luapkgs.luafilesystem ])) ]; # lua_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.busted luapkgs.luafilesystem ])) ];
fennel_pkgs = [ pkgs.lua.withPackages (luapkgs: [ luapkgs.fennel ]) ]; fennel_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.fennel luapkgs.readline ])) ];
in in
{ {
# Jack of all trades # Jack of all trades
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
nativeBuildInputs = py_pkgs ++ fennel_pkgs; nativeBuildInputs = py_pkgs ++ fennel_pkgs;
shellHook = '' shellHook = ''
echo "> Default runtime. This contains both lua and python3 env" echo "> Default runtime. This contains both fennel and python3 env"
echo "Run ./run-py.sh for Python's output and ./run-lua.sh for Lua's output" echo "Run ./run-py.sh for Python's output and ./run-fnl.sh for Fennel's output"
'' + shellHookAfter; '' + shellHookAfter;
}; };
devShells = { devShells = {
@ -35,6 +35,17 @@
# echo "Run ./run-lua.sh to see the output of the solution" # echo "Run ./run-lua.sh to see the output of the solution"
# '' + shellHookAfter; # '' + shellHookAfter;
# }; # };
# nix develop ./#fennel
fennel = pkgs.mkShell
{
nativeBuildInputs = fennel_pkgs;
shellHook = ''
echo "> Fennel runtime"
echo "Run ./run-fnl.sh to see output of Fennel solution"
'' + shellHookAfter;
};
# nix develop ./#python # nix develop ./#python
python = pkgs.mkShell { python = pkgs.mkShell {
nativeBuildInputs = py_pkgs; nativeBuildInputs = py_pkgs;

8
2022/d5/run-fnl.sh Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env sh
echo "example"
fennel ./src/d5.fnl ./data/example.txt
echo "submission"
fennel ./src/d5.fnl ./data/submission.txt

57
2022/d5/src/d5.fnl Normal file
View File

@ -0,0 +1,57 @@
(local fennel (require :fennel))
(fn pp [x]
(print (fennel.view x)))
(fn get-crates-insns [fileloc]
(let [file (assert (io.open fileloc :r))
contents (file:read "*a")
(crates instrs) ((string.gmatch contents "(.+)\n\n(.+)"))
]
(file:close)
[crates instrs]))
(let [fileloc (. _G.arg 1)
(crates instrs) (get-crates-insns fileloc)
]
;; (pp contents)
;; (print split)
(pp crates)
(pp instrs)
)
; REPL helpers
(tset _G :arg ["./data/example.txt"]) ; provides hint for arg[1] to the REPL
(assert (= (. _G.arg 1) "../data/example.txt"))
(local fennel (require :fennel))
(fn _G.pp [x]
(print (fennel.view x)))
;; (_G.pp _G.arg)
;; REPL sandbox
; Function vs lambda: failure to pass a specific or nil variable
;; (fn nilable [a b c]
;; (print a b c))
;; (nilable 10 12)
;;
;; (lambda non-nilable [a b c]
;; (print a b c))
;; (non-nilable 10 12)
;; (let [(a b) ((string.gmatch "hello\nmy\nrandom\nstranger\n\nin\nthis\nsmall\nworld" "(.+)\n\n(.+)"))]
;; (_G.pp a)
;; (_G.pp b))
;; (let [split ((string.gmatch "hello\nmy\nrandom\nstranger\n\nin\nthis\nsmall\nworld" "(.+)\n"))]
;; (_G.pp split)
;; )
;; (fn overpass [a]
;; (print a)) (overpass "hello" "world")
;;
;; (lambda overpass [a]
;; (print a)) (overpass "hello" "world")