diff --git a/README.md b/README.md index a5511ab..be20c90 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,6 @@ _in which I attempt to use a different language every day_ 7. sed 8. Noulith 9. Picat -10. ?? +10. Hy 11. ?? 12. ?? \ No newline at end of file diff --git a/d10/.gitignore b/d10/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/d10/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/d10/README.md b/d10/README.md index 2371ddf..823bc07 100644 --- a/d10/README.md +++ b/d10/README.md @@ -2,10 +2,18 @@ | | | |-------------------|-------------------| -| Language | ?? | -| Input Method | ?? | -| Output Method | ?? | +| Language | Hy | +| Input Method | File | +| Output Method | stdout | ## Running +Requires `hyrule` and `scipy` in addition Hy itself (install via pip). -## Notes \ No newline at end of file +Then +```sh +hy day10.hy +``` + +## Notes +If I had known today would be an integer linear programming problem I wouldn't have used Picat yesterday... +At least this is a Python-based language so I could use scipy.optimize. \ No newline at end of file diff --git a/d10/day10.hy b/d10/day10.hy new file mode 100644 index 0000000..5feb783 --- /dev/null +++ b/d10/day10.hy @@ -0,0 +1,55 @@ +(require hyrule.argmove *) +(require hyrule.anaphoric *) +(import scipy.optimize [milp LinearConstraint]) +(import itertools [combinations-with-replacement]) +(import sys [argv]) + +(defn schematic-to-int [schematic] + (sum (ap-map (** 2 (int it)) schematic))) + +(defn lights-to-int [lights] + (-> (cut lights 1 -1) + (.replace "." "0") + (.replace "#" "1") + (cut None None -1) + (int 2))) + +(defn parse-num-list [nums] + (list (map int (.split (cut nums 1 -1) ",")))) + +(defn read-input [filename] + (->> + (with [f (open filename "r")] + (.readlines f)) + (ap-map (let [line (.split (.strip it) " ") + lights (lights-to-int (get line 0)) + schematics (list (map parse-num-list (cut line 1 -1))) + joltages (parse-num-list (get line -1))] + #(lights schematics joltages))) + (list))) + +(defn get-p1 [line] + (setv [lights schematics _] line) + (setv n 0) + (while True + (+= n 1) + (setv correct (ap-map + (= lights (ap-reduce (^ it acc) it)) + (combinations-with-replacement (map schematic-to-int schematics) n))) + (when (any correct) (break))) + n) + +(defn get-p2 [line] + (setv [_ schematics joltages] line) + (setv c (list (ap-map (let [joltage it] + (list (ap-map (in joltage it) schematics))) + (range (len joltages))))) + (setv constr (LinearConstraint c :lb joltages :ub joltages)) + (setv a (* [1] (len schematics))) + (-> (. (milp a :integrality a :constraints constr) x) + (sum) + (int))) + +(setv inp (read-input (get argv 1))) +(print (sum (map get-p1 inp))) +(print (sum (map get-p2 inp))) \ No newline at end of file