Refactored Day 5.

This commit is contained in:
Jonathan Chan 2020-12-04 22:37:36 -08:00
parent eda88ed824
commit b2f90bc79b
1 changed files with 11 additions and 18 deletions

View File

@ -1,25 +1,18 @@
#lang racket #lang racket
(require "../lib.rkt") (require racket/set
"../lib.rkt")
(define (string->rc str) (define (string->seat str)
(let* ([row-str (substring str 0 7)] (string->number (string-append "#b" (regexp-replaces str '([#rx"F" "0"] [#rx"B" "1"] [#rx"L" "0"] [#rx"R" "1"])))))
[col-str (substring str 7 10)]
[as-binary ( string->number ( string-append "#b"))]
[row (as-binary (regexp-replaces row-str '([#rx"F" "0"] [#rx"B" "1"])))]
[col (as-binary (regexp-replaces col-str '([#rx"L" "0"] [#rx"R" "1"])))])
(cons row col)))
(define input (map string->rc (problem-input 5))) (define input (map string->seat (problem-input 5)))
(define part1 (define-values (part1 part2)
(apply max (map (match-lambda [`(,row . ,col) (+ (* row 8) col)]) input))) (let* ([minimum (apply min input)]
[maximum (apply max input)]
(define part2 [seats (list->set (range minimum (add1 maximum)))]
(let ([grid (make-vector-grid 8 128 #\o)]) [filled (list->set input)])
(for ([rc input]) (values maximum (set-first (set-subtract seats filled)))))
(vector-grid-update grid rc #\x))
(let ([str (apply string-append (map list->string (vectors->lists grid)))])
(add1 (car (first (regexp-match-positions #rx"xox" str)))))))
(show-solution part1 part2) (show-solution part1 part2)