commit 6828b39aa31ce7b173abd51009256229492b3743 Author: Nettika Date: Sun Dec 1 14:47:03 2024 -0800 Initial commit diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d6b45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +__pycache__/ +.direnv/ +.env +.venv/ +*.egg-info \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..daa1621 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1733016324, + "narHash": "sha256-8qwPSE2g1othR1u4uP86NXxm6i7E9nHPyJX3m3lx7Q4=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "7e1ca67996afd8233d9033edd26e442836cc2ad6", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1b8773b --- /dev/null +++ b/flake.nix @@ -0,0 +1,26 @@ +{ + description = "Advent of Code 2024"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05"; + }; + + outputs = { nixpkgs, ... }: + let + pkgs = import nixpkgs { + system = "x86_64-linux"; + }; + in + { + devShells.x86_64-linux.default = pkgs.mkShell { + packages = with pkgs; [ + python313 + python313Packages.venvShellHook + ]; + venvDir = "./.venv"; + postVenvCreation = '' + pip install --editable . + ''; + }; + }; +} diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..143b244 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,5 @@ +[project] +name = "advent-of-code-2024" +version = "0.0.0" +description = "Advent of Code 2024" +dependencies = ["advent"] diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..a4ed777 --- /dev/null +++ b/readme.md @@ -0,0 +1,11 @@ +# Advent of Code 2024 Solvers + +https://adventofcode.com/2024 + +## Usage + +Install python 3.13 or higher. + +``` +python solve.py +``` diff --git a/solve.py b/solve.py new file mode 100644 index 0000000..b4487b1 --- /dev/null +++ b/solve.py @@ -0,0 +1,69 @@ +import sys +from time import time +from importlib import import_module + +from advent.errors import InvalidSessionIDError, PuzzleNotFoundError +from advent.functions import get_puzzle_input + + +def usage(): + print("Usage: python solve.py ") + + +def main(): + if len(sys.argv) != 2: + usage() + sys.exit(1) + + try: + day = int(sys.argv[1]) + except ValueError: + print("Day must be an integer") + usage() + sys.exit(1) + + try: + print(f"Loading 2024, day {day} solver") + mod = import_module(f"puzzles.{day}") + except ModuleNotFoundError: + print(f"Solver for day {day} not found") + sys.exit(1) + + try: + print("Fetching puzzle input...") + puzzle_input = get_puzzle_input(2024, int(day)) + except (PuzzleNotFoundError, InvalidSessionIDError) as exc: + print(exc) + sys.exit(1) + + try: + print("Testing part 1 solution...") + start = time() + assert mod.solve_p1(mod.test_input) == mod.test_solution_p1 + print(f"Test passed in {time() - start:.3f} seconds") + + print("Solving part 1...") + start = time() + solution_p1 = mod.solve_p1(puzzle_input) + print(f"Part 1 solution: {solution_p1} ({time() - start:.3f} seconds)") + + print("Testing part 2 solution...") + start = time() + assert mod.solve_p2(mod.test_input) == mod.test_solution_p2 + print(f"Test passed in {time() - start:.3f} seconds") + + print("Solving part 2...") + solution_p2 = mod.solve_p2(puzzle_input) + print(f"Part 2 solution: {solution_p2} ({time() - start:.3f} seconds)") + + except NotImplementedError: + print("Puzzle solver is incomplete. Exiting") + sys.exit(1) + + except exc: + print("Error:", exc) + sys.exit(1) + + +if __name__ == "__main__": + main()