You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
#+title: Solution to p10 |
|
|
|
#+begin_src emacs-lisp :results none |
|
(with-temp-buffer |
|
(insert-file-contents "input-test") |
|
(advent/replace-multiple-regex-buffer |
|
'(("," . " ") |
|
("^" . "(") |
|
("$" . ")") |
|
("\\[" . "\"") |
|
("\\]" . "\"") |
|
("{" . "(") |
|
("}" . ")") |
|
)) |
|
(goto-char (point-min)) |
|
(insert "(setq data '(") |
|
(goto-char (point-max)) |
|
(insert "))") |
|
(eval-buffer)) |
|
#+end_src |
|
#+begin_src emacs-lisp :results none |
|
(defun zero-one (n) |
|
(if (eq n ?#) 1 0)) |
|
|
|
(setq cleanedup-data (--map (cons (-map #'zero-one (advent/split-string-into-char-list (car it))) (cdr it)) data)) |
|
#+end_src |
|
|
|
|
|
|
|
for part 1 we do not need the last item |
|
#+begin_src emacs-lisp |
|
(setq machines (--map (-drop-last 1 it) cleanedup-data)) |
|
|
|
(defun to-bin (l) |
|
(-sum (--map-indexed (* it (expt 2 it-index)) l))) |
|
|
|
(defun to-mask (l) |
|
(-sum (--map (expt 2 it) l))) |
|
|
|
(setq mask-machines (--map (cons (to-bin (car it)) (-map #'to-mask (cdr it))) machines)) |
|
|
|
(-sum |
|
(-map (lambda (machine) |
|
(-min (-map 'length (--filter (= (car machine) (apply 'logxor it)) |
|
(-powerset (cdr machine)))))) |
|
mask-machines)) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
: 7
|
|
|