aboutsummaryrefslogtreecommitdiff
path: root/2021/day3/03a.rkt
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-01-02 21:58:56 +0000
committerAria <me@aria.rip>2023-01-02 21:58:56 +0000
commit5eb58ad076f2cd435b11b140820da224b60b73d5 (patch)
tree2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2021/day3/03a.rkt
initial commit
Diffstat (limited to '2021/day3/03a.rkt')
-rw-r--r--2021/day3/03a.rkt30
1 files changed, 30 insertions, 0 deletions
diff --git a/2021/day3/03a.rkt b/2021/day3/03a.rkt
new file mode 100644
index 0000000..bb42d83
--- /dev/null
+++ b/2021/day3/03a.rkt
@@ -0,0 +1,30 @@
+#lang racket
+(define (mode xs)
+ (define ht (make-hash))
+ (define max-key '0)
+ (for ([x xs])
+ (define new-val (+ 1 (hash-ref ht x 0)))
+ (hash-set! ht x new-val)
+ (if (> new-val (hash-ref ht max-key 0)) (set! max-key x) 3))
+ max-key)
+
+(define (transpose xss)
+ (apply map list xss))
+
+(define (flip x)
+ (cond [(string=? x "0") "1"]
+ [else "0"]))
+
+(define input (open-input-file "input"))
+(define bit-positions
+ (transpose (map (lambda (xs)
+ (map (curry make-string 1) (string->list xs)))
+ (port->lines input))))
+
+(define gamma-str (foldr string-append "" (map mode bit-positions)))
+(define epsilon-str (foldr string-append "" (map (compose flip mode) bit-positions)))
+
+(define gamma (string->number gamma-str 2))
+(define epsilon (string->number epsilon-str 2))
+
+(* gamma epsilon)