Skip to content

Commit 8f31f60

Browse files
More commentary to 2024/05 shortcut
1 parent 22574c0 commit 8f31f60

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/2024/day05.lisp

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
(push b (gethash a before-than))))
99
(mapcar #'extract-positive-integers updates))))
1010

11-
;; The problem description clearly states that any ordering rule involving pages
12-
;; not included in the update should not be included, so we just have to rely
13-
;; on the explicit rules, without any recursion.
11+
12+
;; By taking a good look at the input we can notice that for every pair of
13+
;; pages p1 and p2 listed in any input page update, there exists an
14+
;; **explicit** rule which either put p1 before p2 or p2 before p1. This means
15+
;; we don't have to mess with recursion to figure out if p1 should be printed
16+
;; before p2, or vice-versa.
1417
(defun before? (rules p1 p2)
15-
(member p2 (gethash p1 rules)))
18+
(aprog1 (member p2 (gethash p1 rules))
19+
(assert (xor it
20+
(member p1 (gethash p2 rules))))))
1621

1722
(defun correctly-ordered? (rules update)
1823
(looping

0 commit comments

Comments
 (0)