aboutsummaryrefslogtreecommitdiff
path: root/2021/day-02/day-02.rkt
blob: dbd1275dd5a7302d8cb3978a9382585b43ee4912 (plain)
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
#lang racket
(require advent-of-code
         threading
         algorithms)

(define motion-data
  (~> (open-aoc-input (find-session) 2021 2 #:cache (string->path "./cache"))
      (port->list read _)
      (chunks-of _ 2)))

;; part 1
(for/fold ([depth 0]
           [position 0]
           #:result (* depth position))
          ([motion (in-list motion-data)])
  (match motion
    [(list 'forward x) (values depth
                               (+ position x))]
    [(list 'up x) (values (- depth x)
                          position)]
    [(list 'down x) (values (+ depth x)
                            position)]))

;; part 2
(for/fold ([aim 0]
           [depth 0]
           [position 0]
           #:result (* depth position))
          ([motion (in-list motion-data)])
  (match motion
    [(list 'forward x) (values aim
                               (+ depth (* aim x))
                               (+ position x))]
    [(list 'up x) (values (- aim x)
                          depth
                          position)]
    [(list 'down x) (values (+ aim x)
                            depth
                            position)]))