Skip to content

Commit

Permalink
Add EXTRACT-{POSITIVE-}INTEGERS to utils, and pull WHILE from quickutil
Browse files Browse the repository at this point in the history
  • Loading branch information
iamFIREcracker committed Dec 6, 2023
1 parent e8e1642 commit c2ec0f2
Show file tree
Hide file tree
Showing 36 changed files with 86 additions and 74 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/.DS_Store
/.common-lisp/
/.quicklisp/
/src/.DS_Store
/vendor/.common-lisp/
/vendor/.quicklisp/
/vendor/quickutil
2 changes: 2 additions & 0 deletions .lispwords
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
(define-test 2)
(destructuring-bind 2)
(dolist 1)
(dolist+ 1)
(dorange 1)
(dorange 1)
(dorangei 1)
(doseq 1)
(dotimes 1)
(ecase 1)
(flet 1)
(fn 1)
(gathering 0)
(handler-case 1)
(if 1)
Expand Down
2 changes: 2 additions & 0 deletions package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
:parse-integers
:read-integer
:parse-char
:extract-positive-integers
:extract-integers

:define-solution
:define-test))
Expand Down
3 changes: 1 addition & 2 deletions src/2015/day15.lisp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(defpackage :aoc/2015/15 #.cl-user::*aoc-use*)
(in-package :aoc/2015/15)

(defun parse-properties (string)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "-?\\d+" string)))
(defun parse-properties (string) (extract-integers string))
(defun calories (props) (nth 4 props))

(defun parse-ingredient (string)
Expand Down
6 changes: 2 additions & 4 deletions src/2015/day21.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
("Defense +2" 40 0 2)
("Defense +3" 80 0 3)))

(defun parse-boss (lines)
(mapcar #'parse-integer
(cl-ppcre:all-matches-as-strings "\\d+"
(format nil "~{~A ~}" lines))))
(defun parse-boss (&optional (lines (uiop:read-file-lines #P"src/2015/day21.txt")))
(extract-positive-integers (format nil "~{~A ~}" lines)))

(defun all-items-combinations ()
(loop for w in *weapons* append
Expand Down
7 changes: 3 additions & 4 deletions src/2015/day22.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@
;; Boss logic
(defstruct (boss (:type list)) life damage)

(defun parse-boss (lines)
(mapcar #'parse-integer
(cl-ppcre:all-matches-as-strings "\\d+"
(format nil "~{~A ~}" lines))))
(defun parse-boss (&optional (lines (uiop:read-file-lines #P"src/2015/day22.txt")))
(extract-positive-integers (format nil "~{~A ~}" lines)))

(defun boss-attack (boss player)
(max 1 (- (boss-damage boss) (player-armor-effect player))))

Expand Down
3 changes: 1 addition & 2 deletions src/2018/day10.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
(mapcar #'parse-star data))

(defun parse-star (string)
(destructuring-bind (x y vx vy)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "-?\\d+" string))
(destructuring-bind (x y vx vy) (extract-integers string)
(make-star :pos (list x y) :vel (list vx vy))))


Expand Down
1 change: 0 additions & 1 deletion src/2018/day13.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
(< (realpart c1) (realpart c2)))))

(defun cart-move (cart track)
(declare (optimize (speed 3)))
(with-slots (pos dir choices) cart
(incf pos dir)
(let ((ch (gethash pos track)))
Expand Down
7 changes: 2 additions & 5 deletions src/2021/day04.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
boards)

(defun parse-bingo (data)
(let ((to-draw (extract-integers (car data)))
(let ((to-draw (extract-positive-integers (car data)))
(boards (parse-boards (cdr data))))
(make-bingo :to-draw to-draw :boards boards)))

Expand All @@ -19,13 +19,10 @@
(let ((lines (subseq data 1 6)))
(looping
(dolist (s lines)
(let ((row (extract-integers s)))
(let ((row (extract-positive-integers s)))
(collect! row))))))
(parse-boards (subseq data 6)))))

(defun extract-integers (s)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))


(defun play (game)
(looping
Expand Down
3 changes: 1 addition & 2 deletions src/2021/day05.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
(defun parse-lines (data)
(mapcar #'parse-line data))

(defun parse-line (string)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string)))
(defun parse-line (string) (extract-positive-integers string))


(defun part1 (lines)
Expand Down
3 changes: 1 addition & 2 deletions src/2021/day07.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
(in-package :aoc/2021/07)


(defun parse-crabs (data)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" (first data))))
(defun parse-crabs (data) (extract-positive-integers (first data)))


(defun minimize-fuel (crabs distance-fun)
Expand Down
8 changes: 3 additions & 5 deletions src/2021/day19.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

;; Input
(defun parse-scanner (paragraph &aux (paragraph (cl-ppcre:split "\\n" paragraph)))
(flet ((numbers (string)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "-?\\d+" string))))
(cons
(first (numbers (first paragraph)))
(mapcar #'numbers (rest paragraph)))))
(cons
(first (extract-integers (first paragraph)))
(mapcar #'extract-integers (rest paragraph))))
(defun id (scn) (car scn))
(defun beacons (scn) (cdr scn))

Expand Down
2 changes: 1 addition & 1 deletion src/2022/day04.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


(defun assignment-pairs ()
(mapcar [mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" _)]
(mapcar [extract-positive-integers _]
(uiop:read-file-lines #P"src/2022/day04.txt")))

(defun solution-run ()
Expand Down
3 changes: 1 addition & 2 deletions src/2022/day05.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
for i from 1 do
(setf (aref array i) (loop for ch in stack when (alpha-char-p ch) collect ch)))
array)
(mapcar [mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" _)]
moves)))))
(mapcar [extract-positive-integers _] moves)))))

(defun top-crates (stacks)
(format nil "~{~A~}" (map 'list #'car (subseq stacks 1))))
Expand Down
6 changes: 2 additions & 4 deletions src/2022/day11.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
throw-if-not-div)

(defun parse-number (s) (car (parse-numbers s)))
(defun parse-numbers (s)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))
(defun parse-numbers (s) (extract-positive-integers s))

(defun parse-operation (s)
(bnd1 (old (symb "_"))
Expand Down Expand Up @@ -121,8 +120,7 @@
throw-if-not-div)

(defun parse-number (s) (car (parse-numbers s)))
(defun parse-numbers (s)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))
(defun parse-numbers (s) (extract-positive-integers s))

(defun parse-operation (s)
(bnd1 (old (symb "_"))
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day14.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(defun parse-cave (&optional (file #P"src/2022/day14.txt"))
(let ((cave (make-hash-table :test 'equal)))
(dolist (s (uiop:read-file-lines file))
(loop for (from-col from-row to-col to-row) on (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)) by #'cddr
(loop for (from-col from-row to-col to-row) on (extract-positive-integers s) by #'cddr
while to-row do (loop with delta-row = (<=> to-row from-row)
with delta-col = (<=> to-col from-col)
for row = from-row then (+ row delta-row)
Expand Down
4 changes: 1 addition & 3 deletions src/2022/day15.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
(defun r (s) (manhattan-distance (pos s) (beacon s)))

(defun parse-sensor (string)
(destructuring-bind (sx sy bx by)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "-?\\d+" string))
(destructuring-bind (sx sy bx by) (extract-integers string)
(make-sensor :pos (list sx sy) :beacon (list bx by))))

(defun sensors (&optional (file #P"src/2022/day15.txt"))
(mapcar #'parse-sensor (uiop:read-file-lines file)))


(defun bounding-square (sensors)
(loop for s in sensors
for r = (r s) for (x y) = (car s)
Expand Down
9 changes: 3 additions & 6 deletions src/2022/day15.lisp.1
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

(defun parse-input (&optional (file #P"src/2022/day15.txt"))
(loop for s in (uiop:read-file-lines file)
for (sx sy bx by) = (cl-ppcre:all-matches-as-strings "-?\\d+" s)
collect (mapcar #'parse-integer (list sx sy bx by))))
collect (extract-integers s)))
(defun bounding-box (&optional (input (parse-input)))
(loop for (sx sy bx by) in input
minimize sx into min-x maximize sx into max-x
Expand Down Expand Up @@ -42,8 +41,7 @@
#; Scratch
(defun parse-input (&optional (file #P"src/2022/day15.txt"))
(loop for s in (uiop:read-file-lines file)
for (sx sy bx by) = (mapcar #'parse-integer
(cl-ppcre:all-matches-as-strings "-?\\d+" s))
for (sx sy bx by) = (extract-integers s)
collect (list (list sx sy) (list bx by))))
(defun bounding-box (&optional (input (parse-input)))
(loop for (sensor beacon) in input
Expand Down Expand Up @@ -95,8 +93,7 @@
(defun x (pos) (first pos))
(defun y (pos) (second pos))
(defun parse-nanobot (string)
(destructuring-bind (sx sy bx by)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "-?\\d+" string))
(destructuring-bind (sx sy bx by) (extract-integers string)
(make-nanobot :pos (list sx sy) :r (manhattan-distance (list sx sy)
(list bx by)))))

Expand Down
2 changes: 1 addition & 1 deletion src/2022/day16.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

(defun parse-valve (s)
(let ((names (mapcar #'symb (cl-ppcre:all-matches-as-strings "[A-Z]{2}" s)))
(rate (first (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))))
(rate (first (extract-positive-integers s))))
(make-valve :name (car names)
:rate rate
:connected-to (rest names))))
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(destructuring-bind (id ore-robot-cost clay-robot-cost
obsidian-robot-ore-cost obsidian-robot-clay-cost
geode-robot-ore-cost geode-robot-obsidian-cost)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string))
(extract-positive-integers string)
(make-blueprint :id id
:costs (list
(list
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(destructuring-bind (id ore-robot-cost clay-robot-cost
obsidian-robot-ore-cost obsidian-robot-clay-cost
geode-robot-ore-cost geode-robot-obsidian-cost)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string))
(extract-positive-integers string)
(make-blueprint :id id
:costs (list
(list
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(destructuring-bind (id ore-robot-cost clay-robot-cost
obsidian-robot-ore-cost obsidian-robot-clay-cost
geode-robot-ore-cost geode-robot-obsidian-cost)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string))
(extract-positive-integers string)
(make-blueprint :id id
:costs (list
(list
Expand Down
5 changes: 1 addition & 4 deletions src/2022/day19.lisp.3
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(destructuring-bind (id ore-robot-cost clay-robot-cost
obsidian-robot-ore-cost obsidian-robot-clay-cost
geode-robot-ore-cost geode-robot-obsidian-cost)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string))
(extract-positive-integers string)
(make-blueprint :id id
:ore (list ore-robot-cost 0 0)
:clay (list clay-robot-cost 0 0)
Expand All @@ -18,9 +18,6 @@
(defun blueprints (&optional (file #P"src/2022/day19.txt"))
(mapcar #'parse-blueprint (uiop:read-file-lines file)))

(defmacro while (test-form &body body)
`(loop while ,test-form ,@body))

(defun can-collect-ore? (target blueprint remaining)
(cond ((< remaining 0) nil)
((<= target remaining) t)
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.4
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
id ore clay obsidian geode)

(defun parse-blueprint (string)
(bnd1 (nums (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string)))
(bnd1 (nums (extract-positive-integers string))
(make-blueprint :id (elt nums 0)
:ore (subseq nums 1 2)
:clay (subseq nums 2 3)
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.5
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
id ore clay obsidian geode)

(defun parse-blueprint (string)
(bnd1 (nums (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string)))
(bnd1 (nums (extract-positive-integers string))
(make-blueprint :id (elt nums 0)
:ore (subseq nums 1 2)
:clay (subseq nums 2 3)
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.6.part1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(destructuring-bind (id ore-robot-cost clay-robot-cost
obsidian-robot-ore-cost obsidian-robot-clay-cost
geode-robot-ore-cost geode-robot-obsidian-cost)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string))
(extract-positive-integers string)
(make-blueprint :id id
:costs (list
(list
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.7
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(destructuring-bind (id ore-robot-cost clay-robot-cost
obsidian-robot-ore-cost obsidian-robot-clay-cost
geode-robot-ore-cost geode-robot-obsidian-cost)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string))
(extract-positive-integers string)
(make-blueprint :id id
:costs (list
(list
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day19.lisp.8
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
id ore clay obsidian geode)

(defun parse-blueprint (string)
(bnd1 (nums (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" string)))
(bnd1 (nums (extract-positive-integers string))
(make-blueprint :id (elt nums 0)
:ore (subseq nums 1 2)
:clay (subseq nums 2 3)
Expand Down
2 changes: 1 addition & 1 deletion src/2022/day22.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:col-max col-max)))

(defun parse-instructions (s)
(let ((steps (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))
(let ((steps (extract-positive-integers s))
(turns (cl-ppcre:all-matches-as-strings "[LR]" s)))
(list steps turns)))

Expand Down
2 changes: 1 addition & 1 deletion src/2022/day22.lisp.part1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:col-max col-max)))

(defun parse-instructions (s)
(let ((steps (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))
(let ((steps (extract-positive-integers s))
(turns (cl-ppcre:all-matches-as-strings "[LR]" s)))
(list steps turns)))

Expand Down
2 changes: 1 addition & 1 deletion src/2022/day22.lisp.part2
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
:col-max col-max)))

(defun parse-instructions (s)
(let ((steps (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))
(let ((steps (extract-positive-integers s))
(turns (cl-ppcre:all-matches-as-strings "[LR]" s)))
(list steps turns)))

Expand Down
6 changes: 3 additions & 3 deletions src/2023/day04.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

(defun winning-numbers (s)
(bnd1 (numbers (second (split-sequence:split-sequence #\: s)))
(destructuring-bind (winning-numbers your-numbers) (split-sequence:split-sequence #\| numbers)
(intersection (mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" winning-numbers))
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" your-numbers))))))
(destructuring-bind (winning yours) (split-sequence:split-sequence #\| numbers)
(intersection (extract-positive-integers winning)
(extract-positive-integers yours)))))

(defun card-points (s) (ash 1 (1- (length (winning-numbers s)))))
#+#:excluded (card-points "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53")
Expand Down
6 changes: 6 additions & 0 deletions src/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@
"Return the first character of `string`"
(char string 0))

(defun extract-positive-integers (s)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "\\d+" s)))

(defun extract-integers (s)
(mapcar #'parse-integer (cl-ppcre:all-matches-as-strings "-?\\d+" s)))

;;;; Problems -----------------------------------------------------------------
(defmacro define-solution ((year day)
(arg &optional (reader 'identity))
Expand Down
Loading

0 comments on commit c2ec0f2

Please sign in to comment.