aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2024/puzzle-2.py18
-rw-r--r--2024/puzzle-3.lisp39
-rw-r--r--2024/util.lisp2
3 files changed, 30 insertions, 29 deletions
diff --git a/2024/puzzle-2.py b/2024/puzzle-2.py
index 98f2efd..ee5bf77 100644
--- a/2024/puzzle-2.py
+++ b/2024/puzzle-2.py
@@ -22,21 +22,21 @@ print(f"Round 1: {len(good_levels)}")
def check_two_levels(x, y, decreasing):
diff = abs(x - y)
- return not ((decreasing and x < y) or (not decreasing and x > y) or not (diff <= 3 and diff >= 1))
+ return not ((decreasing and x < y)\
+ or (not decreasing and x > y) \
+ or not (diff <= 3 and diff >= 1))
def is_good_level_2(level):
# 1) Is decreasing
# 2) Sliding window of two cells (x, y) => 1 <= |x-y| <= 3
# 3) Can remove any one item to make it safe
- ret = is_good_level_1(level)
- if not ret:
- # Consider slices of the level and check if they're good
- slices = [level[:i] + level[i + 1:] for i in range(len(level))]
- for s in slices:
- if is_good_level_1(s):
- return True
- else:
+ if is_good_level_1(level):
return True
+ # Consider slices of the level and check if they're good
+ slices = [level[:i] + level[i + 1:] for i in range(len(level))]
+ for s in slices:
+ if is_good_level_1(s):
+ return True
return False
good_levels = [level for level in levels if is_good_level_2(level)]
diff --git a/2024/puzzle-3.lisp b/2024/puzzle-3.lisp
index cca2f43..46b2902 100644
--- a/2024/puzzle-3.lisp
+++ b/2024/puzzle-3.lisp
@@ -3,19 +3,19 @@
(defparameter input (uiop:read-file-string "3-input"))
(defun is-good-mul (str)
- (let* ((delimiter (search "," str))
- (end (search ")" str)))
- (if (or (null delimiter) (null end)
- (> delimiter end)
- (not (eq #\( (char str 3))))
- nil
- (let* ((first-arg (subseq str 4 delimiter))
- (second-arg (subseq str (1+ delimiter) end)))
- (and (every #'digit-char-p first-arg)
- (every #'digit-char-p second-arg))))))
-
-(defun trim-mul (str)
- (subseq str 0 (1+ (search ")" str))))
+ (let ((start (search "(" str))
+ (middle (search "," str))
+ (end (search ")" str)))
+ (and (not (null start)) (not (null middle)) (not (null end))
+ ;; mul( <- 3 character
+ (eq start 3)
+ ;; Simple to understand
+ (< start end)
+ (< start middle)
+ (< middle end)
+ ;; Make sure the arguments are purely numbers
+ (every #'digit-char-p (subseq str (1+ start) middle))
+ (every #'digit-char-p (subseq str (1+ middle) end)))))
(defun parse-mul (str)
(let ((start (search "(" str))
@@ -28,9 +28,11 @@
(let ((possible (search-all "mul" line)))
(--> (cdr possible)
(append _ (list (length line)))
+ ;; index of mul -> (position substring)
(mapcar (lambda (z1 z2) (cons z1 (subseq line z1 z2))) possible _)
+ ;; remove any bad muls
(remove-if-not (lambda (x) (is-good-mul (cdr x))) _)
- (mapcar (lambda (x) (cons (car x) (trim-mul (cdr x)))) _)
+ ;; parse muls
(mapcar (lambda (x) (cons (car x) (parse-mul (cdr x)))) _))))
(format t "Round 1: ~a~%"
@@ -40,11 +42,10 @@
(defun parse-input-conds (input)
(let ((dos (search-all "do()" input))
(donts (search-all "don't()" input)))
- (cons
- '(do . 0)
- (sort (append (mapcar (lambda (x) (cons 'do x)) dos)
- (mapcar (lambda (x) (cons 'dont x)) donts))
- (lambda (x y) (< (cdr x) (cdr y)))))))
+ (--> (append (mapcar (lambda (x) (cons 'do x)) dos)
+ (mapcar (lambda (x) (cons 'dont x)) donts))
+ (sort _ (lambda (x y) (< (cdr x) (cdr y))))
+ (cons '(do . 0) _))))
(defun current-cond (pos conds)
(caar (last (remove-if (lambda (x) (> (cdr x) pos)) conds))))
diff --git a/2024/util.lisp b/2024/util.lisp
index 11ec230..71a7e13 100644
--- a/2024/util.lisp
+++ b/2024/util.lisp
@@ -2,7 +2,7 @@
(if (null functions)
first
`(let* ,(loop :for f :in (cons first functions)
- appending `((_ ,f)))
+ :appending `((_ ,f)))
_)))
(defun search-all (substr str &optional acc len)