diff --git a/src/05.rkt b/src/05.rkt index 937b101..21c1e21 100644 --- a/src/05.rkt +++ b/src/05.rkt @@ -1,25 +1,18 @@ #lang racket -(require "../lib.rkt") +(require racket/set + "../lib.rkt") -(define (string->rc str) - (let* ([row-str (substring str 0 7)] - [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 (string->seat str) + (string->number (string-append "#b" (regexp-replaces str '([#rx"F" "0"] [#rx"B" "1"] [#rx"L" "0"] [#rx"R" "1"]))))) -(define input (map string->rc (problem-input 5))) +(define input (map string->seat (problem-input 5))) -(define part1 - (apply max (map (match-lambda [`(,row . ,col) (+ (* row 8) col)]) input))) - -(define part2 - (let ([grid (make-vector-grid 8 128 #\o)]) - (for ([rc input]) - (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))))))) +(define-values (part1 part2) + (let* ([minimum (apply min input)] + [maximum (apply max input)] + [seats (list->set (range minimum (add1 maximum)))] + [filled (list->set input)]) + (values maximum (set-first (set-subtract seats filled))))) (show-solution part1 part2)