aboutsummaryrefslogtreecommitdiff
path: root/2015
diff options
context:
space:
mode:
Diffstat (limited to '2015')
-rw-r--r--2015/day-01/day-01.rkt21
-rw-r--r--2015/day-02/day-02.rkt48
-rw-r--r--2015/day-03/day-03.rkt36
-rw-r--r--2015/day-04/day-04.rkt23
-rw-r--r--2015/day-05/day-05.rkt41
-rw-r--r--2015/day-25/day-25.rkt16
6 files changed, 185 insertions, 0 deletions
diff --git a/2015/day-01/day-01.rkt b/2015/day-01/day-01.rkt
new file mode 100644
index 0000000..2432c96
--- /dev/null
+++ b/2015/day-01/day-01.rkt
@@ -0,0 +1,21 @@
+#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)])) \ No newline at end of file
diff --git a/2015/day-02/day-02.rkt b/2015/day-02/day-02.rkt
new file mode 100644
index 0000000..870e380
--- /dev/null
+++ b/2015/day-02/day-02.rkt
@@ -0,0 +1,48 @@
+#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)) \ No newline at end of file
diff --git a/2015/day-03/day-03.rkt b/2015/day-03/day-03.rkt
new file mode 100644
index 0000000..efa09ad
--- /dev/null
+++ b/2015/day-03/day-03.rkt
@@ -0,0 +1,36 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading
+ (only-in algorithms chunks-of)
+ (only-in awesome-list transpose)
+ 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)
+ transpose
+ (map trace-santa _)
+ (match-define (list real robo) _))
+
+(hash-union! real robo #:combine (λ _ #true))
+(~> real hash-values length) \ No newline at end of file
diff --git a/2015/day-04/day-04.rkt b/2015/day-04/day-04.rkt
new file mode 100644
index 0000000..4008b92
--- /dev/null
+++ b/2015/day-04/day-04.rkt
@@ -0,0 +1,23 @@
+#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)) \ No newline at end of file
diff --git a/2015/day-05/day-05.rkt b/2015/day-05/day-05.rkt
new file mode 100644
index 0000000..9714a0f
--- /dev/null
+++ b/2015/day-05/day-05.rkt
@@ -0,0 +1,41 @@
+#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) \ No newline at end of file
diff --git a/2015/day-25/day-25.rkt b/2015/day-25/day-25.rkt
new file mode 100644
index 0000000..786538c
--- /dev/null
+++ b/2015/day-25/day-25.rkt
@@ -0,0 +1,16 @@
+#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))))