summaryrefslogtreecommitdiffstats
path: root/2021/15/rubbish.tl
diff options
context:
space:
mode:
Diffstat (limited to '2021/15/rubbish.tl')
-rw-r--r--2021/15/rubbish.tl99
1 files changed, 99 insertions, 0 deletions
diff --git a/2021/15/rubbish.tl b/2021/15/rubbish.tl
new file mode 100644
index 0000000..e483dc1
--- /dev/null
+++ b/2021/15/rubbish.tl
@@ -0,0 +1,99 @@
+
+(defmeth board depth-search (bo mincost cur goal gpath lpath depth)
+ (unless (or (memqual cur lpath)
+ (memqual cur gpath)
+ (minusp cur.x)
+ (minusp cur.y)
+ (>= cur.x bo.w)
+ (>= cur.y bo.h))
+ #;(prinl ^(trying ,cur))
+ (push cur lpath)
+ (if (or (zerop (pdec depth)) (equal cur goal))
+ (let ((cost (sum lpath bo)))
+ (when (or (null mincost.val) (< cost mincost.val))
+ #;(prinl ^(,cost ,lpath))
+ (set mincost.val cost
+ mincost.path lpath)))
+ (progn
+ bo.(depth-search mincost (new (coord cur.x (succ cur.y)))
+ goal gpath lpath depth)
+ bo.(depth-search mincost (new (coord (succ cur.x) cur.y))
+ goal gpath lpath depth)
+ bo.(depth-search mincost (new (coord cur.x (pred cur.y)))
+ goal gpath lpath depth)
+ bo.(depth-search mincost (new (coord (pred cur.x) cur.y))
+ goal gpath lpath depth)))))
+
+(defmeth board search (bo gmincost cur goal gpath lookahead)
+ (unless (memqual cur gpath)
+ (if (equal cur goal)
+ (set gmincost.val (+ (sum gpath bo) (- [bo cur] [bo goal]))
+ gmincost.path gpath)
+ (let ((lmincost (new cost)))
+ bo.(depth-search lmincost cur goal gpath nil lookahead)
+ (whenlet ((next [lmincost.path -2]))
+ (prinl next)
+ bo.(search gmincost next goal (cons cur gpath) lookahead))))))
+
+(defun solve-one (: (name :))
+ (let ((bo (read-input name))
+ (mincost (new cost)))
+ bo.(search mincost
+ (new (coord 0 0))
+ (new (coord (pred bo.w) (pred bo.h)))
+ nil
+ 13)
+ mincost))
+
+(defmeth board fill-costs (bo lim)
+ (let ((goal (new (coord (pred bo.w) (pred bo.h)))))
+ (labels ((rec (coo path xmin ymin xmax ymax)
+ (unless (or (memqual coo path)
+ (< coo.x xmin)
+ (< coo.y ymin)
+ (>= coo.x bo.w)
+ (>= coo.y bo.h))
+ (push coo path)
+ (placelet ((memo [bo.memo coo]))
+ (or memo
+ (if (equal coo goal)
+ (set memo (new cost
+ coo coo
+ val 0
+ path (list goal))))
+ (iflet ((next (flow
+ (vec (rec (new (coord coo.x (succ coo.y)))
+ path xmin ymin xmax ymax)
+ (rec (new (coord (succ coo.x) coo.y))
+ path xmin ymin xmax ymax)
+ (rec (new (coord coo.x (pred coo.y)))
+ path xmin ymin xmax ymax)
+ (rec (new (coord (pred coo.x) coo.y))
+ path xmin ymin xmax ymax))
+ (remq nil)))
+ (min [find-min next : .val]))
+ (set memo
+ (new cost
+ coo coo
+ val (+ [bo coo] min.val)
+ path (cons coo min.path)))))))))
+ (each ((k (max bo.w bo.h)..0))
+ (each ((y bo.h..k))
+ (let ((coo (new (coord k y)))
+ (xmin (max 0 (- k lim)))
+ (xmax (min bo.w (+ k lim))))
+ (prinl coo)
+ (set [bo.memo coo]
+ (rec coo nil xmin (max 0 (- y lim)) xmax (min bo.h (+ y lim))))))
+ (each ((x bo.w..k))
+ (let ((coo (new (coord x k)))
+ (ymin (max 0 (- k lim)))
+ (ymax (min bo.h (+ k lim))))
+ (prinl coo)
+ (set [bo.memo coo]
+ (rec coo nil (max 0 (- x lim)) ymin (min bo.w (+ x lim)) ymax))))))))
+
+(defun solve-one-bad (: (name :))
+ (let ((bo (read-input name)))
+ bo.(fill-costs 0)
+ [bo.memo (new (coord 0 0))]))