-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday03.rkt
65 lines (44 loc) · 1.43 KB
/
day03.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#lang racket
(require "aoc.rkt" threading)
(define (split-out data col)
(partition
(λ (row) (char=? #\1 (list-ref row col)))
data))
(define (compute nrs)
(for/sum ([(n ex) (in-indexed nrs)])
(* n (expt 2 ex))))
(define (part-1 data)
(define (opposite val)
(- 1 val))
(define (most-common-bit pos data)
(define-values (ones zeros) (split-out data pos))
(if (> (length zeros) (length ones)) 0 1))
(for/fold ([gamma '()]
#:result (* (compute gamma) (compute (map opposite gamma))))
([col (in-range (length (first data)))])
(cons (most-common-bit col data) gamma)))
(define (filter-out most-common? data)
(for/fold ([acc data]
#:result (first acc))
([col (in-range (length (first data)))]
#:break (= 1 (length acc)))
(define-values (ones zeros) (split-out acc col))
(define more-zeros? (> (length zeros) (length ones)))
(if (xor most-common? more-zeros?) ones zeros)))
(define (part-2 data)
(for/fold ([acc 1])
([most-common? '(#t #f)])
(~>> data
(filter-out most-common?)
(map (λ (c) (if (char=? #\1 c) 1 0)))
reverse
compute
(* acc))))
(define numbers (read-input 3 'list))
(part-1 numbers)
(part-2 numbers)
(module+ test
(require rackunit)
(define numbers (read-input "03-test" 'list))
(check-equal? (part-1 numbers) 198)
(check-equal? (part-2 numbers) 230))