Day 18: Partial progress.

This commit is contained in:
Jonathan Chan 2019-12-17 23:53:40 -08:00
parent 10c0b5bf28
commit 5d4d9686b8
3 changed files with 167 additions and 4 deletions

81
input/18.txt Normal file
View File

@ -0,0 +1,81 @@
#################################################################################
#.............#..o..........#...........#...............#b......#...#.........#.#
#.#######.#.#.#.#.#########.###.#######.###.#########.###.###.###.#.#C#####.#.#.#
#.#...A.#.#.#.#.#.#w....#.#...#..e#.....#...#.....#...#...#...#...#.#.....#.#k..#
#.#.###.###.#.###.###.#.#.###.#####.###.#.###.#####.###.###.###.###.#####.#.###.#
#.#...#.....#...#.#...#.....#.....#...#.#...#.#...#.#...#.#.....#.#.....#.#.#...#
#.###.#########.#.#.#######.#####.###.#.###.#.#.#.#.#.###.#######.###.#.#.#.#####
#...#.#...........#.......#.....#.....#.#...#...#.#.#.......#.....#...#.#.#.#...#
###.#.###.#############.#.#############.#.###.###.#P#######.###.#.#.###.#.#.#.#.#
#...#...#.#.....#.....#.#.....#...#.....#.#.....#.......#...#...#.#.#...#.#...#.#
#.#####.#.#.###.#.###.#######.#.#.#.#####.###############.###.#####.#.###.#####.#
#.....#.#.#.#.#.#...#.#.....#.#.#.#.....#.......#.......#.#.......#.#...#.#...#.#
###.###.#.#.#.#.###.#.#.###.#.#.#.#####.#.#####.#.#####.#.#######.#.###.#.#.###.#
#...#...#.#...#.#...#.#.#...#...#.....#.#.#.#...#.#.#...#...#.....#.#...#.#.#...#
#.###.#######.#.#.###.#.###.#.#####.###.#.#.#.#.#.#.#.#####.#.###.#D###.#.#.#.###
#...#.....#...#.....#.#...#.#.#...#.#...#...#.#.#...#.....#.#.#.X.#...#.#...#...#
###.#####.#.#####.###.###.#.###.#.#.#.#######.#.###.#####.#.#.###.###.#####.###.#
#.#.....#...#...#.#...#.#.#.#...#.#.#...#.....#.#.#.#...#.#.#...#...#...#...#...#
#.#####.#####.#.###.#.#.#.#.#.###.#.###.#.#####.#.#.#.#.#.#.###.#.#####.#.###.###
#.#...#.#.....#.....#...#.#...#...#.....#.#.....#.#.#.#.#.......#.#...#.....#...#
#.#.#.#.#.###.#########.#.#####.#######.#.#.#####.#.###.#########.#.#.#########.#
#...#...#...#.#...#...#.#.#...#.......#.#.#.....#.#.#.....#...#...#.#.........#i#
#.#####.###.#.#.#.#.#####.#.#########.#.#.#####.#.#.#.#####.#######.#########.#.#
#.#...#...#.#.#.#...#.....#.........#.#.#.....#...#...#n..#..f..#.Z.#...W...#.#.#
#K#Y#.#.###.#.#.#####.#####.###.#####.#.#####.#####.###.#.#####.#.###.#.#####.#.#
#.#.#.#.#...#.#.#.#...#...#.#.#.#...#.#.#.....#.....#...#.......#.#.#.#......h#.#
#.#.#.###.#.###.#.#.#.###.#.#.#.#.#.#.#.#.#J###.#####.###########.#.#.#########.#
#.#.#.....#.#...#...#...#...#.#.#.#...#.#.#.#...#...#.....#...#g..#.#.#...#.....#
###.#######.#.###.#####.#.###.#.#.#######.###.#####.#####.#.#.#.###.#.#.###.###.#
#...#.....#.#.#...#.....#.....#.#.......#...#.#.......#.#...#...#.....#.....#...#
#.#####.###.#.#.###.#########.#.#######.#.#.#.###.###.#.#########.#####.#####.###
#.....#.....#d#...#.#...#...#.#.......#.#.#.#...#...#.#.......#.#.#...#...#.#...#
#.###.#.#####.###.#.#.#.#.#.#.###.#####.###.###.###.#.###.###.#.#.###.###.#.###.#
#.#...#...#.#.#...#.#.#.#.#...#.#.....#.#...#.#.....#...#.#.#...#...#.....#.#...#
#.#.#####.#.#.#####.#.#.#.#####.#####.#.#.#.#.#########.#.#.###.###.#####.#.#.###
#.#.....#.#.#...#...#.#.#...#.......#.#.#.#.......#...#...#...#...#.....#.#.#...#
#.#####V#.#.###.#.###.#.###.#.#####.#.#.#.#######.#.#.#####.#.###.#####.#.#U###.#
#.....#.#.....#...#...#...#.#.....#.#...#...#.....#.#.......#.#...#..v#.#.....#.#
#####.#.###########.#######.#####.#.#####.#.#######.#####.#####.#####.#.#######.#
#.....#...........................#.......#.............#.........R...#l........#
#######################################.@.#######################################
#.............#......r..#.......#...........#.........#...#.......#.....#.#.....#
#.###########.#######.###.#.###.#.#####.#.###.#######.#.#.#.###.###.#.#.#.#.#.###
#...#.......#.......#.....#.#.#...#...#.#.#...#.....#...#.#...#.....#y#...#.#...#
###.#.###.#.#######.#.#####.#.#######.#.#.#.###.###.#####.###.#######.###.#.###Q#
#...#.#...#.#.......#.#.....#.......#.#.#...#...#...#...#...#...#...#...#.#.#.#.#
#.###.#.#.###.#######.#.#######.###.#.#.#.###.#####.#.#.###.###.#.#.###.#.#.#.#.#
#.#...#.#.#.........#.#...#.....#.....#.#.#...#...#...#.#.#.....#.#.#...#.#...#.#
#.#.###.###.#######.#####.#.#.#########.#.###.#.#.#####.#.#######.#.#.#######.#.#
#q#...#.....#.....#.....#.#.#.#...#.....#...#...#.#...#.........#.#.#.#.......#.#
#.#######.###.###.#####.#.#.###.#.#.#######.#.###.#.#.#########.#.#.#.#.#######.#
#.#.....#...#.#.#.#...#.#.#.#...#.#.....#..j#.#...#.#.....#.....#.#.#.....#.....#
#.#.###.#####.#.#.###.#.#.#.#.###.#####.#.###.#.###.#####.###.###.#.#######.###.#
#.#.#.#...#...#.#.#...#...#.#...#.......#.#.#.#.....#...#...#...#.#.......#...#.#
#.#L#.###.#.###.#.#.#######.###.#########.#.#.#########.###.###.#.#######.###S#.#
#.#...#.#.#.#...#.#.#...#.......#...#...#.#.............#.#.#.#.#.#.....#...#.#.#
#.###.#.#.#.###.#.#.#.#.#.#######.#.#.#.#.#.###########.#.#.#.#.#.###.#.###.#.#.#
#...#...#...#...#.#.#.#.........#.#.#.#.#.#.#.......#.#.#.#.#...#...#.#...#.#.#.#
#.#.###.#####.#.#H#.#.#########.#.#.#N#.#.#.#.#####.#.#.#.#.#######.###.#.#.#.#.#
#.#.#...#.....#.#.#.....#...#...#.#...#.#.#.#.#...#.#.....#.#.....#...#.#.#...#.#
###.#.###.#.#####.#.#####.#.#.###.#####.#.###.#.#.#.#.#####.#.###.###.#.#######.#
#...#...#.#...#...#.#.T...#.#.#.F.#.#...#.....#.#.#.#...#...#.#...#...#...#...#.#
#.#####.#.###.#.###########.###.###.#.#########.#.#.###.#.#.###.###.###.#.#.#.#.#
#.....#.#.#u..#..z........#....t#.G...#.#.......#.#.#...#.#...#.#...#...#...#...#
###.#.#.###.###########.#########.#####.#.#####.#.#.#####.#.###.#.#######.#######
#...#s#...#.....#.....#.........#.#.....#.....#.#.#.....#.#.#...#.......#...#...#
#.###.###.###.#.#.###.#######.###.#.###########.#######.#.#.#.###.#####.#####.#.#
#.#.....#...#.#.....#.......#.....#.....#.....#.......#.#.#.#...#.#...#...#...#.#
#.#########.###############.###########.#.###.#####.###.#.#####.#.#.#####.#.###.#
#.#x......#.#...#...........#...........#.#.........#...#.#.....#...#.....#.#...#
#.#.#####.#I#.#.#.#########.#####.#####.#.###########.###.#.#######.#.#####.#.###
#.#...#...#...#...#.....#.#.#...#...#...#.............#...#.....#.#.#.......#.O.#
#.###.#.#############.#.#.#.#.#.###.#.###.###############.#####.#.#.#######.###.#
#...#.#.....#...#.....#...#.#.#.....#.#.#.#...#...#.....#..m#...#...#.....#.#...#
#.###.###.#.#.#.###.#######.#.#######.#.#.#.#.#.#.#.###.###.#.#######.###.#.#.###
#.#...#.#.#...#.....#.....#...#.....#...#.#.#...#.#...#.#...#.....#...#...#.#...#
#.#M###.#.###########.###.#####.###.###.#.#.#####.###.#.#.#######.#.###.#######.#
#.#...#.#...#..a..#...#.#.....#.#.....#.#p..#...#...#.#.#.#.......#.#...#.....#.#
#.###.#.###.#.###.#.###.###.###.#.#####.#####.#####.#.#.#.#.#######.#.###.###.#.#
#.........#...B.#.........#.....#.......#.............#...#.......E.#.....#....c#
#################################################################################

16
lib.rkt
View File

@ -11,6 +11,7 @@
show-solution show-solution
make-vector-grid make-vector-grid
lists->vectors
vectors->lists vectors->lists
hash->vectors hash->vectors
show-list-grid show-list-grid
@ -78,6 +79,10 @@
(define (make-vector-grid width height [default 0]) (define (make-vector-grid width height [default 0])
(build-vector height (λ (_) (make-vector width default)))) (build-vector height (λ (_) (make-vector width default))))
;; lists->vectors : list-grid -> vector-grid
(define (lists->vectors list-grid)
(list->vector (map list->vector list-grid)))
;; vectors->lists : vector-grid -> list-grid ;; vectors->lists : vector-grid -> list-grid
(define (vectors->lists vector-grid) (define (vectors->lists vector-grid)
(map vector->list (vector->list vector-grid))) (map vector->list (vector->list vector-grid)))
@ -118,6 +123,13 @@
(show-vector-grid char-hash (hash->vectors hash-grid default))) (show-vector-grid char-hash (hash->vectors hash-grid default)))
;; Char helpers ;;
;; nchar=? : char -> char -> boolean
(define (nchar=? c1 c2)
(not (char=? c1 c2)))
;; Number helpers ;; ;; Number helpers ;;
;; sum : (listof number) -> number ;; sum : (listof number) -> number
@ -127,10 +139,6 @@
(define (!= n1 n2) (define (!= n1 n2)
(not (= n1 n2))) (not (= n1 n2)))
;; nchar=? : char -> char -> boolean
(define (nchar=? c1 c2)
(not (char=? c1 c2)))
;; nzero? : number -> boolean ;; nzero? : number -> boolean
(define (nzero? n) (define (nzero? n)
(not (zero? n))) (not (zero? n)))

74
src/18.rkt Normal file
View File

@ -0,0 +1,74 @@
#lang racket
(require data/queue
graph
(except-in "../lib.rkt" transpose))
(define input
(problem-input 18))
(define list-grid
(map string->list input))
(define vector-grid
(lists->vectors list-grid))
(define width
(length (car list-grid)))
(define height
(length list-grid))
(define (get-char coord)
(match-let ([(list x y) coord])
(if (or (< x 0) (< y 0)
(>= x width)
(>= y height))
#\#
(vector-ref (vector-ref vector-grid y) x))))
(define (neighbours coord)
(match-let ([(list x y) coord])
(let ([U (list x (sub1 y))]
[D (list x (add1 y))]
[L (list (sub1 x) y)]
[R (list (add1 x) y)])
(filter-not
( ( char=? #\#) ( get-char))
(list U D L R)))))
(define key-door-graph
(let* ([Q (make-queue)]
[entrance (list (round (/ width 2))
(round (/ height 2)))]
[visited (make-hash `((,entrance . #t)))]
[graph (weighted-graph/undirected '())])
(enqueue! Q (list entrance #\@ 0))
(let loop ()
(if (queue-empty? Q)
graph
(match-let* ([(list coord prev dist) (dequeue! Q)]
[ncoords (neighbours coord)])
(for-each
(λ (ncoord)
(let ([nc (get-char ncoord)])
(cond
[(hash-ref visited ncoord #f)]
[(char=? #\. nc)
(hash-set! visited ncoord #t)
(enqueue! Q (list ncoord prev (add1 dist)))]
[else
(hash-set! visited ncoord #t)
(enqueue! Q (list ncoord nc 0))
(add-edge! graph prev nc (add1 dist))])))
ncoords)
(loop))))))
(define ordered-keys
(let ([keys (filter char-lower-case? (get-vertices key-door-graph))])
(displayln keys)
(define (sort-fn c1 c2)
(not (member (char-upcase c2) (fewest-vertices-path key-door-graph #\@ c1))))
(sort keys sort-fn)))
#;(for-each displayln (map (λ (s) (string-replace (string-replace s "#" "") "." " ")) input))