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