1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| (defun read-input (filename)
(with-open-file (stream filename)
(loop for number = (read stream nil nil)
while number
collect number)))
(defun to-pairs (hashmap)
(loop for key being the hash-keys of hashmap
using (hash-value value)
collect (cons key value)
)
)
(let ((numbers (read-input "input.txt"))
(counts (make-hash-table)))
(loop for x in numbers do
(incf (gethash x counts 0)))
(let* ((pairs (to-pairs counts))
(sorted (sort pairs (lambda (a b) (> (cdr a) (cdr b))))))
(format t "~a~%" (reduce (lambda (a b) (* a b)) (subseq sorted 0 3) :key #'car))
(format t "~a~%" (reduce (lambda (a b) (* a b)) (subseq sorted (max 0 (- (length sorted) 3))) :key #'car))
)
)
|