Skip to content

Instantly share code, notes, and snippets.

@widlarizer
Created December 1, 2023 17:57
Show Gist options
  • Save widlarizer/90d94c5ba3cf413ed1b49cda9d2d3836 to your computer and use it in GitHub Desktop.
Save widlarizer/90d94c5ba3cf413ed1b49cda9d2d3836 to your computer and use it in GitHub Desktop.
AoC 23
(ql:quickload :cl-ppcre)
(ql:quickload "split-sequence")
(ql:quickload :alexandria)
; (load "inp.lisp")
(defvar inp "two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen")
(defvar inp-lines (split-sequence:split-sequence #\Newline inp))
(defvar nums '("zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
(defun pipe-cat (left right) (concatenate 'string left "|" right))
; (defvar nums-re (reduce #'pipe-cat nums))
(defvar nums-re (concatenate 'string (pipe-cat "(?=([0-9]" (reduce #'pipe-cat nums)) "))"))
(princ nums-re)
(defun normalize (s) (let ((num (digit-char-p (char s 0))))
(if num
num
(position s nums :test #'string=))))
(defun cat-first-and-last (s)
(+ (* 10 (car s)) (car (last s))))
(defun first-num (s)
(cl-ppcre:all-matches-as-strings nums-re s))
; (defvar first-nums (mapcar #'first-num inp-lines))
; (princ first-nums)
; (defvar normalized (mapcar (lambda (l) (mapcar #'normalize l)) first-nums))
; (princ (mapcar (lambda (x) (list (first x) (car (last x)))) normalized))
; (princ (reduce #'+ (mapcar #'cat-first-and-last normalized)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment