Created
December 3, 2023 10:51
-
-
Save widlarizer/078b0a192bfc56a1374a507205454d3b to your computer and use it in GitHub Desktop.
AoC 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'asdf) | |
(require 'alexandria) | |
(require 'cl-ppcre) | |
(defparameter inp "467..114.. | |
...*...... | |
..35..633. | |
......#... | |
617*...... | |
.....+.58. | |
..592..... | |
......755. | |
...$.*.... | |
.664.598..") | |
; (defparameter inp (alexandria:read-file-into-string "3/inp.txt")) | |
(defparameter inp-lines (split-sequence:split-sequence #\Newline inp)) | |
(defparameter len (length inp-lines)) | |
(defparameter nums-re "[0-9]+") | |
(defparameter syms-re "[^0-9\.]") | |
(defun idx-tuple (line match) | |
(let ((start (car match)) (end (car (cdr match)))) | |
(list (subseq line start end) start end))) | |
(defun idx-tuples (line matches) (if matches | |
(cons (idx-tuple line (subseq matches 0 2)) | |
(idx-tuples line (cddr matches))))) | |
(defun matches-on-line (line re) (idx-tuples line (cl-ppcre:all-matches re line))) | |
(defparameter syms (mapcar (alexandria:rcurry #'matches-on-line syms-re) inp-lines)) | |
(defparameter nums (mapcar (alexandria:rcurry #'matches-on-line nums-re) inp-lines)) | |
(defun adj-nums (line-idx) | |
(apply #'append (subseq nums (max (- line-idx 1) 0) (min (+ line-idx 1) len)))) | |
(defun match-s (match) (car match)) | |
(defun match-start (match) (cadr match)) | |
(defun match-end (match) (caddr match)) | |
(defun sym-is-adj (sym num) | |
(if (or (>= (match-end num) (- (match-start sym) 1)) | |
(<= (match-start num) (match-start sym))) | |
(parse-integer (match-s num)))) | |
(trace sym-is-adj) | |
(defun myratio (line-idx sym) | |
(let ((numss (adj-nums line-idx)) | |
(adj-to-num (alexandria:curry #'sym-is-adj sym))) | |
(loop for num in numss and num-idx from 0 | |
when (funcall adj-to-num num) | |
collect (funcall adj-to-num num)))) | |
(trace myratio) | |
(defun ratios-on-line (syms-on-line line-idx) | |
(loop for sym in syms-on-line | |
with res = (and sym (myratio line-idx sym)) | |
when (and res (= 2 (length res))) | |
collect res)) | |
; (defparameter out-messy (loop for syms-on-line in syms and line-idx from 0 | |
; with res = (myratio line-idx syms-on-line) | |
; when (= 2 (length res)) | |
; collect (parts-on-line nums-on-line line-idx))) | |
(defparameter out (apply #'+ (mapcar #'parse-integer (apply #'append out-messy)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment