aboutsummaryrefslogtreecommitdiff
path: root/aoc/2021/day02/part1.neb
diff options
context:
space:
mode:
authormryouse2023-06-03 20:22:18 +0000
committermryouse2023-06-03 20:22:18 +0000
commit7b7bf727da3250907284368b98b0083a4b4e2386 (patch)
tree34802caea5181dc0fb97992746ab99b080448418 /aoc/2021/day02/part1.neb
initial commit
Diffstat (limited to 'aoc/2021/day02/part1.neb')
-rw-r--r--aoc/2021/day02/part1.neb44
1 files changed, 44 insertions, 0 deletions
diff --git a/aoc/2021/day02/part1.neb b/aoc/2021/day02/part1.neb
new file mode 100644
index 0000000..82201ab
--- /dev/null
+++ b/aoc/2021/day02/part1.neb
@@ -0,0 +1,44 @@
+;; 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)))