aboutsummaryrefslogtreecommitdiff
path: root/aoc/2021/day02
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/2021/day02')
-rw-r--r--aoc/2021/day02/input.txt6
-rw-r--r--aoc/2021/day02/part1.neb44
2 files changed, 50 insertions, 0 deletions
diff --git a/aoc/2021/day02/input.txt b/aoc/2021/day02/input.txt
new file mode 100644
index 0000000..b7172ac
--- /dev/null
+++ b/aoc/2021/day02/input.txt
@@ -0,0 +1,6 @@
+forward 5
+down 5
+forward 8
+up 3
+down 8
+forward 2
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)))