blob: 315bd5bf5a9771767c8b2cfaea5a3a228ea46b3b (
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
45
46
47
|
;; https://adventofcode.com/2021/day/2
; optional -- 'sum' is more visually indicative than 'apply +'
(func sum (nums) (apply + nums))
(func update-vertical-coords (current instruction)
(def aim
(if (eq? "up" (first instruction))
(- (last current) (last instruction))
(+ (last current) (last instruction))))
(list
(first current) ; horiz unchanged
(first (rest current)) ; depth unchanged
aim))
(func update-coords (current instruction)
; expecting (horiz depth aim)
(if (eq? "forward" (first instruction))
(list
(+ (first current) (last instruction)) ; increase horizontal
(+ (first (rest current)) (* (last current) (last instruction))) ; increase depth by aim * x
(last current)) ; aim unchanged
(update-vertical-coords current instruction)))
(func do-the-thing (inp)
; since order matters now (both multiplication and addition),
; go through each and update one by one
; this *could* be recursive, but i don't know if that would
; be any clearer
(def current (list 0 0 0))
(for-each inp
(redef current (update-coords current _item_)))
(print (concat "horiz: " (->string (first current))))
(print (concat "depth: " (->string (first (rest current)))))
(print (concat "answer: " (->string (apply * (most current))))))
(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")))))
(do-the-thing lines)
|