diff --git a/2022/d5/flake.nix b/2022/d5/flake.nix index e986fc0..3faf300 100644 --- a/2022/d5/flake.nix +++ b/2022/d5/flake.nix @@ -14,16 +14,16 @@ echo "This problem shares one input between two parts" ''; py_pkgs = [ pkgs.python310 ]; - lua_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.busted luapkgs.luafilesystem ])) ]; - fennel_pkgs = [ pkgs.lua.withPackages (luapkgs: [ luapkgs.fennel ]) ]; + # lua_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.busted luapkgs.luafilesystem ])) ]; + fennel_pkgs = [ (pkgs.lua.withPackages (luapkgs: [ luapkgs.fennel luapkgs.readline ])) ]; in { # Jack of all trades devShell = pkgs.mkShell { nativeBuildInputs = py_pkgs ++ fennel_pkgs; shellHook = '' - echo "> Default runtime. This contains both lua and python3 env" - echo "Run ./run-py.sh for Python's output and ./run-lua.sh for Lua's output" + echo "> Default runtime. This contains both fennel and python3 env" + echo "Run ./run-py.sh for Python's output and ./run-fnl.sh for Fennel's output" '' + shellHookAfter; }; devShells = { @@ -35,6 +35,17 @@ # echo "Run ./run-lua.sh to see the output of the solution" # '' + 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 python = pkgs.mkShell { nativeBuildInputs = py_pkgs; diff --git a/2022/d5/run-fnl.sh b/2022/d5/run-fnl.sh new file mode 100755 index 0000000..fb8f971 --- /dev/null +++ b/2022/d5/run-fnl.sh @@ -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 + diff --git a/2022/d5/src/d5.fnl b/2022/d5/src/d5.fnl new file mode 100644 index 0000000..a597c64 --- /dev/null +++ b/2022/d5/src/d5.fnl @@ -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") +