about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-12-02T09·56+0100
committertazjin <mail@tazj.in>2020-12-02T10·05+0000
commitea683b1ce834cbacf14fed0f06bad826704d300e (patch)
treeef17b12aad52beee42c54a6202b60b9c791e2d7b
parent50b32531eee1b1c4bca427af453fbdf474746c3e (diff)
feat(tazjin/aoc2020): Add solution for day 2 r/1979
Change-Id: Id149ff13d1e903a578cdcdc3a8d0045cfefaecfa
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2224
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
-rw-r--r--users/tazjin/aoc2020/default.nix2
-rw-r--r--users/tazjin/aoc2020/solution-day2.el54
2 files changed, 55 insertions, 1 deletions
diff --git a/users/tazjin/aoc2020/default.nix b/users/tazjin/aoc2020/default.nix
index 830b1f7cd3..ebb6e7b0c0 100644
--- a/users/tazjin/aoc2020/default.nix
+++ b/users/tazjin/aoc2020/default.nix
@@ -15,7 +15,7 @@ let
     name = day;
     value = depot.nix.writeElispBin {
       name = "aoc2020";
-      deps = p: with p; [ dash s ht ];
+      deps = p: with p; [ dash s ht p.f ];
       src = ./. + ("/" + f);
     };
   }) solutionFiles;
diff --git a/users/tazjin/aoc2020/solution-day2.el b/users/tazjin/aoc2020/solution-day2.el
new file mode 100644
index 0000000000..5993bf3407
--- /dev/null
+++ b/users/tazjin/aoc2020/solution-day2.el
@@ -0,0 +1,54 @@
+;; Advent of Code 2020 - Day 2
+
+(require 'cl-lib)
+(require 'f)
+(require 'ht)
+(require 's)
+(require 'seq)
+
+(defvar day2/input
+  ;; This one was too large to inline.
+  (s-lines (f-read "/tmp/aoc/day2.txt")))
+
+(defun day2/count-letters (password)
+  (let ((table (ht-create)))
+    (cl-loop for char across password
+             for current = (ht-get table char)
+             do (ht-set table char
+                        (if current (+ 1 current) 1)))
+    table))
+
+(defun day2/parse (input)
+  (let* ((split (s-split " " input))
+         (range (s-split "-" (car split))))
+    (list (string-to-number (car range))
+          (string-to-number (cadr range))
+          (string-to-char (cadr split))
+          (caddr split))))
+
+(defun day2/count-with-validation (func)
+  (length (-filter
+           (lambda (password)
+             (and (not (seq-empty-p password))
+                  (apply func (day2/parse password))))
+           day2/input)))
+
+;; Puzzle 1
+
+(defun day2/validate-oldjob (min max char password)
+  (let ((count (ht-get (day2/count-letters password) char)))
+    (when count
+      (and (>= count min)
+           (<= count max)))))
+
+(message "Solution to day2/1: %s"
+         (day2/count-with-validation #'day2/validate-oldjob))
+
+;; Puzzle 2
+
+(defun day2/validate-toboggan (pos1 pos2 char password)
+  (xor (= char (aref password (- pos1 1)))
+       (= char (aref password (- pos2 1)))))
+
+(message "Solution to day2/2: %s"
+         (day2/count-with-validation #'day2/validate-toboggan))