aboutsummaryrefslogtreecommitdiff
path: root/aoc/2021/day02
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/2021/day02')
-rw-r--r--aoc/2021/day02/part2.neb47
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)