aoc2020/5.rkt

40 lines
1.4 KiB
Racket

#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 5
;; helper functions here
(define (part1 input)
(apply max input))
(define (part2 input)
(for/first ([id (in-range (apply min input) (apply max input))]
#:when (and (not (member id input))
(member (add1 id) input)
(member (sub1 id) input)))
id))
(module+ main
(define input (for/list ([line (in-list (file->lines "inputs/5"))])
;; ok what is this fuckery???
;; the trick is the whole number can just be treated as binary
;; first, notice that the entry is a concatenation of row and column, and both
;; are basically binary numbers, B and R are a 1 digit and F and L are a 0 digit
;; finally... the ID is <first part> * 8 + <second part>, and conveniently the
;; second part is 3 digits
;; so we parse the whole thing as a single binary number. rather than
;; string-replacing the chars and then number->string with radix 2, i used a
;; manual fold to build the number
(for/fold ([val 0]) ([char (in-string line)])
(+ (* 2 val) (if ((or/c #\B #\R) char) 1 0)))))
;; part 1
(answer 5 1 (part1 input))
;; part 2
(answer 5 2 (part2 input))
(displayln "meow"))