about summary refs log tree commit diff
path: root/users/tazjin/aoc2023/day3.el
blob: dd39c1b836d30380c29d7e5d709220a73832a204 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(defun aoc23d3-symbol-p (c)
  (not (or (= c ? )
           (and (>= c ?0)
                (<= c ?9)))))

(defun rectangle-for-bounds (bounds)
  (let* ((start (save-excursion
                     (goto-char (car bounds))
                     (let ((col (current-column)))
                       (forward-line -1)
                       (move-to-column (max 0 (1- col))))
                     (point)))
         (end (save-excursion
                (goto-char (cdr bounds))
                (let ((col (current-column)))
                  (forward-line 1)
                  (move-to-column (1+ col)))
                (point))))
    (list start end)))

(defun get-machine-part ()
  (interactive)
  (when-let* ((num-raw (number-at-point))
              (num (abs num-raw))
              ;; handles negative number edge case (bounds contain the `-')
              (bounds-raw (bounds-of-thing-at-point 'number))
              (bounds (if (< num-raw 0)
                          (cons (1- (car bounds-raw)) (cdr bounds-raw))
                        bounds-raw))
              (rectangle (rectangle-for-bounds bounds))
              (neighbours (apply #'concat
                                 (apply #'extract-rectangle rectangle))))
    (if (-any #'aoc23d3-symbol-p (string-to-list neighbours))
        (cons num rectangle)
      (cons nil rectangle))))


(defun find-machine-parts (input)
  (with-temp-buffer
    (insert input)
    (goto-char (point-min))
    (save-excursion
      (replace-string "." " "))

    (cl-loop while (forward-word)
             for result = (get-machine-part)
             when (car result) collect (car result))))


;; debugging

(defvar aoc23d3-example "467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..")

(defvar aoc23d3-example2 "12.......*..
+.........34
.......-12..
..78........
..*....60...
78..........
.......23...
....90*12...
............
2.2......12.
.*.........*
1.1.......56")

(defvar aoc23d3-example3 "243.
..*.
....")

(defun aoc23d3-debug (p)
  "Interactive debugger for the solution, can be bound to a key in
an input buffer. Dots should already have been replaced with
spaces."
  (interactive "P")
  (unless p
    (goto-char aoc23d3-last))
  (rectangle-mark-mode 1)
  (forward-word)
  (setq aoc23d3-last (point))
  (pcase (get-machine-part)
    (`(nil ,b ,e) (progn (set-mark b)
                          (goto-char e)
                          (set-face-attribute 'region nil :background "#FAA0A0")))
    (`(,num ,b ,e) (progn (set-mark b)
                          (goto-char e)
                          (set-face-attribute 'region nil :background "#d1ffbd")))
    (other (deactivate-mark))))

(cl-assert (= 4361 (-sum (find-machine-parts aoc23d3-example))) nil
           "example from website is working")

(cl-assert (= 413 (-sum (find-machine-parts aoc23d3-example2))) nil
           "example from subreddit is working")

(cl-assert (= 243 (-sum (find-machine-parts aoc23d3-example3))) nil
           "example from telegram is working")

;; day 1 (incomplete)

(-sum (find-machine-parts (s-trim (f-read "~/Downloads/input.txt"))))