-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday16.lisp
More file actions
64 lines (52 loc) · 2.3 KB
/
Copy pathday16.lisp
File metadata and controls
64 lines (52 loc) · 2.3 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(defpackage :aoc/2015/16 #.cl-user::*aoc-use*)
(in-package :aoc/2015/16)
(defparameter *mfcsam-message* '((:children . 3)
(:cats . 7)
(:samoyeds . 2)
(:pomeranians . 3)
(:akitas . 0)
(:vizslas . 0)
(:goldfish . 5)
(:trees . 3)
(:cars . 2)
(:perfumes . 1)))
(defun compound-name (string) (as-keyword string))
(defun parse-compound (string)
(cl-ppcre:register-groups-bind ((#'compound-name name) (#'parse-integer value))
("(\\w+): (\\d+)" string)
(cons name value)))
(defun parse-compounds (string)
(mapcar #'parse-compound
(cl-ppcre:all-matches-as-strings "\\w+: \\d+" string)))
(defun parse-aunt-memories (string)
(cl-ppcre:register-groups-bind ((#'parse-integer n) (#'parse-compounds compounds))
("Sue (\\d+): (.*)" string)
(cons n compounds)))
(defun aunt-name (aunt) (first aunt))
(defun aunt-compound (aunt name) (rest (assoc name (rest aunt))))
(defun parse-memories (lines) (mapcar #'parse-aunt-memories lines))
(defun absent-or-matches (value) (lambda (v) (or (not v) (= v value))))
(defun part1 (memoirs &aux (remaining memoirs))
(loop for (name . value) in *mfcsam-message* do
(setf remaining (remove-if-not
(absent-or-matches value)
remaining
:key (partial-1 #'aunt-compound _ name))))
(aunt-name (first remaining)))
(defun absent-or-matches-with-ranges (name value)
#'(lambda (v)
(or (not v)
(case name
((:cats :trees) (> v value))
((:pomeranians :goldfish) (< v value))
(t (= v value))))))
(defun part2 (memoirs &aux (remaining memoirs))
(loop for (name . value) in *mfcsam-message* do
(setf remaining (remove-if-not
(absent-or-matches-with-ranges name value)
remaining
:key (partial-1 #'aunt-compound _ name))))
(aunt-name (first remaining)))
(define-solution (2015 16) (memoirs parse-memories)
(values (part1 memoirs) (part2 memoirs)))
(define-test (2015 16) (40 241))