Day 03: Keep string instead of list of chars.

This commit is contained in:
Jonathan Chan 2021-12-02 22:34:24 -08:00
parent 1ed23c13fd
commit b5456fa078
1 changed files with 5 additions and 7 deletions

View File

@ -2,14 +2,12 @@
(require "../lib.rkt") (require "../lib.rkt")
(define input (define input (problem-input 3))
(for/list ([b (problem-input 3)])
(string->list b)))
(define bits (length (first input))) (define bits (string-length (first input)))
(define (nth-bit nums p n) (define (nth-bit nums p n)
(let ([bs (map #{list-ref % n} nums)]) (let ([bs (map #{string-ref % n} nums)])
(if (p (count #{char=? % #\1} bs) (/ (length nums) 2)) (if (p (count #{char=? % #\1} bs) (/ (length nums) 2))
#\1 #\0))) #\1 #\0)))
@ -20,11 +18,11 @@
(define (search p) (define (search p)
(for/fold ([nums input] (for/fold ([nums input]
#:result (chars->binary (first nums))) #:result (string->binary (first nums)))
([n (range bits)] ([n (range bits)]
#:break (singleton? nums)) #:break (singleton? nums))
(define bit (nth-bit nums p n)) (define bit (nth-bit nums p n))
(filter #{char=? bit (list-ref % n)} nums))) (filter #{char=? bit (string-ref % n)} nums)))
(define part1 (define part1
(* (squimsh >=) (squimsh <))) (* (squimsh >=) (squimsh <)))