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.
1.6 KiB
1.6 KiB
Solution to p2
First load the data as a list of cons cells (start . end)
(with-temp-buffer
(insert-file-contents "input")
(advent/replace-multiple-regex-buffer
'(("," . "\n")
("-" . " . ")
("^" . "(")
("$" . ")")))
(goto-char (point-min))
(insert "(setq data '(")
(goto-char (point-max))
(insert "))")
(eval-buffer))
This is for part 1; only two repetitions
(defun create-range (rng)
(-iterate #'1+ (car rng) (1+ (- (cdr rng) (car rng)))))
(defun invalid-id-p (num)
(let* ((num-digits (1+ (floor (log num 10))))
(mult (expt 10 (floor (/ num-digits 2)))))
(= (mod num mult) (/ num mult))))
(-sum
(--mapcat (-filter #'invalid-id-p it)
(-map #'create-range data)))
24157613387
This is for part 2: very inefficient, but it works
(defun factors (num)
(let ((candidates (-iterate #'1+ 1 num)))
(--filter (= num (* it (floor (/ num it)))) candidates)))
(defun chop-num (num len)
(let ((mult (expt 10 len)))
(--unfold (when (> it 0) (cons (mod it mult) (floor (/ it mult)))) num)))
(defun invalid-id-p (num)
(let* ((num-digits (1+ (floor(log num 10))))
(factors (-drop-last 1 (factors num-digits))))
(--any (= 1 (length it))
(-map #'-distinct (--map (chop-num num it) factors)))))
(-sum
(--mapcat (-filter #'invalid-id-p it)
(-map #'create-range data)))
: 33832678380