-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.lisp
36 lines (29 loc) · 1.04 KB
/
day08.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
; Advent of Code 2021 day 8: https://adventofcode.com/2021/day/8
(defun read-digits (stream)
(let ((line (read-line stream nil)))
(when line (read-from-string
(concatenate 'string "#(" (substitute "\" \"" " " line)) ")"))))
(defun read-multiple (stream read-fun)
(loop for item = (funcall read-fun stream)
while item
collect item))
(defun load-numbers (filename)
(with-open-file (in filename :direction :input)
(read-multiple in 'read-digits)))
(defun digits-destructure (digits)
(values (subseq digits 0 9)
(subseq digits 11)))
(defun simple-digit-p (digit)
(case (length digit)
(2 t)
(3 t)
(4 t)
(7 t)
(t nil)))
(defun sum-simple-digits (digits)
(multiple-value-bind (num disp) (digits-destructure digits)
(reduce #'(lambda (prev next) (+ prev (length (remove-if-not 'simple-digit-p next))))
disp)))
(defun day8-1 ()
(let ((lines (load-numbers "input2.txt")))
(reduce '+ (mapcar 'sum-simple-digits lines))))