diff options
Diffstat (limited to 'racket/aoc2015')
-rw-r--r-- | racket/aoc2015/day-01/day-01.rkt | 16 | ||||
-rw-r--r-- | racket/aoc2015/day-02/day-02.rkt | 26 | ||||
-rw-r--r-- | racket/aoc2015/day-03/day-03.rkt | 28 | ||||
-rw-r--r-- | racket/aoc2015/day-04/day-04.rkt | 18 | ||||
-rw-r--r-- | racket/aoc2015/day-05/day-05.rkt | 32 | ||||
-rw-r--r-- | racket/aoc2015/day-06/day-06.rkt | 49 | ||||
-rw-r--r-- | racket/aoc2015/day-25/day-25.rkt | 8 |
7 files changed, 177 insertions, 0 deletions
diff --git a/racket/aoc2015/day-01/day-01.rkt b/racket/aoc2015/day-01/day-01.rkt new file mode 100644 index 0000000..efbd02a --- /dev/null +++ b/racket/aoc2015/day-01/day-01.rkt @@ -0,0 +1,16 @@ +#lang racket +(require "../../jj-aoc.rkt") + +;; part 1 +(for/fold ([current-floor 0]) ([l (in-input-port-chars (open-day 1 2015))] [i (in-naturals)]) + (match l + [#\( (add1 current-floor)] + [#\) (sub1 current-floor)])) + +;; part 2 +(for/fold ([current-floor 0] [last-index 0] #:result (add1 last-index)) + ([l (in-input-port-chars (open-day 1 2015))] [i (in-naturals)]) + #:break (= current-floor -1) + (match l + [#\( (values (add1 current-floor) i)] + [#\) (values (sub1 current-floor) i)])) diff --git a/racket/aoc2015/day-02/day-02.rkt b/racket/aoc2015/day-02/day-02.rkt new file mode 100644 index 0000000..579fd00 --- /dev/null +++ b/racket/aoc2015/day-02/day-02.rkt @@ -0,0 +1,26 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading + racket/struct) + +(struct present (l w h) #:transparent) + +(define presents + (for/list ([size-string (in-lines (open-day 2 2015))]) + (~> size-string (string-split "x") (map string->number _) (apply present _)))) + +;; part 1 +(define (paper-area p) + (define main-area (~> p struct->list (combinations 2) (map (λ~> (apply * 2 _)) _) (apply + _))) + (define slack-area (~> p struct->list (sort <) (take 2) (apply * _))) + (+ main-area slack-area)) + +(for/sum ([p (in-list presents)]) (paper-area p)) + +;; part 2 +(define (ribbon-length p) + (define ribbon-around-box (~> p struct->list (sort <) (take 2) (map (λ~> (* 2)) _) (apply + _))) + (define ribbon-for-bow (~> p struct->list (apply * _))) + (+ ribbon-around-box ribbon-for-bow)) + +(for/sum ([p (in-list presents)]) (ribbon-length p)) diff --git a/racket/aoc2015/day-03/day-03.rkt b/racket/aoc2015/day-03/day-03.rkt new file mode 100644 index 0000000..1d44955 --- /dev/null +++ b/racket/aoc2015/day-03/day-03.rkt @@ -0,0 +1,28 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading + (only-in algorithms chunks-of) + racket/hash) + +(define directions + (for/list ([l (in-input-port-chars (open-day 3 2015))]) + (string->symbol (string l)))) + +(define (trace-santa dirs) + (define visits (make-hash)) + (for/fold ([x 0] [y 0] #:result visits) ([dir (in-list dirs)]) + (hash-set! visits `(,x ,y) #true) + (match dir + ['^ (values x (add1 y))] + ['v (values x (sub1 y))] + ['< (values (add1 x) y)] + ['> (values (sub1 x) y)]))) + +;; part 1 +(~> directions trace-santa hash-values length) + +;; part 2 +(~> directions (chunks-of 2) (apply map list _) (map trace-santa _) (match-define (list real robo) _)) + +(hash-union! real robo #:combine (λ _ #true)) +(~> real hash-values length) diff --git a/racket/aoc2015/day-04/day-04.rkt b/racket/aoc2015/day-04/day-04.rkt new file mode 100644 index 0000000..2c16043 --- /dev/null +++ b/racket/aoc2015/day-04/day-04.rkt @@ -0,0 +1,18 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading + file/md5) + +(define secret-key (~> (open-day 4 2015) port->string string-trim)) + +(define (find-n-zeroes n) + (for/first ([i (in-naturals)] + #:when + (~>> i (~a secret-key) md5 bytes->string/utf-8 (string-prefix? _ (make-string n #\0)))) + i)) + +;; part 1 +(time (find-n-zeroes 5)) + +;; part 2 +(time (find-n-zeroes 6)) diff --git a/racket/aoc2015/day-05/day-05.rkt b/racket/aoc2015/day-05/day-05.rkt new file mode 100644 index 0000000..3449adc --- /dev/null +++ b/racket/aoc2015/day-05/day-05.rkt @@ -0,0 +1,32 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(define strs (port->lines (open-day 5 2015))) + +;; part 1 +(define (at-least-three-vowels? str) + (~>> str (regexp-replace* #px"[^aeiou]" _ "") string-length (<= 3))) + +(define (at-least-one-pair? str) + (regexp-match? #px"(.)\\1{1,}" str)) + +(define (no-forbidden-pairs? str) + (~>> (list "ab" "cd" "pq" "xy") (ormap (λ~>> (string-contains? str))) not)) + +(define (nice? str) + (~>> (list at-least-three-vowels? at-least-one-pair? no-forbidden-pairs?) (andmap (λ (f) (f str))))) + +(count nice? strs) + +;; part 2 +(define (repeating-pair? str) + (regexp-match? #px"(..).*\\1" str)) + +(define (symmetry? str) + (regexp-match? #px"(.).\\1" str)) + +(define (new-nice? str) + (~>> (list repeating-pair? symmetry?) (andmap (λ (f) (f str))))) + +(count new-nice? strs) diff --git a/racket/aoc2015/day-06/day-06.rkt b/racket/aoc2015/day-06/day-06.rkt new file mode 100644 index 0000000..d2eed08 --- /dev/null +++ b/racket/aoc2015/day-06/day-06.rkt @@ -0,0 +1,49 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(struct instruction (todo x1 y1 x2 y2) #:transparent) + +(define (make-instruction lst) + (apply instruction (string->symbol (first lst)) (map string->number (rest lst)))) + +(define instructions + (for/list ([l (in-lines (open-day 6 2015))]) + (~>> l + (regexp-match + #px"(turn on|toggle|turn off) (\\d{1,3}),(\\d{1,3}) through (\\d{1,3}),(\\d{1,3})") + rest + make-instruction))) + +(define (vector2d-modify! vec x y f) + (define pos (+ x (* 1000 y))) + (vector-set! vec pos (f (vector-ref vec pos)))) + +;; part 1 +(define (todo inst) + (match (instruction-todo inst) + ['|turn on| (λ _ #true)] + ['|turn off| (λ _ #false)] + ['|toggle| not])) + +(define (modify-light-grid inst light-grid using) + (for ([x (inclusive-range (instruction-x1 inst) (instruction-x2 inst))]) + (for ([y (inclusive-range (instruction-y1 inst) (instruction-y2 inst))]) + (vector2d-modify! light-grid x y (using inst))))) + +(define light-grid (make-vector (* 1000 1000) #false)) +(for ([i (in-list instructions)]) + (modify-light-grid i light-grid todo)) +(vector-count identity light-grid) + +;; part 2 +(define (todo-dimmer inst) + (match (instruction-todo inst) + ['|turn on| add1] + ['|turn off| (λ (x) (max 0 (sub1 x)))] + ['|toggle| (curry + 2)])) + +(define dimmable-grid (make-vector (* 1000 1000) 0)) +(for ([i (in-list instructions)]) + (modify-light-grid i dimmable-grid todo-dimmer)) +(apply + (vector->list dimmable-grid)) diff --git a/racket/aoc2015/day-25/day-25.rkt b/racket/aoc2015/day-25/day-25.rkt new file mode 100644 index 0000000..975f4c3 --- /dev/null +++ b/racket/aoc2015/day-25/day-25.rkt @@ -0,0 +1,8 @@ +#lang racket + +(define max-r 2978) +(define max-c 3083) + +(for/fold ([code 20151125] [r 1] [c 1]) ([i (in-naturals)] #:break (and (= max-r r) (= max-c c))) + (define new-code (modulo (* code 252533) 33554393)) + (if (= r 1) (values new-code (add1 c) 1) (values new-code (sub1 r) (add1 c)))) |