Created
December 5, 2020 18:00
-
-
Save saahityaedams/67f2f354e60d2d7f640cf24035b264eb to your computer and use it in GitHub Desktop.
aoc day 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang racket | |
| (define input | |
| (port->lines (open-input-file "input5.in"))) | |
| (define (find-row lower upper seq) | |
| (cond | |
| [(equal? (string-length seq) 0) lower] | |
| [(equal? (string-ref seq 0) #\F) (find-row lower (/ (+ lower upper (- 1)) 2) (substring seq 1))] | |
| [(equal? (string-ref seq 0) #\B) (find-row (/ (+ lower upper 1) 2) upper (substring seq 1))] | |
| )) | |
| (define (find-col lower upper seq) | |
| (cond | |
| [(equal? (string-length seq) 0) lower] | |
| [(equal? (string-ref seq 0) #\L) (find-col lower (/ (+ lower upper (- 1)) 2) (substring seq 1))] | |
| [(equal? (string-ref seq 0) #\R) (find-col (/ (+ lower upper 1) 2) upper (substring seq 1))] | |
| )) | |
| (define (unique-seat-id seq) (+ (* 8 (find-row 0 127 (substring seq 0 7))) (find-col 0 7 (substring seq 7 10)))) | |
| (define test_case_1 (equal? (unique-seat-id "FBFBBFFRLR") 357)) | |
| (define test_case_2 (equal? (unique-seat-id "BFFFBBFRRR") 567)) | |
| (define test_case_3 (equal? (unique-seat-id "FFFBBBFRRR") 119)) | |
| (define test_case_4 (equal? (unique-seat-id "BBFFBBFRLL") 820)) | |
| ; (apply max (map unique-seat-id input)) Part 1 Ans | |
| (define unique-seat-ids (map unique-seat-id input)) | |
| (define (bad-neigh-seats unique-seat-ids id) | |
| (if (and (member (+ id 1) unique-seat-ids) (member (- id 1) unique-seat-ids)) #f #t)) | |
| (filter (curry bad-neigh-seats unique-seat-ids ) unique-seat-ids) ; Part 2 Ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment