From 18bacdfb012042bb31f73bf86b6b0f4af3ba05bb Mon Sep 17 00:00:00 2001 From: arthomnix Date: Tue, 9 Dec 2025 15:02:06 +0000 Subject: [PATCH] Day 9 --- README.md | 2 +- d09/README.md | 16 ++++++++++++---- d09/day9.pi | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 d09/day9.pi diff --git a/README.md b/README.md index 9c7d33c..a5511ab 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ _in which I attempt to use a different language every day_ 6. LaTeX 7. sed 8. Noulith -9. ?? +9. Picat 10. ?? 11. ?? 12. ?? \ No newline at end of file diff --git a/d09/README.md b/d09/README.md index 6ad4b2c..67c8e80 100644 --- a/d09/README.md +++ b/d09/README.md @@ -2,10 +2,18 @@ | | | |-------------------|-------------------| -| Language | ?? | -| Input Method | ?? | -| Output Method | ?? | +| Language | Picat | +| Input Method | File | +| Output Method | stdout | ## Running +Obtain Picat from [here](https://picat-lang.org/download.html) and add it to your PATH, +then +```sh +picat day9.pi +``` -## Notes \ No newline at end of file +## Notes +This solution is based on constraint programming, which is what Picat was designed for. +This is of course nowhere near as fast as a bespoke optimised solution; this code takes +~15 minutes to solve both parts on my laptop. \ No newline at end of file diff --git a/d09/day9.pi b/d09/day9.pi new file mode 100644 index 0000000..f456573 --- /dev/null +++ b/d09/day9.pi @@ -0,0 +1,46 @@ +import util. +import cp. + +read_input(Path, Points) => + Lines = read_file_lines(Path), + Points = new_array(Lines.length), + I = 1, + foreach (Line in Lines) + Points[I] = Line.split(",").map(to_integer).to_array, + I := I + 1 + end. + +area(Ax, Ay, Bx, By, Area) => + Area #= (abs(Ax - Bx) + 1) * (abs(Ay - By) + 1). + +part_one(Points) = Area => + area(Ax, Ay, Bx, By, Area), + matrix_element(Points, I, 1, Ax), + matrix_element(Points, I, 2, Ay), + matrix_element(Points, J, 1, Bx), + matrix_element(Points, J, 2, By), + solve([$max(Area)], [I, J]). + +pairs(L) = [{L[I], L[(I mod L.len) + 1]} : I in 1..L.len]. + +part_two(Points) = Area => + area(Ax, Ay, Bx, By, Area), + matrix_element(Points, I, 1, Ax), + matrix_element(Points, I, 2, Ay), + matrix_element(Points, J, 1, Bx), + matrix_element(Points, J, 2, By), + foreach ({{Fx, Fy}, {Sx, Sy}} in Points.pairs) + (Fx #<= min(Ax, Bx) #/\ Sx #<= min(Ax, Bx)) + #\/ (Fx #>= max(Ax, Bx) #/\ Sx #>= max(Ax, Bx)) + #\/ (Fy #<= min(Ay, By) #/\ Sy #<= min(Ay, By)) + #\/ (Fy #>= max(Ay, By) #/\ Sy #>= max(Ay, By)) + end, + solve([$max(Area)], [I, J]). + +main => + println("Please provide input filename as a command-line argument."). + +main(Args) => + read_input(Args[1], Points), + println(part_one(Points)), + println(part_two(Points)). -- 2.51.2