diff options
author | Vincent Ambo <mail@tazj.in> | 2020-12-08T23·10+0100 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2020-12-08T23·13+0000 |
commit | 93f0ab5af86d8bb285a19de00766e9a4a184afdc (patch) | |
tree | 773b816e40cb472409f7713cbab14386a032fb17 /users | |
parent | 3061b3228f89fd1c69d1f1f66b264e7ca8396575 (diff) |
feat(tazjin/aoc2020): Add solution for day 8, part 1 r/1993
I'm too tired for part 2. Change-Id: Ic7058344806466276e3792e9ff9bbf660a18f672 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2239 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-rw-r--r-- | users/tazjin/aoc2020/solution-day8.el | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/users/tazjin/aoc2020/solution-day8.el b/users/tazjin/aoc2020/solution-day8.el new file mode 100644 index 000000000000..eb84badde398 --- /dev/null +++ b/users/tazjin/aoc2020/solution-day8.el @@ -0,0 +1,33 @@ +;; Advent of Code 2020 - Day + +(require 'cl-lib) +(require 'dash) +(require 'f) +(require 's) + +(setq day8/input + (apply #'vector + (-map (lambda (s) + (pcase-let ((`(,op ,val) (s-split " " s t))) + (cons (intern op) (string-to-number val)))) + (s-lines (s-chomp (f-read "/tmp/aoc/day8.txt")))))) + +(defun day8/step (code position acc) + (let ((current (aref code position))) + (aset code position nil) + (pcase current + ('() (cons 'final acc)) + (`(nop . ,val) (cons (+ position 1) acc)) + (`(acc . ,val) (cons (+ position 1) (+ acc val))) + (`(jmp . ,val) (cons (+ position val) acc))))) + +;; Puzzle 1 + +(message "Solution to day8/1: %s" + (let ((code (copy-sequence day8/input)) + (position 0) + (acc 0)) + (cl-loop for next = (day8/step code position acc) + when (equal 'final (car next)) return (cdr next) + do (setq position (car next)) + do (setq acc (cdr next))))) |