about summary refs log tree commit diff
path: root/users/tazjin/aoc2020/solution-day3.el
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-12-04T09·59+0100
committertazjin <mail@tazj.in>2020-12-04T11·37+0000
commita9980bb631c954fd0e4d022816b4cacfeb9ce0d7 (patch)
treefb817b9933387e1803d35257579475ebb7aaedc0 /users/tazjin/aoc2020/solution-day3.el
parentea683b1ce834cbacf14fed0f06bad826704d300e (diff)
feat(tazjin/aoc2020): Add solution for day 3 r/1980
Change-Id: I84147731e1508032510a52cda28be74bbbb17c61
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2225
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/aoc2020/solution-day3.el')
-rw-r--r--users/tazjin/aoc2020/solution-day3.el43
1 files changed, 43 insertions, 0 deletions
diff --git a/users/tazjin/aoc2020/solution-day3.el b/users/tazjin/aoc2020/solution-day3.el
new file mode 100644
index 000000000000..80ea4a226405
--- /dev/null
+++ b/users/tazjin/aoc2020/solution-day3.el
@@ -0,0 +1,43 @@
+;; Advent of Code 2020 - Day 3
+
+(require 'cl-lib)
+(require 'dash)
+(require 'f)
+(require 's)
+(require 'seq)
+
+(setq day3/input
+      (-filter (lambda (s) (not (seq-empty-p s)))
+         (s-lines (f-read "/tmp/aoc/day3.txt"))))
+
+(setq day3/input-width (length (elt day3/input 0)))
+(setq day3/input-height (length day3/input))
+
+(defun day3/thing-at-point (x y)
+  "Pun intentional."
+  (when (>= day3/input-height y)
+    (let ((x-repeated (mod (- x 1) day3/input-width)))
+      (elt (elt day3/input (- y 1)) x-repeated))))
+
+(defun day3/slope (x-steps y-steps)
+  "Produce the objects encountered through this slope until the
+  bottom of the map."
+  (cl-loop for x from 1 by x-steps
+           for y from 1 to day3/input-height by y-steps
+           collect (day3/thing-at-point x y)))
+
+;; Puzzle 1
+
+(defun day3/count-trees (x-steps y-steps)
+  (cl-loop for thing being the elements of (day3/slope x-steps y-steps)
+           count (= thing ?#)))
+
+(message "Solution to day3/1: One encounters %s trees" (day3/count-trees 3 1))
+
+;; Puzzle 2
+
+(message "Solution to day3/2 %s" (* (day3/count-trees 1 1)
+                                    (day3/count-trees 3 1)
+                                    (day3/count-trees 5 1)
+                                    (day3/count-trees 7 1)
+                                    (day3/count-trees 1 2)))