diff options
author | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
commit | 8ab65dc2da1742eb86ec636c50c7018385b68167 (patch) | |
tree | c4fd556aca9b867cfa1f2f174128c30857353884 /2021/day-03 | |
parent | fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff) | |
download | gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip |
prep for 2023, renaming for consistency
Diffstat (limited to '2021/day-03')
-rw-r--r-- | 2021/day-03/day-03.rkt | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/2021/day-03/day-03.rkt b/2021/day-03/day-03.rkt deleted file mode 100644 index 95b7efd..0000000 --- a/2021/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) |