Compare commits

...

4 Commits

Author SHA1 Message Date
pegasust 4d2c3161d2 d6: python 2022-12-06 05:11:35 +00:00
pegasust 053ca4fa25 add partial fennel solution for d5 2022-12-06 04:58:14 +00:00
pegasust 2dd975ea24 d6 init 2022-12-06 04:57:59 +00:00
pegasust c0912bcc1e d5-py: bonehead on data copy-paste 2022-12-05 06:02:53 +00:00
11 changed files with 232 additions and 5 deletions

View File

@ -15,6 +15,7 @@
''; '';
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 = [];
in in
{ {
# Jack of all trades # Jack of all trades

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")

View File

@ -17,6 +17,7 @@ class MoveInsn:
def part1(crates: list[list[str]], insns: list[MoveInsn]) -> str: def part1(crates: list[list[str]], insns: list[MoveInsn]) -> str:
for move in insns: for move in insns:
for _ in range(move.how_many): for _ in range(move.how_many):
# print(crates)
crates[move.to_crate - 1].append(crates[move.from_crate - 1].pop()) crates[move.to_crate - 1].append(crates[move.from_crate - 1].pop())
return "".join([crate[-1] for crate in crates]) return "".join([crate[-1] for crate in crates])
@ -75,3 +76,4 @@ if __name__ == "__main__":
import sys import sys
with open(sys.argv[1], "r") as f: with open(sys.argv[1], "r") as f:
main(f) main(f)

4
2022/d6/.envrc Normal file
View File

@ -0,0 +1,4 @@
if command -v nix-shell &> /dev/null
then
use flake
fi

43
2022/d6/flake.lock Normal file
View File

@ -0,0 +1,43 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1670064435,
"narHash": "sha256-+ELoY30UN+Pl3Yn7RWRPabykwebsVK/kYE9JsIsUMxQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "61a8a98e6d557e6dd7ed0cdb54c3a3e3bbc5e25c",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

61
2022/d6/flake.nix Normal file
View File

@ -0,0 +1,61 @@
{
description = "D4 AOC with Lua!";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { nixpkgs, flake-utils, ... } @ inputs:
flake-utils.lib.eachSystem flake-utils.lib.defaultSystems (sys:
let
overlays = [ ];
pkgs = import nixpkgs { system = sys; overlays = overlays; };
shellHookAfter = ''
echo "The input files should be placed under ./data/{submission,example}.txt"
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 luapkgs.readline ])) ];
in
{
# Jack of all trades
devShell = pkgs.mkShell {
nativeBuildInputs = py_pkgs ++ fennel_pkgs;
shellHook = ''
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 = {
# nix develop ./#lua
# lua = pkgs.mkShell {
# nativeBuildInputs = lua_pkgs;
# shellHook = ''
# echo "> Lua runtime"
# 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;
shellHook = ''
echo "> Python3 runtime"
echo "Run ./run-py.sh to see the output of the solution"
'' + shellHookAfter;
};
};
}
);
}

7
2022/d6/run-py.sh Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
echo "example"
python3 ./src/d6.py ./data/example.txt
echo "submission"
python3 ./src/d6.py ./data/submission.txt

33
2022/d6/src/d6.py Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env python3
from typing import TextIO
def part1(f: TextIO):
s = f.read().strip()
print(f"{s=}")
for i in range(3, len(s)):
if len({c for c in s[i-3:i+1]}) == 4:
return i + 1
return None
def part2(f: TextIO):
s = f.read().strip()
print(f"{s=}")
for i in range(13, len(s)):
if len({c for c in s[i-13:i+1]}) == 14:
return i + 1
return None
def main(fileloc: str):
with open(fileloc, "r") as f:
print(f"{part1(f)=}")
with open(fileloc, "r") as f:
print(f"{part2(f)=}")
if __name__ == "__main__":
import sys
main(sys.argv[1])