Created
December 3, 2021 23:46
-
-
Save aaronjeline/0e58b8a5f181466ab89989c3a0323f8e to your computer and use it in GitHub Desktop.
This file contains 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 | |
(require threading) | |
(require data/bit-vector) | |
(define src | |
(~> | |
(with-input-from-file | |
"/tmp/input" | |
(λ () (port->lines (current-input-port)))) | |
(map string->list _))) | |
(define pos (length (first src))) | |
(define (compute-transpose m) | |
(for/list [(i (in-range (length (first m))))] | |
(map (λ (lst) (list-ref lst i)) m))) | |
(define transpose (compute-transpose src)) | |
(define (counteq x) | |
(λ (lst) (count (λ (y) (eq? x y)) lst))) | |
(define (bnot x) | |
(match x | |
[#\1 #\0] | |
[#\0 #\1])) | |
(define (most-common lst) | |
(define 1s ((counteq #\1) lst)) | |
(define 0s ((counteq #\0) lst)) | |
(if (>= 1s 0s) #\1 #\0)) | |
(define (least-common lst) | |
(define 1s ((counteq #\1) lst)) | |
(define 0s ((counteq #\0) lst)) | |
(if (<= 0s 1s) | |
#\0 #\1)) | |
(define (gamma) | |
(~> | |
(map most-common transpose) | |
bits->number)) | |
(define (bits->number bits) | |
(~> | |
bits | |
list->string | |
(string->number 2))) | |
(define (epsilon) | |
(~> | |
(map (compose bnot most-common) transpose) | |
bits->number)) | |
(define (f-in-col f m col) | |
(~> | |
(compute-transpose m) | |
(list-ref col) | |
f)) | |
(define (at-bit-position entry p) | |
(list-ref entry p)) | |
(define (round bit-position m f) | |
(if (= 1 (length m)) | |
(first m) | |
(let* [(looking-for (f-in-col f m bit-position)) | |
(p (λ (entry) | |
(char=? looking-for | |
(at-bit-position entry bit-position))))] | |
(round (add1 bit-position) | |
(filter p m) f)))) | |
(define (oxygen) | |
(~> | |
(round 0 src most-common) | |
bits->number)) | |
(define (co2) | |
(~> | |
(round 0 src least-common) | |
bits->number)) | |
(* (oxygen) (co2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment