aboutsummaryrefslogtreecommitdiff
path: root/aoc/2021/day02/part1.neb
blob: 82201abad2f87616d9a0323166e2ce1267794cdd (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
40
41
42
43
44
;; https://adventofcode.com/2021/day/2

; optional -- 'sum' is more visually indicative than 'apply +'
(func sum (nums) (apply + nums))

(func do-the-thing (inp)
    ; order doesn't matter, so we can split horizontal and vertical
    ; movement without affecting the answer
    (def vert-changes (filter (lambda (x) (in? (first x) (list "up" "down"))) inp))
    (def horiz-changes (filter (lambda (x) (eq? (first x) "forward")) inp))

    ; turn "up"/"down" into +/- 1 
    (def vert-numbers
        (map
            (lambda (x)
                (if (eq? (first x) "up")
                    (prepend (rest x) (- 1))
                    (prepend (rest x) 1)))
            vert-changes))

    (def vert
        (sum ; add everything up
            (map
                (lambda (x) (* (first x) (last x))) ; multiply each pair together
                vert-numbers)))

    (def horiz
        (sum
            (map last horiz-changes))) ; we know there's only 2 elements in each list, and we want the last one

    (print (concat "vert: " (->string vert)))
    (print (concat "horiz: " (->string horiz)))
    (* vert horiz))

(def lines 
    (map
        (lambda (x) (list (first x) (string->int (first (rest x))))) ; convert second values to int
        (map 
            (lambda (x) (split x " "))
            (map strip
                (read-lines "input.txt")))))

(print (->string lines))
(print (->string (do-the-thing lines)))