From e5af484c65bb78f99fb3a646bcec796a119a22f3 Mon Sep 17 00:00:00 2001 From: haskal Date: Sat, 5 Dec 2020 00:12:11 -0500 Subject: [PATCH] day 5: initial solution --- 5.rkt | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 5.rkt diff --git a/5.rkt b/5.rkt new file mode 100644 index 0000000..7dd8454 --- /dev/null +++ b/5.rkt @@ -0,0 +1,39 @@ +#lang curly-fn racket + +(require "scripts/aoc.rkt") + +;; solution for day 5 + +;; helper functions here + +(define (part1 input) + (apply max + (for/list ([line (in-list input)]) + (match-define (list _ rowstr colstr) (regexp-match #px"^([FB]{7})([LR]{3})$" line)) + (define row (string->number (string-replace (string-replace rowstr "F" "0") "B" "1") 2)) + (define col (string->number (string-replace (string-replace colstr "L" "0") "R" "1") 2)) + (+ (* row 8) col)))) + +(define (part2 input) + (define seats + (for/list ([line (in-list input)]) + (match-define (list _ rowstr colstr) (regexp-match #px"^([FB]{7})([LR]{3})$" line)) + (define row (string->number (string-replace (string-replace rowstr "F" "0") "B" "1") 2)) + (define col (string->number (string-replace (string-replace colstr "L" "0") "R" "1") 2)) + (+ (* row 8) col))) + + (for/first ([id (in-range (apply min seats) (apply max seats))] + #:when (and (not (member id seats)) + (member (add1 id) seats) + (member (sub1 id) seats))) + id)) + +(module+ main + (define input (file->lines "inputs/5")) + ;; part 1 + (answer 5 1 (part1 input)) + + ;; part 2 + (answer 5 2 (part2 input)) + + (displayln "meow"))