From c63d0ad2e502d532a400c0e04c586b19979e3854 Mon Sep 17 00:00:00 2001 From: Matt Russell Date: Sun, 8 Dec 2024 20:40:45 +0000 Subject: [PATCH] automation --- Aoc2024/Day09/Examples.lean | 2 + Aoc2024/Day09/Main.lean | 19 +++++++++ Aoc2024/Day09/Parser.lean | 12 ++++++ Aoc2024/Day09/Solve.lean | 20 +++++++++ Aoc2024/Day09/Types.lean | 1 + Aoc2024/Day09/inputs/example.txt | 0 Aoc2024/Day09/inputs/input.txt | 0 Taskfile.yml | 73 +++++++++++++++++++------------- add_day_to_lakefile.py | 52 +++++++++++++++++++++++ add_day_to_taskfile.py | 41 ++++++++++++++++++ 10 files changed, 191 insertions(+), 29 deletions(-) create mode 100644 Aoc2024/Day09/Examples.lean create mode 100644 Aoc2024/Day09/Main.lean create mode 100644 Aoc2024/Day09/Parser.lean create mode 100644 Aoc2024/Day09/Solve.lean create mode 100644 Aoc2024/Day09/Types.lean create mode 100644 Aoc2024/Day09/inputs/example.txt create mode 100644 Aoc2024/Day09/inputs/input.txt create mode 100755 add_day_to_lakefile.py create mode 100755 add_day_to_taskfile.py diff --git a/Aoc2024/Day09/Examples.lean b/Aoc2024/Day09/Examples.lean new file mode 100644 index 0000000..1effa2a --- /dev/null +++ b/Aoc2024/Day09/Examples.lean @@ -0,0 +1,2 @@ +def exampleInput := +"..." diff --git a/Aoc2024/Day09/Main.lean b/Aoc2024/Day09/Main.lean new file mode 100644 index 0000000..06308ca --- /dev/null +++ b/Aoc2024/Day09/Main.lean @@ -0,0 +1,19 @@ +import Aoc2024.Day09.Solve +import Aoc2024.CustomMonadLift + +def main : IO Unit := do + IO.println "Day 09" + IO.println "" + IO.println "Part 1" + let exampleInput <- IO.FS.readFile "Aoc2024/Day09/inputs/example.txt" + let puzzleInput <- IO.FS.readFile "Aoc2024/Day09/inputs/input.txt" + IO.println s!"Example: {<- parseAndSolvePart1 exampleInput}" + let answerPart1 <- parseAndSolvePart1 puzzleInput + IO.println s!"Puzzle: {answerPart1}" + assert! (answerPart1 == -1) + IO.println "" + IO.println "Part 2" + IO.println s!"Example: {<- parseAndSolvePart2 exampleInput}" + let answerPart2 <- parseAndSolvePart2 puzzleInput + IO.println s!"Puzzle: {answerPart2}" + assert! (answerPart2 == -1) diff --git a/Aoc2024/Day09/Parser.lean b/Aoc2024/Day09/Parser.lean new file mode 100644 index 0000000..b1875ba --- /dev/null +++ b/Aoc2024/Day09/Parser.lean @@ -0,0 +1,12 @@ +import Aoc2024.Day09.Examples +import Aoc2024.Day09.Types +import Aoc2024.Utils +import Std +open Std.Internal.Parsec.String +open Std.Internal.Parsec + +private def inputParser : Parser (List Int) := sorry + +def parseInput : String -> Except String (List Int) := inputParser.run + +-- #guard parseInput exampleInput == Except.ok 42 diff --git a/Aoc2024/Day09/Solve.lean b/Aoc2024/Day09/Solve.lean new file mode 100644 index 0000000..0c99d93 --- /dev/null +++ b/Aoc2024/Day09/Solve.lean @@ -0,0 +1,20 @@ +import Aoc2024.Utils +import Aoc2024.Day09.Examples +import Aoc2024.Day09.Parser +import Aoc2024.Day09.Types + +-- Part 1 + +private def solvePart1 (input : List Int) : Int := sorry + +def parseAndSolvePart1 (s : String): Except String Int := parseInput s |>.map solvePart1 + +-- #guard parseAndSolvePart1 exampleInput == Except.ok -1 + +-- Part 2 + +private def solvePart2 (input : List Int) : Int := sorry + +def parseAndSolvePart2 (s : String): Except String Int := parseInput s |>.map solvePart2 + +-- #guard parseAndSolvePart2 exampleInput == Except.ok -1 diff --git a/Aoc2024/Day09/Types.lean b/Aoc2024/Day09/Types.lean new file mode 100644 index 0000000..59780fa --- /dev/null +++ b/Aoc2024/Day09/Types.lean @@ -0,0 +1 @@ +import Aoc2024.Utils diff --git a/Aoc2024/Day09/inputs/example.txt b/Aoc2024/Day09/inputs/example.txt new file mode 100644 index 0000000..e69de29 diff --git a/Aoc2024/Day09/inputs/input.txt b/Aoc2024/Day09/inputs/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/Taskfile.yml b/Taskfile.yml index 383c90b..7ef01f9 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,56 +1,71 @@ version: '3' - tasks: default: task --list build: desc: Build sources: - - Aoc2024/**/*.lean + - Aoc2024/**/*.lean generates: - - .lake/build/bin/* + - .lake/build/bin/* cmds: - - lake build + - lake build make-day: desc: Template a day cmds: - - ./make-day.sh {{.CLI_ARGS}} + - ./make-day.sh {{.CLI_ARGS}} + - ./add_day_to_taskfile.py {{.CLI_ARGS}} + - ./add_day_to_lakefile.py {{.CLI_ARGS}} day1: desc: Run day 1 - deps: [build] - cmds: - - .lake/build/bin/day1 + deps: + - build + cmds: + - .lake/build/bin/day1 day2: desc: Run day 2 - deps: [build] - cmds: - - .lake/build/bin/day2 + deps: + - build + cmds: + - .lake/build/bin/day2 day3: desc: Run day 3 - deps: [build] - cmds: - - .lake/build/bin/day3 + deps: + - build + cmds: + - .lake/build/bin/day3 day4: desc: Run day 4 - deps: [build] - cmds: - - .lake/build/bin/day4 + deps: + - build + cmds: + - .lake/build/bin/day4 day5: desc: Run day 5 - deps: [build] - cmds: - - .lake/build/bin/day5 + deps: + - build + cmds: + - .lake/build/bin/day5 day6: desc: Run day 6 - deps: [build] - cmds: - - .lake/build/bin/day6 + deps: + - build + cmds: + - .lake/build/bin/day6 day7: desc: Run day 7 - deps: [build] - cmds: - - .lake/build/bin/day7 + deps: + - build + cmds: + - .lake/build/bin/day7 day8: desc: Run day 8 - deps: [build] - cmds: - - .lake/build/bin/day8 + deps: + - build + cmds: + - .lake/build/bin/day8 + day9: + desc: Run day 9 + deps: + - build + cmds: + - .lake/build/bin/day9 diff --git a/add_day_to_lakefile.py b/add_day_to_lakefile.py new file mode 100755 index 0000000..3c6dc01 --- /dev/null +++ b/add_day_to_lakefile.py @@ -0,0 +1,52 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python -p python3Packages.toml + +import sys +import toml + +def add_day_to_lakefile(file_path, day_number): + try: + with open(file_path, 'r') as file: + lakefile = toml.load(file) + + # Update defaultTargets + default_targets = lakefile.get('defaultTargets', []) + new_target = f"day{day_number}" + if new_target in default_targets: + print(f"Target '{new_target}' already exists in defaultTargets.") + else: + default_targets.append(new_target) + lakefile['defaultTargets'] = default_targets + + # Add new lean_exe entry + lean_exes = lakefile.get('lean_exe', []) + new_exe = { + "name": f"day{day_number}", + "root": f"Aoc2024.Day{day_number:02d}.Main" + } + if any(exe['name'] == new_exe['name'] for exe in lean_exes): + print(f"Lean exe '{new_exe['name']}' already exists.") + else: + lean_exes.append(new_exe) + lakefile['lean_exe'] = lean_exes + + # Write back to the file + with open(file_path, 'w') as file: + toml.dump(lakefile, file) + + print(f"Day {day_number} added successfully.") + except Exception as e: + print(f"Error: {e}") + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python add_day_to_lakefile.py ") + sys.exit(1) + + file_path = sys.argv[1] + try: + day_number = int(sys.argv[2]) + add_day_to_lakefile(file_path, day_number) + except ValueError: + print("Error: N must be an integer.") + sys.exit(1) diff --git a/add_day_to_taskfile.py b/add_day_to_taskfile.py new file mode 100755 index 0000000..dd7be00 --- /dev/null +++ b/add_day_to_taskfile.py @@ -0,0 +1,41 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i python -p python3Packages.pyyaml + +import sys +import yaml + +def add_day_to_taskfile(file_path, day_number): + try: + with open(file_path, 'r') as file: + taskfile = yaml.safe_load(file) + + day_task_name = f"day{day_number}" + if day_task_name in taskfile['tasks']: + print(f"Task '{day_task_name}' already exists in {file_path}.") + return + + taskfile['tasks'][day_task_name] = { + 'desc': f"Run day {day_number}", + 'deps': ['build'], + 'cmds': [f".lake/build/bin/day{day_number}"] + } + + with open(file_path, 'w') as file: + yaml.dump(taskfile, file, default_flow_style=False, sort_keys=False) + + print(f"Task '{day_task_name}' added successfully.") + except Exception as e: + print(f"Error: {e}") + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python add_day_to_taskfile.py ") + sys.exit(1) + + file_path = sys.argv[1] + try: + day_number = int(sys.argv[2]) + add_day_to_taskfile(file_path, day_number) + except ValueError: + print("Error: N must be an integer.") + sys.exit(1)