Last active
December 9, 2021 15:46
-
-
Save adolfopa/8d1591db844aad6993eda38350d9473e 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 | |
(define (part-1) | |
(with-input-from-file "input" | |
(thunk | |
(define input | |
(for/list ([ln (in-lines)]) | |
(string->number ln))) | |
(for/fold ([n 0] [p (first input)]) | |
([x (in-list (rest input))]) | |
(values (if (> x p) (add1 n) n) | |
x))))) | |
(define (part-2) | |
(with-input-from-file "input" | |
(thunk | |
(define input | |
(for/list ([ln (in-lines)]) | |
(string->number ln))) | |
(for/fold ([n 0] [s +Inf.0]) | |
([(x y z) (in-parallel input (cdr input) (cddr input))]) | |
(define sum | |
(+ x y z)) | |
(values (if (> sum s) (add1 n) n) | |
sum)))) |
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 | |
(define (part-1) | |
(with-input-from-file "input" | |
(thunk | |
(define-values (d h) | |
(for/fold ([depth 0] [horiz 0]) | |
([cmd (sequence-map string-split (in-lines))]) | |
(match cmd | |
[`("forward" ,n) | |
(values depth (+ horiz (string->number n)))] | |
[`("up" ,n) | |
(values (- depth (string->number n)) horiz)] | |
[`("down" ,n) | |
(values (+ depth (string->number n)) horiz)]))) | |
(* d h)))) | |
(define (part-2) | |
(with-input-from-file "input" | |
(thunk | |
(define-values (_ d h) | |
(for/fold ([aim 0] [depth 0] [horiz 0]) | |
([cmd (sequence-map (compose (match-lambda | |
[`(,c ,n) `(,c ,(string->number n))]) | |
string-split) | |
(in-lines))]) | |
(match cmd | |
[`("forward" ,n) | |
(values aim (+ (* n aim) depth) (+ horiz n))] | |
[`("up" ,n) | |
(values (- aim n) depth horiz)] | |
[`("down" ,n) | |
(values (+ aim n) depth horiz)]))) | |
(* d h)))) |
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 | |
(define (part-1) | |
(with-input-from-file "input" | |
(thunk | |
(define-values (ones n) | |
(for/fold ([acc (make-list 12 0)] [n 0]) | |
([x (in-lines)]) | |
(values (map + acc (map string->number (remf* (curry string=? "") (string-split x "")))) | |
(add1 n)))) | |
(define-values (γ ε) | |
(for/fold ([γ 0] [ε 0]) | |
([x ones]) | |
(values (+ (* γ 2) (if (<= (- n x) x) 0 1)) | |
(+ (* ε 2) (if (<= (- n x) x) 1 0))))) | |
(* γ ε)))) | |
(define (part-2) | |
(with-input-from-file "input" | |
(thunk | |
(let ([data | |
(for/list ([ln (in-lines)]) | |
(list->vector | |
(map string->number | |
(remf* (curry string=? "") (string-split ln "")))))]) | |
(define (most-common-bits xss) | |
(let ([ones | |
(for/fold ([sum (make-vector 12 0)]) | |
([xs (in-list xss)]) | |
(vector-map + sum xs))]) | |
(for/vector ([n ones]) | |
(if (> (- (length xss) n) n) 0 1)))) | |
(define (invert-bits xs) | |
(for/vector ([x (in-vector xs)]) | |
(if (= x 0) 1 0))) | |
(define (rate fn readings) | |
(first | |
(for/fold ([elts readings]) | |
([n (in-range 12)] | |
#:break (= (length elts) 1)) | |
(let ([mcb (fn elts)]) | |
(for/list ([elt (in-list elts)] | |
#:when (= (vector-ref elt n) (vector-ref mcb n))) | |
elt))))) | |
(define (bits->number xs) | |
(for/fold ([e 0]) | |
([x (in-vector xs)]) | |
(+ (* e 2) x))) | |
(* (bits->number (rate most-common-bits data)) | |
(bits->number (rate (compose invert-bits most-common-bits) data))))))) |
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 srfi/43) | |
(struct board ([win? #:mutable] [moves #:mutable] rows) #:transparent) | |
(define (read-data) | |
(define (read-board) | |
(for/list ([ln (in-lines)] | |
#:break (zero? (string-length ln))) | |
(for/vector ([n (in-list (regexp-split #px"\\s+" (string-trim ln)))]) | |
(string->number n)))) | |
(with-input-from-file "input" | |
(thunk | |
(values (map string->number (string-split (read-line) ",")) | |
(begin (read-line) | |
(for/list ([b (in-producer read-board (compose zero? length))]) | |
(board #f '() b))))))) | |
(define (winning-board? board) | |
(or (for/or ([row (in-list (board-rows board))]) | |
(for/and ([col (in-vector row)]) | |
(eq? col '*))) | |
(apply vector-any (λ cols (andmap (curry eq? '*) cols)) (board-rows board)))) | |
(define (apply-moves moves b) | |
(for* ([m (in-list moves)] | |
[row (in-list (board-rows b))] | |
[(col i) (in-indexed row)] | |
#:break (board-win? b)) | |
(when (eqv? m col) | |
(vector-set! row i '*) | |
(set-board-win?! b (winning-board? b))) | |
(set-board-moves! b (cons m (board-moves b)))) | |
b) | |
(define (board-score board) | |
(* (car (board-moves board)) | |
(for*/sum ([row (in-list (board-rows board))] | |
[col (in-vector row)] | |
#:unless (eq? col '*)) | |
col))) | |
(define (find-winning-board rank moves boards) | |
(board-score (rank (compose length board-moves) | |
(filter board-win? | |
(map (curry apply-moves moves) boards))))) | |
(define (part-1) | |
(call-with-values read-data (curry find-winning-board argmin))) | |
(define (part-2) | |
(call-with-values read-data (curry find-winning-board argmax))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment