diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2021/day-03/day-03.rkt | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'aoc2021/day-03/day-03.rkt')
-rw-r--r-- | aoc2021/day-03/day-03.rkt | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/aoc2021/day-03/day-03.rkt b/aoc2021/day-03/day-03.rkt deleted file mode 100644 index 95b7efd..0000000 --- a/aoc2021/day-03/day-03.rkt +++ /dev/null @@ -1,39 +0,0 @@ -#lang racket -(require advent-of-code - threading) - -(define data - (~> (open-aoc-input (find-session) 2021 3 #:cache (string->path "./cache")) - port->lines - (map string->list _))) - -;; part 1 -(define most-common-bits - (for*/list ([row (in-list (apply map list data))] [len (in-value (length data))]) - (if (> (count (λ (c) (char=? #\1 c)) row) (/ len 2)) #\1 #\0))) -(define (bit-list->number lst) - (~> lst (apply string _) (string->number _ 2))) - -(define gamma (bit-list->number most-common-bits)) -(define epsilon (~> most-common-bits (map (λ (c) (if (char=? c #\1) #\0 #\1)) _) bit-list->number)) - -(* gamma epsilon) - -;; part 2 -(define (rating-search data comparison) - (for/fold ([candidates data] #:result (bit-list->number (first candidates))) - ([bit (in-list most-common-bits)] [bit-index (in-range 0 (length most-common-bits))]) - #:break (= 1 (length candidates)) - (define keep-bit - (~> candidates - (apply map list _) - (list-ref _ bit-index) - (count (λ (c) (char=? #\1 c)) _) - (comparison _ (/ (length candidates) 2)) - (if _ #\1 #\0))) - (filter (λ (row) (char=? keep-bit (list-ref row bit-index))) candidates))) - -(define oxygen-rating (rating-search data >=)) -(define scrubber-rating (rating-search data <)) - -(* oxygen-rating scrubber-rating) |