From cbe01ffb4bb3fea45692696e41726e4d645a0d12 Mon Sep 17 00:00:00 2001 From: Hane Date: Wed, 13 Dec 2023 02:00:11 +0100 Subject: [PATCH] full aoc1 --- .gitignore | 1 + src/aoc1-2.lisp | 122 +++++++++++++++++++++++++++++++++++++++++++++ src/aoc1-2test.txt | 8 +++ 3 files changed, 131 insertions(+) create mode 100644 .gitignore create mode 100644 src/aoc1-2.lisp create mode 100644 src/aoc1-2test.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a7b27d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.fasl \ No newline at end of file diff --git a/src/aoc1-2.lisp b/src/aoc1-2.lisp new file mode 100644 index 0000000..2182909 --- /dev/null +++ b/src/aoc1-2.lisp @@ -0,0 +1,122 @@ +;; #!/bin/sbcl --script + +(import 'ppcre) + +(with-open-file (file "aoc1input.txt") + (let((num 0) + (firstMatch nil)(lastMatch nil) + (translation (list "one" "two" "three" "four" "five" "six" "seven" "eight" "nine")) + ) + (loop for line = (read-line file nil) + while line do + (loop for c across line + with currentIdx = 0 + do + (if (parse-integer (format nil "~c" c) :junk-allowed t) + (if firstMatch + (setq lastMatch c) + (setq firstMatch c) + ) + + (loop for numInList in translation + with listIdx = 0 + do + ;;debug + ;;(if (< (+ currentIdx 2) (length line)) (print (subseq line currentIdx (+ currentIdx 3))) ) + ;; + (if(and (< (+ currentIdx 2) (length line)) (string= (subseq line currentIdx (+ currentIdx 3)) numInList) ) + (if firstMatch + (setq lastMatch (digit-char (+ listIdx 1))) + (setq firstMatch (digit-char (+ listIdx 1)) ) + )) + (if (and (< (+ currentIdx 3) (length line)) (string= (subseq line currentIdx (+ currentIdx 4)) numInList) ) + (if firstMatch + (setq lastMatch (digit-char (+ listIdx 1))) + (setq firstMatch (digit-char (+ listIdx 1))) + )) + (if (and (< (+ currentIdx 4) (length line)) (string= (subseq line currentIdx (+ currentIdx 5)) numInList) ) + (if firstMatch + (setq lastMatch (digit-char (+ listIdx 1))) + (setq firstMatch (digit-char (+ listIdx 1))) + )) + (incf listIdx) + ) + ) + (incf currentIdx) + ) + (print firstMatch) + (print lastMatch) + (if (not lastMatch) (setq lastMatch firstMatch)) + (setq num (+ num (parse-integer (format nil "~c~c" firstMatch lastMatch)))) + (print num) + + (print "---") + (setq firstMatch nil) + (setq lastMatch nil) + ) + ) + +) + + + ;; (setq finding (ppcre:all-matches-as-strings "\\d|one|two|three|four|five|six|seven|eight|nine" line :sharedp t )) + ;; (print finding) + ;; ;(print (length(nth 0 (last finding))) ) + ;; (if (> (length(first finding)) 1 ) + ;; (loop for numInList in translation + ;; for currentIdx upfrom 1 + ;; when (string= (first finding) numInList) + ;; do ;(print currentIdx) + ;; (setq lineNum (concatenate 'string lineNum (write-to-string currentIdx)) ) + ;; (return currentIdx) + ;; ) + ;; (setq lineNum (concatenate 'string lineNum (first finding))) ) + + + ;; (if (> (length(nth 0 (last finding))) 1 ) + ;; (loop for numInList in translation + ;; for currentIdx upfrom 1 + ;; when (string= (nth 0 (last finding)) numInList) + ;; do ;(print currentIdx) + ;; (setq lineNum (concatenate 'string lineNum (write-to-string currentIdx)) ) + ;; (return currentIdx) + ;; ) + ;; (setq lineNum (concatenate 'string lineNum (nth 0 (last finding) )) ) + ;; ) + + +;; (defun find-downstep-index (list) +;; (if (endp (rest list)) +;; (length list) +;; (loop :for current-element :in list +;; :for next-element :in (rest list) +;; :for current-index :upfrom 0 +;; :when (> current-element next-element) +;; :do (return-from find-downstep-index current-index) +;; :finally (return (+ current-index 2))))) + + ;(setq lineNum (parse-integer (concatenate 'string (first finding) (nth 0 (last finding)) ))) + ;(setq num (+ num lineNum)) + ;(setq lineNum 0) + ;) + ;(print num) + +;; (with-open-file (file "aoc1test.txt") +;; (let((num 0) (lineNum 0) (finding nil)) +;; (loop for line = (read-line file nil) +;; while line do ;( +;; (setq finding (map 'list #'parse-integer (ppcre:all-matches-as-strings "||d" line ))) +;; (setq lineNum (+ (nth 0 (last finding)) (first finding))) +;; (setq num (+ num lineNum)) +;; (setq lineNum 0) +;; ;) +;; (print num) +;; )) + +;; ;; format t "~a~%" line)) +;; ) + +;; (let ((in (open "/some/file/name.txt" :if-does-not-exist nil))) +;; (when in +;; (close in)) +;; ) diff --git a/src/aoc1-2test.txt b/src/aoc1-2test.txt new file mode 100644 index 0000000..960d7f2 --- /dev/null +++ b/src/aoc1-2test.txt @@ -0,0 +1,8 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen +8