Browse Source

whatever

canon
Hazel Levine 8 months ago
parent
commit
4964776681
Signed by: hazel GPG Key ID: 1884029A28789A62
  1. 8
      README.org
  2. 33
      day5.rkt

8
README.org

@ -160,13 +160,13 @@ Again, run =count= with this validator on the parsed input data.
I wanted to see if I could do the Part 2 validator with almost nothing but regex, so I did that.
* Day 5: Binary Boarding
** Part 1
I did some stuff with doing manual BSP here, but I'm a fucking idiot and couldn't get it to work.
I am a failure at the one thing that I should be good at and I should be ashamed of myself.
Anyway, each boarding pass can actually be treated as a binary number. F and L are zero digits,
Each boarding pass can actually be treated as a binary number. F and L are zero digits,
B and R are one digits. The ID is =row * 8 + column=, and the column part is 3 digits, so... uh...
yeah.
Convert them all to IDs, maximize 'em.
** Part 2
Find the first ID that's missing but doesn't have neighbors. This is just =(first (memf ...))=.
** Revisions
I re-did this with manual BSP, since I tried and failed at that for a while before realizing the
binary string method.

33
day5.rkt

@ -1,6 +1,35 @@
#lang racket
(require "lib/common.rkt")
;; naive solution: manual BSP
(define (lower-half lst)
(take lst (quotient (length lst) 2)))
(define (upper-half lst)
(drop lst (quotient (length lst) 2)))
(define (pass->seat str)
(define (row-bsp row)
(first
(for/fold ([rng (range 0 127)])
([ch (in-string row)])
(match ch
[#\F (lower-half rng)]
[#\B (upper-half rng)]))))
(define (col-bsp col)
(first
(for/fold ([rng (range 0 8)])
([ch (in-string col)])
(match ch
[#\L (lower-half rng)]
[#\R (upper-half rng)]))))
(define row-pass (substring str 0 7))
(define col-pass (substring str 7 10))
(cons (add1 (row-bsp row-pass)) (col-bsp col-pass)))
(define (seat->id seat)
(+ (* (car seat) 8) (cdr seat)))
;; big brain solution: it's just a binary string
(define (pass->id str)
(for/fold ([id 0])
([ch (in-string str)])
@ -10,10 +39,10 @@
[(#\B #\R) 1]))))
(define (day5a in)
(apply max (map pass->id in)))
(apply max (map (compose1 seat->id pass->seat) in)))
(define (day5b in)
(define ids (map pass->id in))
(define ids (map (compose1 seat->id pass->seat) in))
(first (memf (lambda (id)
(and (not (member id ids))
(member (add1 id) ids)

Loading…
Cancel
Save