From f8b314b156e4d795fd58176a0327b0b8d58031bd Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sat, 24 Dec 2022 00:08:51 -0800 Subject: [PATCH] Day 24 lol --- README.md | 1 + input/24.txt | 27 ++++++++++++++++ src/24.rkt | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 input/24.txt create mode 100644 src/24.rkt diff --git a/README.md b/README.md index b046b9b..a3bcf9f 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,4 @@ Approximate execution times of the hardest problems, compiled with `raco exe`. | 16 | 20.6 | | 19 | 75.7 | | 23 | 14.0 | +| 24 | 747 | diff --git a/input/24.txt b/input/24.txt new file mode 100644 index 0000000..d66380d --- /dev/null +++ b/input/24.txt @@ -0,0 +1,27 @@ +#.######################################################################################################################## +#.v^<>^^.<^^v>>>.<^<vv>>^<<.v<^v^<^vv.v^^.>^><^<.<^v<>.v>.vvv.v>^^<^>.<<^<^<^>v.^^<# +#.^^><..<>v<^v><<>><>v.>v.^^<<.v.^<^<<^<>^v>v>><>v..v^<^.v><>^>>v^<v.v><^v# +#>>v>v<^>^^^^vv^^<^.^^>^<^vv>^v>><<^^<.>^>v<^v<.v.^<^^v.<.>.<^>v^>.v>>^vv^v.>>>v<<<<<<<.^>.>vvv>># +#<.vv>.><<.<^v.>v^v>>><<^vv>^v^>.>>><^>.v^^vv<>v>^v.>>v^>..v<>^v.^>><.vv><^...<>^^>v>vvv>^<^<>v<^^^<# +#>^.^>vvv>vv>^^vv>v^^v>vv>v^v>^>vvvv^>.v^<^>vvv<>vv<^v<.<^v^>^vv^<><>.^.^>><<^^>^>>>v><^^>>v^v.<^vv^<^v<># +#><^>>^v>^<<<^^<><^<^<<<>^v>^.>v>>^^>^>><>.<<>>>^..^^<.^^>>v.><><>v^><# +#>>^.<^^v.>v.vv^<<.v^v.><>v<^^<<.>v>v>v>^v>.<>v<><<^v><^>><^v>v^v>^vvv>>>..>^>>.><>.^^v.><^^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>v^.^>vv<<^>v<><.^^v<>vv>v^v<><^>>><# +#><>^v>v<>>>v<>^v^^>^v^>^^v>^^v><<^v>>vv<^><>^>^^>>v^^<^.^>v^vv^<.v>^v^.v^><><>>v.v^vv^^>^>^>^>v>^v..<# +#>v<^^<^>.<>v>v><^^v<^^>>^>^<^^>>>^v<^>v>^>v^v>^.<vv<^<.><^><<^>v><^^<^v>v^>><^vvv^>>v>>vv>v^>^><<# +#<^.>v.^><^...v^><<^^><^>^^vv>^v>v^^>^^..<^<<<>.><<>^vvvv>v^><.^^>>v>>>^>>^<^<^vvv>># +#><<<^^.^<.^.^^<>^.vv^^^^>v^>>v<<<.v><<^>>v><.v>v^^v^v<^>v^.<>^.^><>^v><>^<# +#<.<>.>v^><<>^^<^>^v>>>v>v><^v>>^<>>.vv>>^<^vv.>>>.>^v<^>v<^^>v.># +#<>^^<>>^v>v<<>^.>>^^^<^vv^<^<.<<>v>^^<^v><><>.<>.^><<^^^>^vv><>v.<>v<.>>^>vv^v..>^^<^^.><>^><# +########################################################################################################################.# diff --git a/src/24.rkt b/src/24.rkt new file mode 100644 index 0000000..b5e745c --- /dev/null +++ b/src/24.rkt @@ -0,0 +1,88 @@ +#lang curly-fn racket + +(require "../lib.rkt" + data/queue) + +(define test + (list "#.######" + "#>>.<^<#" + "#.<..<<#" + "#>v.><>#" + "#<^v^^>#" + "######.#")) + +(define input (problem-input 24)) + +(define blizzards + (for/fold ([blizzards (make-immutable-hash)]) + ([row input] + [y (in-naturals)]) + (for/fold ([blizzards blizzards]) + ([col (string->list row)] + [x (in-naturals)]) + (if ((or/c #\> #\< #\^ #\v) col) + (hash-update blizzards `(,x ,y) #{cons col %} '()) + blizzards)))) + +(define xmax (- (string-length (first input)) 2)) +(define ymax (- (length input) 2)) +(define start '(1 0)) +(define end `(,xmax ,(add1 ymax))) + +(define (show blizzards) + (for ([y (in-inclusive-range 1 ymax)]) + (for ([x (in-inclusive-range 1 xmax)]) + (display (first (hash-ref blizzards (list x y) '(#\.))))) + (newline))) + +(define (step blizzards) + (for*/fold ([blizzards* (make-immutable-hash)]) + ([(xy bs) blizzards] + [b bs]) + (match (list xy b) + [`((,x ,y) #\>) + (define x* (if (= x xmax) 1 (add1 x))) + (hash-update blizzards* `(,x* ,y) #{cons #\> %} '())] + [`((,x ,y) #\<) + (define x* (if (= x 1) xmax (sub1 x))) + (hash-update blizzards* `(,x* ,y) #{cons #\< %} '())] + [`((,x ,y) #\v) + (define y* (if (= y ymax) 1 (add1 y))) + (hash-update blizzards* `(,x ,y*) #{cons #\v %} '())] + [`((,x ,y) #\^) + (define y* (if (= y 1) ymax (sub1 y))) + (hash-update blizzards* `(,x ,y*) #{cons #\^ %} '())]))) + +(define (clears blizzards x y) + (filter #{match-let ([(and xy `(,x ,y)) %]) + (or (and (<= 1 x xmax) + (<= 1 y ymax) + (empty? (hash-ref blizzards xy '()))) + (equal? xy start) + (equal? xy end))} + `((,x ,y) + (,(add1 x) ,y) + (,(sub1 x) ,y) + (,x ,(add1 y)) + (,x ,(sub1 y))))) + +(define (wayfind blizzards start end) + (define Q (make-queue)) + (enqueue! Q `(,start ,0 ,blizzards)) + (let loop ([seen (set)]) + (match (dequeue! Q) + [`(,xy ,t ,blizzards) #:when (equal? xy end) (values blizzards t)] + [`((,x ,y) ,t ,_) #:when (set-member? seen `(,x ,y ,t)) (loop seen)] + [`((,x ,y) ,t ,blizzards) + (define blizzards* (step blizzards)) + (for ([clear (clears blizzards* x y)]) + (enqueue! Q `(,clear ,(add1 t) ,blizzards*))) + (loop (set-add seen `(,x ,y ,t)))]))) + +(define-values (part1 part2) + (let*-values ([(blizzards t1) (wayfind blizzards start end)] + [(blizzards t2) (wayfind blizzards end start)] + [(blizzards t3) (wayfind blizzards start end)]) + (values t1 (+ t1 t2 t3)))) + +(show-solution part1 part2) \ No newline at end of file