diff options
| author | mryouse | 2023-06-04 18:24:06 +0000 | 
|---|---|---|
| committer | mryouse | 2023-06-04 18:24:06 +0000 | 
| commit | e89f95e937f1df3466741d734182e8341852b1f5 (patch) | |
| tree | 9f5747fc0e45ef3f15a837219ad7a606d0e8156c /aoc/2021/day02 | |
| parent | 2e3ffdbd229761bbf49da61476210f062057f969 (diff) | |
Diffstat (limited to 'aoc/2021/day02')
| -rw-r--r-- | aoc/2021/day02/part2.neb | 47 | 
1 files changed, 47 insertions, 0 deletions
| diff --git a/aoc/2021/day02/part2.neb b/aoc/2021/day02/part2.neb new file mode 100644 index 0000000..315bd5b --- /dev/null +++ b/aoc/2021/day02/part2.neb @@ -0,0 +1,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) | 
