;; 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)