Find the shortest path through a grid, returning only the value and not the path.
$ cat 15a.rkt #lang racket (provide example-a-input example-b-input example-b-input-result (struct-out path-map) path-map:start-state read-input (struct-out risk-path) ) (require "aoc.rkt" (prefix-in array: "array.rkt") ) (define (aoc15a) ; Return the risk of the minimal risk path for the first half of the ; fifteenth AoC '21 problem. (find-minimal-path (read-input))) (define (find-minimal-path rmap-data) ; Return the risk associated with the minimal-risk path from the ; start state to the goal state using the given risk-map data. (define path-map (path-map:new rmap-data)) (define (run-through paths) ; Return a list of paths derived from the given path list. Each ; path returned is a valid, one-step extension of one of the given ; paths. ; Using map to run over the paths makes this a more or less ; breadth-first search. (flatten (map (lambda (p) (path-map:extend-path path-map p)) paths))) ; A brute-force search: look through all paths from start to end, ; return the least risky. (let loop ((paths (list (risk-path 0 0 0)))) (let ((paths (run-through paths))) (if (null? paths) (path-map:goal-risk path-map) (loop paths))))) (define read-input (lambda arg? ; Return the risk-map described by the problem input. If no ; argument is given, use the contest problem input; otherwise assume ; the argument is the string representation of a problem input. (let ((inp (car (aoc-read-input (if (null? arg?) 15 (car arg?)))))) (map (lambda (l) (map (lambda (c) (- (char->integer c) (char->integer #\0))) (string->list (car l)))) inp)))) ; The problem only wants the total risk, so the path consists of most ; recent step taken and the risk encountered so far. (struct risk-path (x y risk)) ; The risk-map is a read-only representation of the program input. ; The element at (x, y) in the path-map keeps track of the lowest risk ; of any path reaching (x, y). (struct path-map (risk-map path-map)) (define (path-map:extend-path path-map path) ; Return a list of valid paths one step away from the given path ; according to the given path map (which is changed to reflect the ; valid path extensions). ; A path is valid if it is in within bounds and it's the least risky ; path to a location so far. (foldr (lambda (deltas new-paths) (let* ((pmap (path-map-path-map path-map)) (new-x (+ (risk-path-x path) (car deltas))) (new-y (+ (risk-path-y path) (cdr deltas))) (new-risk (+ (risk-path-risk path) (array:ref (path-map-risk-map path-map) new-x new-y)))) (if (> (array:ref pmap new-x new-y) new-risk) (begin (array:set! pmap new-x new-y new-risk) (cons (risk-path new-x new-y new-risk) new-paths)) new-paths))) '() '((0 . -1) (-1 . 0) (1 . 0) (0 . 1)))) (define (path-map:goal-risk path-map) ; Return the current goal-state risk, which represents the minimal ; risk of a path from the start state to the goal state. No such ; path may exist when this function is called, in which case +inf.0 ; is returned. (let ((pm (path-map-path-map path-map))) (array:ref pm (- (array:cols pm) 1) (- (array:rows pm) 1)))) (define (path-map:goal-state? path-map path) ; Return #t iff the given path ends at the goal state. (let ((pm (path-map-path-map path-map))) (and (= (risk-path-y path) (- (array:cols pm) 1)) (= (risk-path-x path) (- (array:rows pm) 1))))) (define (path-map:new rmap-data) ; Return a new path map based on the given risk-map data. (let ((pmap-data (map (lambda (row) (map (lambda (e) +inf.0) row)) rmap-data))) (path-map (array:new rmap-data) (array:new pmap-data)))) (define (path-map:start-state) ; Return a start state. (risk-path 0 0 0)) (define example-a-input (string-join '("1163751742" "1381373672" "2136511328" "3694931569" "7463417111" "1319128137" "1359912421" "3125421639" "1293138521" "2311944581") "\n")) (define example-a-input-result 40) (define example-b-input (string-join '("11637517422274862853338597396444961841755517295286" "13813736722492484783351359589446246169155735727126" "21365113283247622439435873354154698446526571955763" "36949315694715142671582625378269373648937148475914" "74634171118574528222968563933317967414442817852555" "13191281372421239248353234135946434524615754563572" "13599124212461123532357223464346833457545794456865" "31254216394236532741534764385264587549637569865174" "12931385212314249632342535174345364628545647573965" "23119445813422155692453326671356443778246755488935" "22748628533385973964449618417555172952866628316397" "24924847833513595894462461691557357271266846838237" "32476224394358733541546984465265719557637682166874" "47151426715826253782693736489371484759148259586125" "85745282229685639333179674144428178525553928963666" "24212392483532341359464345246157545635726865674683" "24611235323572234643468334575457944568656815567976" "42365327415347643852645875496375698651748671976285" "23142496323425351743453646285456475739656758684176" "34221556924533266713564437782467554889357866599146" "33859739644496184175551729528666283163977739427418" "35135958944624616915573572712668468382377957949348" "43587335415469844652657195576376821668748793277985" "58262537826937364893714847591482595861259361697236" "96856393331796741444281785255539289636664139174777" "35323413594643452461575456357268656746837976785794" "35722346434683345754579445686568155679767926678187" "53476438526458754963756986517486719762859782187396" "34253517434536462854564757396567586841767869795287" "45332667135644377824675548893578665991468977611257" "44961841755517295286662831639777394274188841538529" "46246169155735727126684683823779579493488168151459" "54698446526571955763768216687487932779859814388196" "69373648937148475914825958612593616972361472718347" "17967414442817852555392896366641391747775241285888" "46434524615754563572686567468379767857948187896815" "46833457545794456865681556797679266781878137789298" "64587549637569865174867197628597821873961893298417" "45364628545647573965675868417678697952878971816398" "56443778246755488935786659914689776112579188722368" "55172952866628316397773942741888415385299952649631" "57357271266846838237795794934881681514599279262561" "65719557637682166874879327798598143881961925499217" "71484759148259586125936169723614727183472583829458" "28178525553928963666413917477752412858886352396999" "57545635726865674683797678579481878968159298917926" "57944568656815567976792667818781377892989248891319" "75698651748671976285978218739618932984172914319528" "56475739656758684176786979528789718163989182927419" "67554889357866599146897761125791887223681299833479") "\n")) (define example-b-input-result 315) (define problem-a-result 592) (module+ main (aoc15a)) (module+ test (require rackunit) (check-equal? (find-minimal-path (read-input example-a-input)) example-a-input-result) (check-equal? (find-minimal-path (read-input example-b-input)) example-b-input-result) (check-equal? (aoc15a) problem-a-result) ) $ raco test 15a.rkt raco test: (submod "15a.rkt" test) 3 tests passed $