about summary refs log tree commit diff
path: root/fun/aoc2019/solution-day4.el
blob: 2805f3f4e9cd85324d1e2f8d6a11cd39448c0922 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
;; -*- lexical-binding: t; -*-
;; Advent of Code 2019 - Day 4

(require 'cl-lib)
(require 'dash)

;; Puzzle 1

(defun day4/to-digits (num)
  "Convert NUM to a list of its digits."
  (cl-labels ((steps (n digits)
                     (if (= n 0) digits
                       (steps (/ n 10) (cons (% n 10) digits)))))
    (steps num '())))

(defvar day4/input (-map #'day4/to-digits (number-sequence 128392 643281)))

(defun day4/filter-password (digits)
  "Determines whether the given rules match the supplied
  number."

  (and
   ;; It is a six digit number
   (= 6 (length digits))

   ;; Value is within the range given in puzzle input
   ;; (noop because the range is generated from the input)

   ;; Two adjacent digits are the same (like 22 in 122345).
   (car (-reduce-from (-lambda ((acc . prev) next)
                        (cons (or acc (= prev next)) next))
                      '(nil . 0) digits))

   ;; Going from left to right, the digits never decrease; they only
   ;; ever increase or stay the same (like 111123 or 135679).
   (car (-reduce-from (-lambda ((acc . prev) next)
                        (cons (and acc (>= next prev)) next))
                      '(t . 0) digits))))

;; Puzzle 2
;;
;; Additional criteria: If there's matching digits, they're not in a group.

(cl-defstruct day4/acc state prev count)

(defun day4/filter-longer-groups (digits)
  (let ((res (-reduce-from
              (lambda (acc next)
                (cond ;; sequence is broken and count was at 1 ->
                 ;; match!
                 ((and (= (day4/acc-count acc) 2)
                       (/= (day4/acc-prev acc) next))
                  (setf (day4/acc-state acc) t))

                 ;; sequence continues, counter increment!
                 ((= (day4/acc-prev acc) next)
                  (setf (day4/acc-count acc) (+ 1 (day4/acc-count acc))))

                 ;; sequence broken, reset counter
                 ((/= (day4/acc-prev acc) next)
                  (setf (day4/acc-count acc) 1)))

                (setf (day4/acc-prev acc) next)
                acc)
              (make-day4/acc :prev 0 :count 0) digits)))
    (or (day4/acc-state res)
        (= 2 (day4/acc-count res)))))

(let* ((simple (-filter #'day4/filter-password day4/input))
       (complex (-filter #'day4/filter-longer-groups simple)))
  (message "Solution to day4/1: %d" (length simple))
  (message "Solution to day4/2: %d" (length complex)))